elliotk
1 year ago

JSON Decode Help

Posted 1 year ago by elliotk

Hello,

I'm getting data from PayPal which I am converting to JSON.

$bodyReceived = file_get_contents('php://input');
$json = json_decode($bodyReceived);

I'm able to access $json->id and $json->resource_type but I am not sure how I can access any objects that are in their own arrays for example $json->resource->state

Can someone help me understand how best to process and access the data? Below is a sample of what comes from PayPal.

array (
  'id' => 'WH-3JG8044908997094X-31F55277P94300413',
  'event_version' => '1.0',
  'create_time' => '2018-09-04T13:15:45.561Z',
  'resource_type' => 'sale',
  'event_type' => 'PAYMENT.SALE.PENDING',
  'summary' => 'Payment pending for $ 0.01 USD',
  'resource' => 
  array (
    'id' => '7TN57446DV952442R',
    'state' => 'pending',
    'amount' => 
    array (
      'total' => '0.01',
      'currency' => 'USD',
      'details' => 
      array (
        'subtotal' => '0.01',
      ),
    ),
    'payment_mode' => 'INSTANT_TRANSFER',
    'reason_code' => 'RECEIVING_PREFERENCE_MANDATES_MANUAL_ACTION',
    'protection_eligibility' => 'ELIGIBLE',
    'protection_eligibility_type' => 'ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE',
    'invoice_number' => '',
    'custom' => '90048630024435',
    'parent_payment' => 'PAY-6HU3694398562973HLOHIK2I',
    'create_time' => '2018-09-04T13:15:37Z',
    'update_time' => '2018-09-04T13:15:37Z',
    'links' => 
    array (
      0 => 
      array (
        'href' => 'https://api.sandbox.paypal.com/v1/payments/sale/7TN57446DV952442R',
        'rel' => 'self',
        'method' => 'GET',
      ),
      1 => 
      array (
        'href' => 'https://api.sandbox.paypal.com/v1/payments/sale/7TN57446DV952442R/refund',
        'rel' => 'refund',
        'method' => 'POST',
      ),
      2 => 
      array (
        'href' => 'https://api.sandbox.paypal.com/v1/payments/payment/PAY-6HU3694398562973HLOHIK2I',
        'rel' => 'parent_payment',
        'method' => 'GET',
      ),
    ),
  ),
  'links' => 
  array (
    0 => 
    array (
      'href' => 'https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-3JG8044908997094X-31F55277P94300413',
      'rel' => 'self',
      'method' => 'GET',
    ),
    1 => 
    array (
      'href' => 'https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-3JG8044908997094X-31F55277P94300413/resend',
      'rel' => 'resend',
      'method' => 'POST',
    ),
  ),
)

Please sign in or create an account to participate in this conversation.