加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何验证PHP中的Paypal webhook签名?

发布时间:2020-12-13 22:25:21 所属栏目:PHP教程 来源:网络整理
导读:我对SSL和证书知之甚少.我用过这个帖子 “ How to use hash_hmac() with “SHA256withRSA” on PHP?”看看我是否可以通过PayPal工作获得webhooks. 我遇到的问题是我在调用openssl_verify()并返回结果为(0)后收到以下错误: OpenSSL error openssl_verify err
我对SSL和证书知之甚少.我用过这个帖子
“ How to use hash_hmac() with “SHA256withRSA” on PHP?”看看我是否可以通过PayPal工作获得webhooks.

我遇到的问题是我在调用openssl_verify()并返回结果为(0)后收到以下错误:

OpenSSL error openssl_verify error:04091068:rsa routines:INT_RSA_VERIFY:bad signature

我试图解决这个问题,但关于错误和网络功能的文档很少见.

我当前的代码如下所示:

// get the header post to my php file by PayPal
 $headers = apache_request_headers();
 // get the body post to me php file by PayPal
 $body = @file_get_contents('php://input');
 $json = json_decode($body);

 // TransmissionId|TransmissionTimeStamp|WebhookId|CRC32 as per PayPal documentation
 $sigString = $headers['Paypal-Transmission-Id'].'|'.$headers['Paypal-Transmission-Time'].'|'.$json->id.'|'.crc32($body);

 // $headers['Paypal-Cert-Url'] contains the "-----BEGIN CERTIFICATE---MIIHmjCCBoKgAwIBAgIQDB8 ... -----END CERTIFICATE-----"
 $pubKey = openssl_pkey_get_public(file_get_contents($headers['Paypal-Cert-Url']));

 // and this is the call to verify that returns result (0)
 $verifyResult = openssl_verify($sigString,base64_decode($headers['Paypal-Transmission-Sig']),$pubKey,'sha256WithRSAEncryption');

只是与我使用的参考代码不同,我不使用openssl_pkey_get_details($pubKey),因为除了现有的签名错误之外我还会得到以下错误:

OpenSSL error openssl_verify error:0906D06C:PEM routines:PEM_read_bio:no start line
OpenSSL error openssl_verify error:04091068:rsa routines:INT_RSA_VERIFY:bad signature

此外,我通过不在标题上使用base64_decode()尝试了一个变体,但是会得到相同的返回结果(0),错误说明:

OpenSSL error openssl_verify error:04091077:rsa routines:INT_RSA_VERIFY:wrong signature length

签名有什么问题?

解决方法

这可能不是您正在寻找的,但使用Open SSL手动验证签名的替代方法可能是使用PayPal PHP Restful API.

PayPal Restful API公开了一个端点,允许您验证webhook:/v1/notifications/verify-webhook-signature

PayPal-PHP-SDK提供了一个VerifyWebhookSignature类,可以轻松调用该端点.

他们还有一个Sample Script,说明了如何使用VerifyWebhookSignature类.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读