PayPal Plus - Webhook - to change payment status in database

661 Views Asked by At

If a customer in my opencart shop pays with paypal I need a webhook to control a payment change like pending, refund, etc.

So, if the customer pays with paypal the method below is called from paypal plus over the 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);
}

What I need at this point is the current transaction_id and the new payment status to update the values in my database.

Could someone tell me how I can get these parameters within the method "webhook"?

Edit:

The result is:

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

There are 2 best solutions below

0
On

Getting the data you need, is easy. You are calling PP and a json_encoded variable ($json) is the result.

Now you have access to these value like:

$webhooks[0]->id

But to get the data you need (here transaction_id and new status), you use the wrong call.

payment/payments/PAYMENT_ID

is the service you need for this.

0
On

Don't call! Wait for a call and process.

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