微信查询订单API与商派ecstore订单状态对接

今天一个项目用微信扫码支付,客户扫码后卡死在前台。但是金额已经支付成功了。通过百度发现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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值