提醒1:提交支付请求后微信会同步返回支付结果(后续说明异步的方法)。当返回结果为“系统错误”时,商户系统等待5秒后调用【查询订单API】,查询支付实际交易结果;当返回结果为“USERPAYING”时,商户系统可设置间隔时间(建议10秒)重新查询支付结果,直到支付成功或超时(建议30秒);
我用计划任务做了查询订单API,根据订单支付成功结果修改ecstore表的状态。代码如下:
/**
* 微信检查订单后,修改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 = "";
$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;
}
//去掉开头的&并且进行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; //修改支付状态