无法验证Webhook签名PHP

时间:2019-10-26 20:36:31

标签: paypal paypal-rest-sdk

我正在尝试从Webhook验证签名,如下所示:

require_once('vendor/autoload.php');

use \PayPal\Api\VerifyWebhookSignature;
use \PayPal\Api\WebhookEvent;
use \PayPal\Api\VerifyWebhookSignatureResponse;

$apiContext = require __DIR__ . '/bootstrap.php';

$bodyReceivedJson = file_get_contents('php://input');
$bodyDecoded = json_decode($requestBodyJson, true);

$headers = getallheaders();
$headers = array_change_key_case($headers, CASE_UPPER);

$signatureVerification = new VerifyWebhookSignature();
$signatureVerification->setAuthAlgo($headers['PAYPAL-AUTH-ALGO']);
$signatureVerification->setTransmissionId($headers['PAYPAL-TRANSMISSION-ID']);
$signatureVerification->setCertUrl($headers['PAYPAL-CERT-URL']);
$signatureVerification->setWebhookId("55555555555555555");
$signatureVerification->setTransmissionSig($headers['PAYPAL-TRANSMISSION-SIG']);
$signatureVerification->setTransmissionTime($headers['PAYPAL-TRANSMISSION-TIME']);

$signatureVerification->setRequestBody($bodyReceivedJson);
$request = clone $signatureVerification;

try{
  $output = $signatureVerification->post($apiContext);
}
catch (Exception $ex){
  exit(1);
}

但是我收到此错误:

[26-Oct-2019 20:18:16 UTC] PHP Fatal error:  Uncaught TypeError: Argument 1 passed to PayPal\Transport\PayPalRestCall::__construct() must be an instance of PayPal\Rest\ApiContext, int given, called in C:\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php on line 101 and defined in C:\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php:38
Stack trace:
#0 C:\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php(101): PayPal\Transport\PayPalRestCall->__construct(1)
#1 C:\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\VerifyWebhookSignature.php(225): PayPal\Common\PayPalResourceModel::executeCall('/v1/notificatio...', 'POST', '{"auth_algo": "...', NULL, 1, NULL)
#2 C:\listener.php(33): PayPal\Api\VerifyWebhookSignature->post(1)
#3 {main}
  thrown in C:\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php on line 38

我不知道此错误试图告诉我什么。

$ request(已修改的ID和我不确定是否应该共享的内容):

{"auth_algo": "SHA256withRSA","transmission_id": "ff0e44a0-f82e-11e9-93f0-f3e5d7c6e89f","cert_url": "https://api.paypal.com/v1/notifications/certs/CERT-360caa44-fsa5a584-5edf0ebc","webhook_id": "55555555555555555","transmission_sig": "Xcc1HHjcjCDZ/EU7oHDQdH/OFCshmbZB1hEHwHSyYUKFBWLkwOgFRhfsnYco10lLZ/oEexycET2pI4c0wpqA7M3UXHUSNxTp8FTeKjSaBqKsCW36hh3fhxhhhghI3CdjhhFZzB06kEzhryVgZpUqyqSz/NihlChhDGiShLfMh4P5hhhhhfeR47GGb5LZlHIlP7C2s9UE8h8heXhh9m/vflhgUDh/EhE/hmQ6+eMsh8oHAhsV8pLCBhkUKTF8tMoPjCVDMhFdFd2Hg5m8MIwbgEzGEYiAPKxkZd3YWXBP1XOgR2MDh6cUidhhC8olh1h6wh5lh3U6hoh4hI1hSezdhQ==","transmission_time": "2019-10-26T20:27:14Z","webhook_event": {"id":"WH-2WR55555HC02533534-65976315F54553755","event_version":"1.0","create_time":"2014-10-23T17:23:52Z","resource_type":"sale","event_type":"PAYMENT.SALE.COMPLETED","summary":"A successful sale payment was made for $ 0.48 USD","resource":{"id":"5555555555555","create_time":"2014-10-23T17:22:56Z","update_time":"2014-10-23T17:23:04Z","amount":{"total":"0.48","currency":"USD"},"payment_mode":"ECHECK","state":"completed","protection_eligibility":"ELIGIBLE","protection_eligibility_type":"ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE","clearing_time":"2014-10-30T07:00:00Z","parent_payment":"PAY-1PA52105FU58455KRE5S4AH","links":[{"href":"https://api.paypal.com/v1/payments/sale/80021663DE681814L","rel":"self","method":"GET"},{"href":"https://api.paypal.com/v1/payments/sale/80021663DE681814L/refund","rel":"refund","method":"POST"},{"href":"https://api.paypal.com/v1/payments/payment/PAY-1PA52105FU58455KRE5S4AH","rel":"parent_payment","method":"GET"}]},"links":[{"href":"https://api.paypal.com/v1/notifications/webhooks-events/WH-2WR55555HC02533534-65976315F54553755","rel":"self","method":"GET"},{"href":"https://api.paypal.com/v1/notifications/webhooks-events/WH-2WR55555HC02533534-65976315F54553755/resend","rel":"resend","method":"POST"}]}}

1 个答案:

答案 0 :(得分:0)

结果是:

$apiContext = require __DIR__ . '/bootstrap.php';

实际上没有得到所需的东西:

  $apiContext  = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
      'YOUR APPLICATION CLIENT ID'
      'YOUR APPLICATION CLIENT SECRET'
    )
  );