1.获取prepay_id
2.根据prepay_id获取支付参数
后台
$wx_pay_config = [];
$wx_pay_config['appid'] = config('appid');
$wx_pay_config['appsecret'] = config('appsecret');
$wx_pay_config['token'] = config('token');
$wx_pay_config['encodingaeskey'] = config('encodingaeskey');
$wx_pay_config['mch_id'] = config('mch_id');
$wx_pay_config['partnerkey'] = config('partnerkey');
$wx_pay_config['cert_key'] = config('cert_key');
$wx_pay_config['cert_cert'] = config('cert_cert');
$wechatpay = new WechatPay( $wx_pay_config );
$openid = $order_info['openid'];
$body = $order_info['product_name'];
$out_trade_no = $order_info['order_sn'];
$total_fee = $order_info['total_price'] * 100;
$notify_url = config('url_domain_root') .'mobile/Notify/index';
$prepayid = $wechatpay->getPrepayId( $openid, $body, $out_trade_no, $total_fee, $notify_url );
$wx_res = $wechatpay -> createMchPay( $prepayid );
$jsticket = new WechatScript( $this->option );
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$jssign_info = $jsticket -> getJsSign( $url );
//return view( 'index', [ 'data'=> json_encode( $wx_res ) ] );
return view( 'index', [ 'title' => '支付订单', 'data'=> $wx_res, 'obj'=> $jssign_info, 'order_info'=>$order_info] );
前台
<script src="http://res.wx.qq.com/open/js/jweixin-1.1.0.js"></script>
<script>
wx.config({
debug: false,
appId: '{$obj["appId"]}',
timestamp: '{$obj["timestamp"]}',
nonceStr: '{$obj["nonceStr"]}',
signature: '{$obj["signature"]}',
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'chooseWXPay',
]
});
</script>
<script type="text/javascript">
function getOrder(){
wx.chooseWXPay({
timestamp: '{$data["timestamp"]}', // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: '{$data["nonceStr"]}', // 支付签名随机串,不长于 32 位
package: '{$data["package"]}', // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id =***)
signType: '{$data["signType"]}', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: '{$data["paySign"]}', // 支付签名
success: function (res) {
// 支付成功后的回调函数
alert('支付成功!');
window.location.href = "{:url('Mycenter/index')}";
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
}
</script>
注意事项:
1.商户后台填写授权支付域名,如果带参数,最好带一个,其余参数可以根据这个去获取,如:http://xxxxxx/mobile/wxpay/index/order_id/33.html
这时候授权支付域名就是 http://xxxxxx/mobile/wxpay/index/order_id/
2.支付成功异步回调,一定不能继承逻辑代码的父类,最好单独一个类