这两天优化了一下支付宝支付和微信支付订单回调的问题,之前我们的订单都是用手动回调给服务器,现在改成支付宝和微信原生的异步回调,结果并没有像我们想象的那么简单,支付宝是很顺利的解决回调,用一般的方式接收即可,但是微信接收时用普通的接收方式是不行的必须用另一种方式,如下且看:
支付宝和微信的回调地址
notifyUrl = RequestUrl.BASE_URL+"order/order_payment"+"?order_code="+orderCode; |
服务器端是PHP开发,因此此处以PHP为例:
支付宝接收回调的方法
/** * sCreater * function:订单支付完成后回写数据库(支付宝支付回调) * @return json */ public function actionOrder_payment(){ $getData = $this->_requestData; parent::write_log('order/order_payment',$getData); if (empty($getData['order_code'])){ $data = array( 'error_code' => 1, 'error_msg' => '参数错误', 'data' => '' ); parent::json_encode($data); } if (!empty($getData['trade_status']) || $getData['trade_status'] =='TRADE_SUCCESS') { LinshiOrderMaster::confirmPayment($getData['order_code'],$getData['price'],'2',$getData['trade_no']); $data = array( 'error_code' => 0, 'error_msg' => '支付成功', 'data' => '' ); parent::json_encode($data); } } |
支付宝服务器返回的数据
{ "_id": ObjectId("55ec4550c6fdc2f03d8b48c5"), "name": "order/order_payment", "server_url": "http://api.xxx.**/v1.5.4/order/order_payment", "accept_data": { "order_code": "15090604451164", "discount": "0.00", "payment_type": "1", "subject": "陈*梅", "trade_no": "2015090600001000310060229256", "buyer_email": "*********@qq.com", "gmt_create": "2015-09-06 18:29:13", "notify_type": "trade_status_sync", "quantity": "1", "out_trade_no": "090618284176257", "seller_id": "2088021159681245", "notify_time": "2015-09-06 21:53:20", "body": "课程支付", "trade_status": "WAIT_BUYER_PAY", "is_total_fee_adjust": "Y", "total_fee": "0.01", "seller_email": "xxxpay@xxx.biz", "price": "0.01", "buyer_id": "2088302454945312", "notify_id": "4e25737fd44a50070bbaa6f2eac2c39e3q", "use_coupon": "N", "sign_type": "RSA", "sign": "******4GyXJaugFZqoiRQ4DE5VOn/EQjohiCulI5jRuogGiFb7ncZv/FjgZVD00QrnDGxYT8+XUAKThAQ01kCEHJJMLKHMxix9NXdeh8thXcDRBX/MJOnc4C/J8tk+U1D4VwkL1c [...]" }, "header": [
], "time": "21:53:20" } |
微信接收回调方法
/** * sCreater: miki * function:订单支付完成后回写数据库(微信支付回调) * @return json */ public function actionOrder_wx_payment(){ $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; $getData = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); parent::write_log('order/order_wx_payment',$getData); if (empty($getData['order_code'])){ $data = array( 'error_code' => 1, 'error_msg' => '参数错误', 'data' => '' ); parent::json_encode($data); } if (!empty($getData['result_code']) || $getData['result_code'] =='SUCCESS') { LinshiOrderMaster::confirmPayment($getData['order_code'],$getData['total_fee']/100,'1',$getData['transaction_id']); $data = array( 'error_code' => 0, 'error_msg' => '支付成功', 'data' => '' ); parent::json_encode($data); } } |
微信接口返回的数据
{ "_id": ObjectId("55ed2bcdc6fdc2c83d8b4e79"), "name": "order/order_payment", "server_url": "http://api.xxx.***/v1.5.4/order/order_payment", "accept_data": { "appid": "wx8be381be5d594578", "bank_type": "COMM_DEBIT", "cash_fee": "1", "fee_type": "CNY", "is_subscribe": "N", "mch_id": "1240212802", "nonce_str": "9a3d458322d70046f63dfd8b0153ece4", "openid": "*********YfgoQPliYWg", "order_code": "15090604453558", "out_trade_no": "5d79099fcdf499f12b79770834c0164a", "result_code": "SUCCESS", "return_code": "SUCCESS", "sign": "D2AD1EE0F4890FA23B424AC2A94E0CE4", "time_end": "20150907141237", "total_fee": "1", "trade_type": "APP", "transaction_id": "1006410556201509070811059912" }, "header": [
], "time": "14:16:45" } |
注:
1、回调地址之所以加了order_code="+orderCode,主要是便于更新订单的状态,并且支付宝和微信都会自动将我们带的参数插入到原生数据一起返回给我们的服务端;
2、支付宝回调的方法只是普通的post接收即可;
3、微信回调的方法用的关键代码在 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
(1)支付成功通知的内容可以使用 $_GET 获取订单信息
(2)接收微信后台发送过来的消息,该消息数据结构为XML,不是php默认的数据类型
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
(3)使用simplexml_load_string() 函数将接收到的XML消息数据载入对象$postStr中。 $getData = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
总结:支付宝支付和微信支付回调的区别就在于他们回调的数据方式不一样,所以app服务端接收数据的方式也不一样,切记,写此博文方便大家不再走弯路,与君共享! 另外如果有jsp方面的还请大家一起分享,一起进步。
转载于:https://blog.51cto.com/wyong/1692322