本文介绍的是微信支付sdk+微擎,不是微擎原生支付
微信支付回调携带订单号,判断订单号获取uniacid
1、回调入口加session
//小程序支付回调
public function receiveNotify()
{
$notify = new WxNotify();
$notify->Handle();
}
2、配置文件加构造函数
创建订单和回调支付都会用到WxPayConfig.php
所以从回调入口加session,这里判断是否执行
//WxPayConfig.php
public function __construct()
{
if(session('notify')){
if(!session('wx_ucid')){
return;
}else{
$uniacid=session('wx_ucid');
}
}else{
$uniacid=TokenService::getCTVar('ucid');
if(!$uniacid){
$uniacid=TokenService::getCTVar('uniacid');
}
if(!$uniacid){
throw new Exception("缺少标识");
}
}
$xcx=SysConfig::where(['uniacid'=>$uniacid])->select();
$arr=[];
foreach ($xcx as $k=>$v){
$arr[$v['key']]=$v['value'];
}
$this->APPID = $arr['wx_app_id'];
$this->APPSECRET = $arr['wx_app_secret'];
$this->MCHID = $arr['pay_num'];
$this->KEY = $arr['pay_key'];
$this->CURL_PROXY_HOST = "0.0.0.0";
$this->CURL_PROXY_PORT = 0;
$this->REPORT_LEVENL= 1;
$this->SSLCERT_PATH = '/cert/apiclient_cert.pem';
$this->SSLKEY_PATH = '/cert/apiclient_key.pem';
}
3、获取uniacid
//WxPayData.php
public function FromXml($xml)
{
if(!$xml){
throw new WxPayException("xml数据异常!");
}
//将XML转为array
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$order_num=$this->values['out_trade_no'];
$uniacid=Order::where('order_num',$order_num)->value('uniacid');
if($uniacid) {
Log::error('ucid:'.$uniacid);
session('wx_ucid', $uniacid);
}
return $this->values;
}
4、更新订单状态
这里不需要修改
//自定义类继承NotifyProcess
public function NotifyProcess($data, &$msg){
...
}
5、回调结束告知微信服务器时,清空session
//WxPayNotify.php
final private function ReplyNotify($needSign = true)
{
//如果需要签名
if($needSign == true &&
$this->GetReturn_code() == "SUCCESS")
{
$this->SetSign();
}
session('notify',null);
session('wx_ucid',null);
WxpayApi::replyNotify($this->ToXml());
}