微信查询订单API与商派ecstore订单状态对接
生活随笔
收集整理的這篇文章主要介紹了
微信查询订单API与商派ecstore订单状态对接
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
今天一個項目用微信掃碼支付,客戶掃碼后卡死在前臺。但是金額已經支付成功了。通過百度發現https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1。屬于下面情況:
提醒1:提交支付請求后微信會同步返回支付結果(后續說明異步的方法)。當返回結果為“系統錯誤”時,商戶系統等待5秒后調用【查詢訂單API】,查詢支付實際交易結果;當返回結果為“USERPAYING”時,商戶系統可設置間隔時間(建議10秒)重新查詢支付結果,直到支付成功或超時(建議30秒);
我用計劃任務做了查詢訂單API,根據訂單支付成功結果修改ecstore表的狀態。代碼如下:
<?php /*** 微信檢查訂單后,修改ecsotre表支付狀態*/Class checkPay{public function __construct() {date_default_timezone_set('Asia/Shanghai');require_once("../config/config.php");require_once("./dbase.php");$this->dbase = new DB();$this->dbase->connect(DB_HOST, DB_USER, DB_PASSWORD,DB_NAME );$this->is_log = "./logs/checkPay".date("Ymd").".log";$this->wx['Appid'] = "*";$this->wx['mch_id'] = "*";$this->wx['key'] = "*"; }public function getSdbOrder() {$sql = "select order_id From sdb_b2c_orders where pay_status='0' and status='active'"; //pay_status表示未成功的$rows= $this->dbase->get_rows($sql);foreach ($rows as $key => $value) {$cash_fee = $this->getWxOrderInfo($value['order_id']);if ($cash_fee>0) {$sql = "UPDATE `sdb_b2c_orders` SET `pay_status`='1', `payed`='{$cash_fee}',updatestats_tag='微信查帳修改' WHERE `order_id`={$value['order_id']}";$this->dbase->query($sql); $this->dbase->write_log("line 32: sql=".$sql,$this->is_log);}}}public function getWxOrderInfo($order_id) {//微信的$wx['Appid'] = $this->wx['Appid'];$wx['mch_id'] = $this->wx['mch_id'];//$wx['out_trade_no'] = "161222225332249";$wx['out_trade_no'] = $order_id;$wx['nonce_str'] = "abcdefg2abcdefg";$wx['sign'] = $this->weiXinSign($wx);$xmlStr = "<xml>";$xmlStr .="<appid>{$wx['Appid']}</appid>";$xmlStr .="<mch_id>{$wx['mch_id']}</mch_id>";$xmlStr .="<nonce_str>{$wx['nonce_str']}</nonce_str>";$xmlStr .="<out_trade_no>{$wx['out_trade_no']}</out_trade_no>";$xmlStr .="<sign>{$wx['sign']}</sign>";$xmlStr .="</xml>";$sendpost = $this->sendpost($xmlStr);$this->dbase->write_log("line 35: sendpost=".$sendpost,$this->is_log);$postObj = simplexml_load_string($sendpost, 'SimpleXMLElement', LIBXML_NOCDATA);$cash_fee= 0 ;if ($postObj->trade_state =='SUCCESS' && $postObj->result_code=='SUCCESS' ) {$cash_fee= sprintf("%.2f",$postObj->cash_fee/100);}return $cash_fee;}private function weiXinSign($wx) {//微信的$signArr = array();$signArr['appid']= $this->wx['Appid'];$signArr['mch_id']= $this->wx['mch_id'];$signArr['nonce_str']=$wx['nonce_str'];$signArr['out_trade_no']=$wx['out_trade_no'];ksort($signArr);$signArr2 = array();foreach ($signArr as $key => $value) {$signArr2[] = $key.'='.$value;}//去掉開頭的&并且進行MD5加密處理$stringSignTemp = implode("&",$signArr2).'&key='.$this->wx['key'];$sign = md5($stringSignTemp);$sign = strtoupper($sign);return $sign;}private function sendpost($data){$url = "https://api.mch.weixin.qq.com/pay/orderquery";$ch = curl_init(); $header[] = "Content-type: text/xml";//定義content-type為xml curl_setopt($ch, CURLOPT_URL, $url); //定義表單提交地址 curl_setopt($ch, CURLOPT_POST, 1); //定義提交類型 1:POST ;0:GET curl_setopt($ch, CURLOPT_HEADER, 0); //定義是否顯示狀態頭 1:顯示 ; 0:不顯示 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//定義請求類型 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//定義是否直接輸出返回流 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定義提交的數據,這里是XML文件 $result = curl_exec($ch); curl_close($ch);//關閉 return $result;} }$cPay = new checkPay();$cPay->getSdbOrder();使用計劃任務php checkPay.php 就可以了上面還有一個工作沒有做,就是商派的ecstore沒有支付訂單記錄。這個后續有要求在加入。分析一下,其實就是涉及下面的幾個表INSERT INTO `zhihuimendian`.`sdb_ectools_order_bills` (`rel_id`, `bill_id`, `money`) VALUES ('161222200529117', '16122220052911700001', '33.900'); //插入支付表與order的關聯UPDATE `sdb_b2c_orders` SET `pay_status`='1', `payed`='0.100',updatestats_tag='微信查帳修改' WHERE `order_id`=161222200529117; //修改支付狀態轉載于:https://my.oschina.net/7795442/blog/811037
總結
以上是生活随笔為你收集整理的微信查询订单API与商派ecstore订单状态对接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1-1. 虚拟机控制
- 下一篇: LEADTOOLS Multimedia