支付宝异步通知数据形式如下,
$arr=array ( 'discount' => '0.00', 'payment_type' => '1', 'subject' => '测试02', 'trade_no' => '2016031.............2958619', 'buyer_email' => '18776152065', 'gmt_create' => '2016-03-14 17:04:52', 'notify_type' => 'trade_status_sync', 'quantity' => '1', 'out_trade_no' => '16031238100064254059', 'seller_id' => '2099122451677261', 'notify_time' => '2016-03-14 17:04:53', 'body' => '测试02', 'trade_status' => 'TRADE_SUCCESS', 'is_total_fee_adjust' => 'N', 'total_fee' => '0.01', 'gmt_payment' => '2016-03-14 17:04:52', 'seller_email' => '[email protected]', 'price' => '0.01', 'buyer_id' => '2065912804441725', 'notify_id' => '2cf4b6638.............c176e3f3lk2', 'use_coupon' => 'N', 'sign_type' => 'RSA', 'sign' => 'Q3uCDyyCs/nODpHaUeCb1HqGINlQ3AxOr40MUN.............Br40MU=', )
对数组按KEY首字母排序
ksort($arr);
按官方文档做法,排除掉sign_type
,sign
然后拼成下面形式的待签名支付串 $SemiFinishedSign
body=Hello&buyer_email=13788888888&buyer_id=2088002007013600 ................................................................................. trade_no=2014040311001004361525&trade_status=TRADE_FINISHED&use_coupon=N
执行下面代码验签!
$pukey=file_get_contents('alipay_public_key.pem'); $publickey=openssl_pkey_get_public($pukey); $verify=(bool)openssl_verify($SemiFinishedSign, base64_decode($arr['sign']), $publickey); echo $verify?'验签成功':'验签失败';
用于支付的公私钥都没有问题,对任意字符,openssl_sign();openssl_verify();都能通过
但是验签总是失败
--------------------------更新 2016-03-15-----------------------------------------
已解决,文档中说,验签用的是支付宝公钥
,并不是RSA公钥,申请接口的人没有给我支付宝公钥,我一直以为用RSA公钥,我用错了,上传公钥,然后复制支付宝公钥下来用于验证签名。
转载:https://segmentfault.com/q/1010000004607880
参考:
https://doc.open.alipay.com/doc2/detail?treeId=59&articleId=103927&docType=1
https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.F1IOBD&treeId=58&articleId=103596&docType=1