Parsing/using WebhookEvent data sent to a PHP listener

465 Views Asked by At

I'm receiving the JSON data from PayPal's webhook to my end point.

I can of course parse it into a PHP object. Is this the best way to handle this data? I would have thought that the JSON data could be converted back into a new 'WebhookEvent' object and then handled that way?? But either that's not the case or I'm doing something wrong.

use \PayPal\Api\WebhookEvent;

$webhook_json_data = '{"auth_algo": "SHA256withRSA","transmission_id": "d97395d0-de4a06f7","cert_url": "https://api.sandbox.paypal.com/v1/notifications/certs/CERT-360caa42-fca2a594-aecacc47","webhook_id": "2V9E918418","transmission_sig": "Svsp/4l67b4sasdq/rjQojgKoOG95rltTuEtpip/UeNxTap7I/ao7Vg7VDJIYPaEzTz1UDNdsmI+/l1dhVfQsEAImg1bz0VpZ+0+JAwUuZpr0EtF7g==","transmission_time": "2018-11-16T03:01:01Z","webhook_event": {"id":"WH-6DT935852K089633W-9X42044474276933K","event_version":"1.0","create_time":"2018-11-16T03:01:01.000Z","resource_type":"payouts","event_type":"PAYMENT.PAYOUTSBATCH.SUCCESS","summary":"Payouts batch completed successfully.","resource":{"batch_header":{"payout_batch_id":"7PHK3S3LKM8D6","batch_status":"SUCCESS","time_created":"2018-11-16T03:00:42Z","time_completed":"2018-11-16T03:01:01Z","sender_batch_header":{"sender_batch_id":"5bee32daa6bd8"},"amount":{"currency":"USD","value":"235.0"},"fees":{"currency":"USD","value":"1.25"},"payments":5},"links":[{"href":"https://api.sandbox.paypal.com/v1/payments/payouts/7PHM8D6","rel":"self","method":"GET"}]},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-6DT9276933K","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-6DT933K/resend","rel":"resend","method":"POST"}]}}';

$webhook_obj =  json_decode ( $webhook_json_data );

$w_event = new \PayPal\Api\WebhookEvent($webhook_json_data);

// this gives PAYMENT.PAYOUTSBATCH.SUCCESS
echo $webhook_obj->webhook_event->event_type ."<br>"; 

// this yields nothing !! (the $w_event data appears to 
// be 'protected' and thus inaccessible)
echo $w_event->event_type ."<br>";  

Is the correct way to use the data received on a Webhook notification to just walk through the JSON decoded structure? For example, I want to confirm the total amount of the Payouts payment, which in this case is $235.00

I can retrieve it using:

$webhook_obj->webhook_event->resource->batch_header->amount->value

But it seems like there should be a better way to process these Webhook notifications??

0

There are 0 best solutions below