您好,登錄后才能下訂單哦!
這兩天優化了一下支付寶支付和微信支付訂單回調的問題,之前我們的訂單都是用手動回調給服務器,現在改成支付寶和微信原生的異步回調,結果并沒有像我們想象的那么簡單,支付寶是很順利的解決回調,用一般的方式接收即可,但是微信接收時用普通的接收方式是不行的必須用另一種方式,如下且看:
支付寶和微信的回調地址
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方面的還請大家一起分享,一起進步。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。