Search code examples
phpbraintree

How do I parse braintree create customer response?


I verify a credit card as follows:

$result = Braintree_PaymentMethod::create([
    'paymentMethodNonce' => nonceFromTheClient,
    'options' => [
        'verifyCard' => true
    ]
]);

Once the verification is successful, I get back a huge object that consists of:

Braintree_Result_Successful[Braintree_Customer[id=123456, merchantId=xyz, firstName=, lastName=, company=, email=, phone=, fax=, website=, createdAt=Friday, 11-Sep-15 22:05:53 UTC, updatedAt=Friday, 11-Sep-15 22:05:53 UTC, customFields=, creditCards=0=Braintree_CreditCard[bin=411111, expirationMonth=03, expirationYear=2017, last4=1111, cardType=Visa, cardholderName=sc, commercial=Unknown, countryOfIssuance=Unknown, createdAt=Friday, 11-Sep-15 22:05:53 UTC, customerId=64065056, customerLocation=US, debit=Unknown, default=1, durbinRegulated=Unknown, expired=, healthcare=Unknown, imageUrl=https://assets.braintreegateway.com/payment_method_logo/visa.png?environment=sandbox, issuingBank=Unknown, payroll=Unknown, prepaid=Unknown, subscriptions=, token=384zbr, uniqueNumberIdentifier=123xyz, updatedAt=Friday, 11-Sep-15 22:05:53 UTC, venmoSdk=, verifications=0=status=verified, cvvResponseCode=M, avsErrorResponseCode=, avsPostalCodeResponseCode=I, avsStreetAddressResponseCode=I, gatewayRejectionReason=, merchantAccountId=SignNow, processorResponseCode=1000, processorResponseText=Approved, id=d2jqnd, billing=firstName=, lastName=, company=, streetAddress=, extendedAddress=, locality=, region=, postalCode=, countryName=, creditCard=token=384zbr, bin=411111, last4=1111, cardType=Visa, expirationMonth=03, expirationYear=2017, customerLocation=US, cardholderName=sc, uniqueNumberIdentifier=xyz123, prepaid=Unknown, healthcare=Unknown, debit=Unknown, durbinRegulated=Unknown, commercial=Unknown, payroll=Unknown, issuingBank=Unknown, countryOfIssuance=Unknown, productId=Unknown, createdAt=Friday, 11-Sep-15 22:05:52 UTC, updatedAt=Friday, 11-Sep-15 22:05:53 UTC, riskData=id=xyz, decision=Approve, billingAddress=, expirationDate=03/2017, maskedNumber=411111******1111, ......]]]]] [] []

I want to read the credit card token value as seen in the above response: "token=384zbr"

I tried accessing it in the following ways, which did not work

$result->creditCard->token
$result->creditCards[0]->token

I get an exception that says "Undefined property on Braintree_Result_Successful: creditCard [] []."


Solution

  • Braintree support helped me correct it. The correct way to access the token is

    $result->customer->paymentMethods[0]->token;