2

I'm trying to implement a Paypal Webhook. I need an event every time a user paid with Paypal for a product.

enter image description here

I created a webhook linked to the "PAYEMENT.SALE.COMPLETED" event.

And this is my handleWebHook.php file on my server:

<?php
include "vendor/autoload.php";

use PayPal\Api\Webhook;
use PayPal\Api\WebhookEvent;
use PayPal\Api\WebhookEventType;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;

$apiContext = new ApiContext(
    new OAuthTokenCredential(
        'my client id',     // ClientID
        'my client secret'      // ClientSecret
    )
);
$apiContext->setConfig(
    array(
        'log.LogEnabled' => true,
        'log.FileName' => 'PayPal.log',
        'log.LogLevel' => 'DEBUG'
    )
);

$params = array(
);

// ### Search Webhook events
try {
    $output = WebhookEvent::all($params, $apiContext);
} catch (Exception $ex) {
    exit(1);
}

//ResultPrinter::printResult("Search Webhook events", "WebhookEventList", null, $params, $output);

$log  = "Datas: " . print_r($output) . "  " . date("F j, Y, g:i a").PHP_EOL.
    "-------------------------".PHP_EOL;
file_put_contents('./log_'.date("j.n.Y").'.log', $log, FILE_APPEND);

I tried to simulate webhook event with the API call https://api.sandbox.paypal.com/v1/notifications/simulate-event and with the online webhook simulator.

It seems to work in both ways, the result of the API call is a 202 Accepted with a lot of infirmation in the body including

"resource_type": "sale",
"event_type": "PAYMENT.SALE.COMPLETED",
"summary": "A successful sale payment was made for $ 0.48 USD", 

But I can't retrieve those events on my listener, my logs are empty and even if I manually go to the handleWebHook.php page on a browser, I got

PayPal\Api\WebhookEventList Object ( [_propMap:PayPal\Common\PayPalModel:private] => Array ( [events] => Array ( ) [count] => 0 ) )

witch mean there are no event.

I also tried the https://api.sandbox.paypal.com/v1/notifications/webhooks-events API call, but I've got the same result

{
    "events": [],
    "count": 0
}

in the request response body.

Why can't I retrieve the event on my listener ?

CE_
  • 1,078
  • 2
  • 16
  • 33
  • Try to log down what is coming in to the php to inspect it. Use `file_get_contents('php://input')` to grab the raw data sent from paypal. I've not actually used `WebhookEvent::all` before, as we first do `VerifyWebhookSignature`, then if that is a success, the `json_decode` the raw body and do our processes based on that result. – IncredibleHat Aug 20 '20 at 15:08
  • @IncredibleHat I tried this : $json = file_get_contents("php://input"); $data = json_decode($json, true); Then put it in the log File. The log File IS created, so I do received something but it's only "1" – CE_ Aug 20 '20 at 15:24
  • @IncredibleHat Maybe I absolutely need to verify the Webhook Signature, using this https://paypal.github.io/PayPal-PHP-SDK/sample/doc/notifications/ValidateWebhookEvent.html , but Why do I need bootstrap ? – CE_ Aug 20 '20 at 15:33
  • 1
    Why not log just the raw `$json`? If you are trying to log to file with that `print_r()` as you have it, you will not see anything because you have forgotten to include `true`. `$log = 'stuff '. print_r($array) .' etc';` will not do what you think it does. But doing `$log = 'stuff '. print_r($array,true) .' etc';` will. – IncredibleHat Aug 20 '20 at 16:03
  • 1
    Bootstrap is just that documentations way of including a common pile of junk for all. It sets up the api context and such. Not needed if you are doing your own at the top of the script. – IncredibleHat Aug 20 '20 at 16:04
  • 1
    Turned out All my code was useless, all I had to do was simply to log the result of $bodyReceived = file_get_contents('php://input');. – CE_ Aug 20 '20 at 16:19
  • Welcome to working with PayPal code! :-D ... I've been there too. – IncredibleHat Aug 20 '20 at 16:36

0 Answers0