PayPal Plus - Webhook - 用于更改数据库中的付款状态

3

如果我的 Opencart 商店客户使用 PayPal 进行支付,我需要一个 Webhook 来控制付款变化,如待定、退款等。

因此,如果客户使用 PayPal 进行支付,则从 PayPal Plus 通过 Webhook URL 调用以下方法:

public function webhook(){

    $token = $this->getToken();

    $mode = ".sandbox";

    $ch = curl_init();

    $header = array('Content-Type: application/json', 'Authorization:Bearer'.$token);

    curl_setopt($ch,  CURLOPT_HTTHEADER, $headers);

    curl_setopt($ch, CURLOPT_URL, "https://api".$mode."paypal.com/v1/notification/webhooks/");

    curl_setopt($ch,  CURLOPT_HEADER, false);
    curl_setopt($ch,  CURLOPT_SSL_VERYFYPEER, false);
    curl_setopt($ch,  CURLOPT_CUSTOMREQUEST, 'GET');
    curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);

    $json = json_decode($result);
}

目前我需要的是当前交易ID和新的付款状态,以便更新我的数据库中的值。

有人能告诉我如何在“webhook”方法中获取这些参数吗?

编辑:

结果为:

json stdClass Object
(
    [webhooks] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 5EB94006KU40xxxxx
                    [url] => https://shopexample.de/index.php?route=payment/pp_plus/webhook
                    [event_types] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [name] => *
                                    [description] => ALL
                                    [status] => ENABLED
                                )

                        )

                    [links] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [href] => https://api.sandbox.paypal.com/v1/notifications/webhooks/5EB94006KU40xxxxx
                                    [rel] => self
                                    [method] => GET
                                )

                            [1] => stdClass Object
                                (
                                    [href] => https://api.sandbox.paypal.com/v1/notifications/webhooks/5EB94006KU40xxxxx
                                    [rel] => update
                                    [method] => PATCH
                                )

                            [2] => stdClass Object
                                (
                                    [href] => https://api.sandbox.paypal.com/v1/notifications/webhooks/5EB94006KU40xxxxx
                                    [rel] => delete
                                    [method] => DELETE
                                )

                        )

                )

        )

)

print_r($result) 是什么? - Prince Adeyemi
嗨,我已将结果添加到文本中。我不知道如何从最后一次付款中获取transaction_id和付款状态。 - Nils
2个回答

0

不要主动调用!等待调用并处理。

public function webhook() {
    $body = file_get_contents('php: //input');

    $data = json_decode($body, true);

    //Validate and verify webhook here.

    if ($data['event_type'] == 'PAYMENT.AUTHORIZATION.CREATED') {
        //Payment authorization created.
    } else if ($data['event_type'] == 'PAYMENT.AUTHORIZATION.VOIDED') {
        //Payment authorization voided.
    } else if ($data['event_type'] == 'PAYMENT.CAPTURE.COMPLETED') {
        //Payment capture completed.
    } else if ($data['event_type'] == 'PAYMENT.CAPTURE.DENIED') {
        //Payment capture denied.
    } else if ($data['event_type'] == 'PAYMENT.CAPTURE.PENDING') {
        //Payment capture pending.
    } else if ($data['event_type'] == 'PAYMENT.CAPTURE.REFUNDED') {
        //Payment capture refunded.
    } else if ($data['event_type'] == 'PAYMENT.CAPTURE.REVERSED') {
        //Payment capture reversed.
    } else {
        //Handle other webhooks
    }
}

0

获取所需的数据很容易。

您正在调用PP,json_encoded变量($json)是结果。

现在您可以访问这些值,例如:

$webhooks[0]->id

但是要获取所需的数据(这里是交易 ID 和新状态),您使用了错误的调用。

payment/payments/PAYMENT_ID

这是您所需的服务。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接