微信app支付流程
需要的配置参数
private function wechat($body,$indent_id,$cou,$user_id,$total_fee,$ip,$domain,$nonce_str){ //微信配置信息和初始逻辑
$appid= WxPayConfig::APPID; //appid (微信开放平台的应用appid)
$body= $body; //商品描述
$mch_id= WxPayConfig::MCHID; //商户号(注册商户平台时,发置注册邮箱的商户id)
$notify_url= ******; //回调地址(外部可访问的)
$out_trade_no= time(); //商户订单编号自定义
$ip = $ip;
$total_fee= $total_fee; //支付金额 分
$key = WxPayConfig::KEY;(商户平台api支付处设置的key)
$param = $this->signature($appid,$body,$mch_id,$nonce_str,$notify_url,$out_trade_no,$ip,$total_fee,$key); //请求数据转码,xml格式 下文会有介绍
$payment = Payment::create([//插入预订单(这个对应数据库字段就行,根据自己情况来)
'indent_id'=> $indent_id, //订单id
'coupon_id'=> $cou==null?0:$cou->id, //优惠券id
'ordernum'=> $out_trade_no,
'user_id'=> $user_id, //用户id
'update_time' => time(),
'create_time'=> time(),
'way'=> $body,
'content' => 'APP马上拍【微信】'
]);
return $param;
}
2.请求数据组装成xml格式
private function signature($appid,$body,$mch_id,$nonce_str,$notify_url,$out_trade_no,$ip,$total_fee,$key){ //支付请求数据组装
$stringA = "appid=$appid&body=$body&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$out_trade_no&spbill_create_ip=$ip&total_fee=$total_fee&trade_type=APP";
$stringSignTemp = $stringA."&key=$key";
$sign = strtoupper(md5($stringSignTemp)); //签名
$param = "<xml>
";
$param .= "<appid>{$appid}</appid> ";
$param .= "<body>{$body}</body> ";
$param .= "<mch_id>{$mch_id}</mch_id> ";
$param .= "<nonce_str>{$nonce_str}</nonce_str> ";
$param .= "<notify_url>{$notify_url}</notify_url> ";
$param .= "<out_trade_no>{$out_trade_no}</out_trade_no> ";
$param .= "<spbill_create_ip>{$ip}</spbill_create_ip> ";
$param .= "<total_fee>{$total_fee}</total_fee> ";
$param .= "<trade_type>APP</trade_type> ";
$param .= "<sign>{$sign}</sign> ";
$param .= "</xml>";
return $param;
}
3.统一下单,拿到需要的参数,并二次签名,(这一步,就可以拿到所有的字段,倒是后app请求的时候,返回给app就可以,app藉此可以调起app支付)
private function unify_curl($param,$ip,$total_fee,$domain,$body,$timestamp){//统一下单(拿到微信临时会话id,二次签名组装,返回给app ,)
$xml = $this->post_curl("https://api.mch.weixin.qq.com/pay/unifiedorder",$param); //发起请求
$info = json_decode(json_encode(simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA)),true); //数据结果解析
$appid= $info['appid'];//二次签名
$body= $body; //商品描述
$mch_id= $info['mch_id'];
$ip = $ip;
$key = WxPayConfig::KEY;
$prepay_id= $info['prepay_id'];
$noncestr = $info['nonce_str'];
$SignA = strtoupper(md5("appid=$appid&noncestr=$noncestr&package=Sign=WXPay&partnerid=$mch_id&prepayid=$prepay_id×tamp=$timestamp&key=$key"));
$info['sign']= $SignA;
$info['timestamp'] = $timestamp;
return $info;
}
private function post_curl($url,$data,$agreement = 0){//curl远程请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
if($agreement == 0){//0 https 1 http
unset($_REQUEST['agreement']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
4.微信支付回调
function wechat_notify(){
$postStr = @$GLOBALS["HTTP_RAW_POST_DATA"];
$getData = json_decode(json_encode(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
if (!empty($getData['result_code']) || $getData['result_code'] =='SUCCESS') {//如果支付成功
//你的业务逻辑
return 'SUCCESS';
}
}
转载自:https://blog.csdn.net/qq_34629975/article/details/53609241