Search code examples
phpjsonlaravel-5paypalbraintree

How to get the ID out of Braintree's JSON response?


I'm trying to integrate PayPal Express Checkout using Braintree SDK.

I can so far charge the Nonce returned from the client and I receive this response. However, I need to take the ID out in order to save it in a table.

My question is how do I parse the id??

Successful {#315
  +success: true
  -_returnObjectNames: array:1 [
    0 => "transaction"
  ]
  #_attributes: []
  +"transaction": Transaction {#324
    #_attributes: array:63 [
      "id" => "xxx"// How do I parse this out?
      "status" => "settling"
      "type" => "sale"
      "currencyIsoCode" => "USD"
      "amount" => "6.00"

The variable that holds this JSON is $success. Thus, I tried several things like:

$success->transaction->id
$success->id

If I do $success->success I get true and If I do $success->transaction I get the Transaction object.


Solution

  • Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.

    If running $success->successreturns True and $success->transaction returns the Transaction object, then you should be able to query the transaction ID using $success->transaction->id, we also demonstrate this in our developer documentation.

    $result = $gateway->transaction()->sale([
      'amount' => '10.00',
      'paymentMethodNonce' => nonceFromTheClient,
      'options' => [
        'submitForSettlement' => True
      ]
    ]);
    
    if ($result->success) {
      // See $result->transaction for details
    } else {
     // Handle errors
    }