PayPal Plus - Webhook - 更改数据库

时间:2017-08-31 21:31:55

标签: php curl paypal opencart paypal-webhooks

如果我的opencart商店的客户使用paypal付款,我需要一个webhook来控制付款变更,例如待处理,退款等。

因此,如果客户使用paypal付款,则通过paypal以及webhook网址调用以下方法:

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);
}

此时我需要的是当前的transaction_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
                                )

                        )

                )

        )

)

2 个答案:

答案 0 :(得分:0)

轻松获取所需数据。 您正在调用PP,结果为json_encoded变量($ json)。

现在您可以访问以下值:

$webhooks[0]->id

但是要获取所需的数据(此处为transaction_id和新状态),请使用错误的调用。

payment/payments/PAYMENT_ID

是您需要的服务。

答案 1 :(得分: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
    }
}