我正在从 PayPal 的 webhook 接收 JSON 数据到我的终点。
我当然可以将其解析为 PHP 对象。这是处理这些数据的最佳方式吗?我原以为可以将 JSON 数据转换回新的“WebhookEvent”对象,然后以这种方式处理?但要么事实并非如此,要么我做错了什么。
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>";
使用 Webhook 通知中收到的数据来遍历 JSON 解码结构的正确方法是什么?例如,我想确认付款的总金额,在本例中为 $235.00
我可以使用以下方法检索它:
$webhook_obj->webhook_event->resource->batch_header->amount->value
但似乎应该有更好的方法来处理这些 Webhook 通知?