import base64
import binascii


def get_payment_data(token):
    """Build a string of the concatenated payment
    data provided in the apple pay token, including
    the ephemeral public key, payload, transaction id
    and the optional application data.

    This assumes the provided token contains an ECDSA
    signature.  RSA is not supported.

    Args:
        token (dict): the decoded apple pay token
    Returns:
        str: the concatenated payment data
    """
    ephemeral_public_key = token['header']['ephemeralPublicKey']
    payload = token['data']
    transaction_id = token['header']['transactionId']
    application_data = token['header'].get('applicationData')  # optional

    concatenated_data = base64.b64decode(ephemeral_public_key)
    concatenated_data += base64.b64decode(payload)
    concatenated_data += binascii.unhexlify(transaction_id)
    if application_data:
        concatenated_data += binascii.unhexlify(application_data)

    return concatenated_data