easy引入请使用composer自行处理
php代码:
if ($order['pay_price'] > 0) { /*返回给支付二维码 还有官方收款码*/ $app = $this->get_wechat_pay_app(); $jssdk = $app->jssdk; $pay_param = $app->order->unify([ 'body' => '会查查-参赛报名费', 'out_trade_no' => $order['order_sn'], 'total_fee' => $order['pay_price'] * 100, 'spbill_create_ip' => get_client_ip(), // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址 // 'notify_url' => 'https://www.huixx.cn/index/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型 'openid' => $this->user['wechat_openid'], //'sandbox' => true, // 设置为 false 或注释则关闭沙箱模式 ]); $prepayId = $pay_param['prepay_id']; $jsApiParameters = $jssdk->bridgeConfig($prepayId); $order['pay_param'] = json_decode($jsApiParameters); }
封装方法:
/**获取微信支付APP*/ public function get_wechat_pay_app() { $config = [ // 必要配置 'app_id' => sysconfig('pre', 'appid'), 'mch_id' => sysconfig('pre', 'mchid'), 'key' => sysconfig('pre', 'mchkey'), // API 密钥 // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书) // 'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!! //'key_path' => 'path/to/your/key', // XXX: 绝对路径!!!! 'notify_url' => 'https://www.huixx.cn/index/notify', // 你也可以在下单时单独设置来想覆盖它 ]; $app = Factory::payment($config); return $app; }
html代码:
<script> let order_id = {$order.id}; let jsApiParameters = {:json_encode($order.pay_param)}; function callPay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest',jsApiParameters, function(res){ //WeixinJSBridge.log(res.err_msg); if (res.err_msg == "get_brand_wcpay_request:ok") { //alert('支付成功') //可以进行查看订单,等操作 window.location.href = '/index/wap/match_pay_success/order_id/'+order_id; } else { layer.msg('取消支付') return } //alert(res.err_code+res.err_desc+res.err_msg); } ); } </script>