#### 1. 前言 --- 公眾號(hào)網(wǎng)頁(yè)中調(diào)起微信支付,常用的支付類型就是 JSAPI 準(zhǔn)備工作: 1、公眾號(hào):APPID、配置網(wǎng)頁(yè)授權(quán)域名(設(shè)置與開(kāi)發(fā)-公眾號(hào)設(shè)置-網(wǎng)頁(yè)授權(quán)域名) 2、微信商戶號(hào):關(guān)聯(lián)公眾號(hào),商戶號(hào)ID、商戶API密鑰、JSAPI支付授權(quán)目錄 補(bǔ)充:微信支付不需要商戶證書文件,在企業(yè)付款到零錢、退款等敏感操作才使用到證書 安裝 easywechat 4.x 版本,PHP版本要求 7.0.+ ```html composer require overtrue/wechat:~4.0 ``` #### 2. 獲取微信支付實(shí)例 ---- 微信支付配置:[https://easywechat.com/docs/4.x/payment/index](https://easywechat.com/docs/4.x/payment/index) ``` use EasyWeChat\Factory; $config = [ // 支付所需配置 'app_id' => 'xxxx',//公眾號(hào)APPID 'mch_id' => 'your-mch-id',//商戶號(hào)ID 'key' => 'key-for-signature',// 商戶號(hào)API 密鑰 'notify_url' => '默認(rèn)的訂單回調(diào)地址',// 你也可以在下單時(shí)單獨(dú)設(shè)置來(lái)想覆蓋它 ]; $app = Factory::payment($config); ``` #### 3. 統(tǒng)一下單,生成預(yù)支付交易單 --- 統(tǒng)一下單:[https://easywechat.com/docs/4.x/payment/order](https://easywechat.com/docs/4.x/payment/order) JSAPI 支付官方開(kāi)發(fā)文檔:[https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1](https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1) ``` $result = $app->order->unify([ 'body' => '騰訊充值中心-QQ會(huì)員充值',//商品名稱 'out_trade_no' => '20150806125346', // 商家訂單號(hào)(自己生成隨機(jī)訂單號(hào)) 'total_fee' => 88, // 支付金額,單位:分 'notify_url' => 'https://pay.weixin.qq.com/wxpay/pay.action', // 支付結(jié)果通知網(wǎng)址 'trade_type' => 'JSAPI', // 請(qǐng)對(duì)應(yīng)換成你的支付方式對(duì)應(yīng)的值類型 'openid' => 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o', ]); if (isset($result['result_code']) && $result['result_code'] === 'SUCCESS') { // 預(yù)支付交易單生成成功 $prepayId = $result['prepay_id']; } else { // 預(yù)支付交易單生成失敗 } ``` 返回結(jié)果示例:預(yù)支付交易單生成成功 ```php [ "return_code" => "SUCCESS" "return_msg" => "OK" "result_code" => "SUCCESS" "mch_id" => "1491118112" "appid" => "wx4f54d5e8808e0b5e" "nonce_str" => "PZ9DbpDDTjlan3rQ" "sign" => "C3CBC4D8D94D310C0DA5B31BA3C20E69" "prepay_id" => "wx191119031406819681e5d74f3761470000" "trade_type" => "JSAPI" ] ``` 預(yù)支付交易單生成失敗 ```php [ "return_code" => "SUCCESS" "return_msg" => "OK" "result_code" => "FAIL" "err_code_des" => "無(wú)效的openid" "err_code" => "PARAM_ERROR" "mch_id" => "1491118112" "appid" => "wx4f54d5e8808e0b5e" "nonce_str" => "20GD48pP9GlPJVSt" "sign" => "782E4BE6B6559ECE250689E65A7BB935" ] ``` #### 4. 生成支付 JS 配置 ---- 生成支付 JS 配置(三種發(fā)起方式):[https://easywechat.com/docs/4.x/payment/jssdk](https://easywechat.com/docs/4.x/payment/jssdk) 以 `WeixinJSBridge.invoke()` 為例 ``` $json = $jssdk->bridgeConfig($prepayId); ``` $json 是一個(gè)json字符串,示例值如下 ``` { "appId": "wx4f54d5e8808e0b5e", "timeStamp": "1624082215", "nonceStr": "60cd87271f9dc", "package": "prepay_id=wx191356511785218014f91263552d430000", "signType": "MD5", "paySign": "9A06CA325371AA7EC627BEFF34AA0325" } ```