您好,登錄后才能下訂單哦!
這兩天優(yōu)化了一下支付寶支付和微信支付訂單回調(diào)的問題,之前我們的訂單都是用手動回調(diào)給服務器,現(xiàn)在改成支付寶和微信原生的異步回調(diào),結(jié)果并沒有像我們想象的那么簡單,支付寶是很順利的解決回調(diào),用一般的方式接收即可,但是微信接收時用普通的接收方式是不行的必須用另一種方式,如下且看:
支付寶和微信的回調(diào)地址
notifyUrl = RequestUrl.BASE_URL+"order/order_payment"+"?order_code="+orderCode; |
服務器端是PHP開發(fā),因此此處以PHP為例:
支付寶接收回調(diào)的方法
/** * sCreater * function:訂單支付完成后回寫數(shù)據(jù)庫(支付寶支付回調(diào)) * @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' => '參數(shù)錯誤', '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); } } |
支付寶服務器返回的數(shù)據(jù)
{ "_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" } |
微信接收回調(diào)方法
/** * sCreater: miki * function:訂單支付完成后回寫數(shù)據(jù)庫(微信支付回調(diào)) * @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' => '參數(shù)錯誤', '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); } } |
微信接口返回的數(shù)據(jù)
{ "_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、回調(diào)地址之所以加了order_code="+orderCode,主要是便于更新訂單的狀態(tài),并且支付寶和微信都會自動將我們帶的參數(shù)插入到原生數(shù)據(jù)一起返回給我們的服務端;
2、支付寶回調(diào)的方法只是普通的post接收即可;
3、微信回調(diào)的方法用的關鍵代碼在 $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
(1)支付成功通知的內(nèi)容可以使用 $_GET 獲取訂單信息
(2)接收微信后臺發(fā)送過來的消息,該消息數(shù)據(jù)結(jié)構(gòu)為XML,不是php默認的數(shù)據(jù)類型
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
(3)使用simplexml_load_string() 函數(shù)將接收到的XML消息數(shù)據(jù)載入對象$postStr中。 $getData = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
總結(jié):支付寶支付和微信支付回調(diào)的區(qū)別就在于他們回調(diào)的數(shù)據(jù)方式不一樣,所以app服務端接收數(shù)據(jù)的方式也不一樣,切記,寫此博文方便大家不再走彎路,與君共享! 另外如果有jsp方面的還請大家一起分享,一起進步。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。