ecstore mysql_微信查询订单API与商派ecstore订单状态对接
提醒1:提交支付請(qǐng)求后微信會(huì)同步返回支付結(jié)果(后續(xù)說明異步的方法)。當(dāng)返回結(jié)果為“系統(tǒng)錯(cuò)誤”時(shí),商戶系統(tǒng)等待5秒后調(diào)用【查詢訂單API】,查詢支付實(shí)際交易結(jié)果;當(dāng)返回結(jié)果為“USERPAYING”時(shí),商戶系統(tǒng)可設(shè)置間隔時(shí)間(建議10秒)重新查詢支付結(jié)果,直到支付成功或超時(shí)(建議30秒);
我用計(jì)劃任務(wù)做了查詢訂單API,根據(jù)訂單支付成功結(jié)果修改ecstore表的狀態(tài)。代碼如下:
/**
* 微信檢查訂單后,修改ecsotre表支付狀態(tài)
*/
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 = "";
$xmlStr .="{$wx['Appid']}";
$xmlStr .="{$wx['mch_id']}";
$xmlStr .="{$wx['nonce_str']}";
$xmlStr .="{$wx['out_trade_no']}";
$xmlStr .="{$wx['sign']}";
$xmlStr .="";
$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;
}
//去掉開頭的&并且進(jìn)行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); //定義是否顯示狀態(tài)頭 1:顯示 ; 0:不顯示
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//定義請(qǐng)求類型
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//定義是否直接輸出返回流
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定義提交的數(shù)據(jù),這里是XML文件
$result = curl_exec($ch);
curl_close($ch);//關(guān)閉
return $result;
}
}
$cPay = new checkPay();
$cPay->getSdbOrder();
使用計(jì)劃任務(wù)php checkPay.php 就可以了
上面還有一個(gè)工作沒有做,就是商派的ecstore沒有支付訂單記錄。這個(gè)后續(xù)有要求在加入。
分析一下,其實(shí)就是涉及下面的幾個(gè)表
INSERT INTO `zhihuimendian`.`sdb_ectools_order_bills` (`rel_id`, `bill_id`, `money`) VALUES ('161222200529117', '16122220052911700001', '33.900'); //插入支付表與order的關(guān)聯(lián)
UPDATE `sdb_b2c_orders` SET `pay_status`='1', `payed`='0.100',updatestats_tag='微信查帳修改' WHERE `order_id`=161222200529117; //修改支付狀態(tài)
總結(jié)
以上是生活随笔為你收集整理的ecstore mysql_微信查询订单API与商派ecstore订单状态对接的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3.8.2_Python|P
- 下一篇: 大学计算机专业绩点在3.5算好,大学平均