刷卡支付是用戶展示微信錢包內的「收付款/向商家付款」(舊版微信APP中為「刷卡條碼/二維碼」)給商戶系統掃瞄後直接完成支付的模式。它主要應用線下面對面收款的場景。
微信用戶進入「我」|「錢包」|「收付款」(舊版微信APP中為「付款」)界面,將出現一個條碼和二維碼,如圖17-8所示。
圖17-8 付款碼
刷卡條形碼的規則是18位純數字,以10、11、12、13、14、15開頭。商戶用掃碼設備掃瞄用戶的條碼/二維碼後,將向微信提交支付。微信支付後台系統收到支付請求,根據驗證密碼規則判斷是否驗證用戶的支付密碼,不需要驗證密碼的交易直接發起扣款,需要驗證密碼的交易會彈出密碼輸入框。支付成功後微信端會彈出成功頁面,支付失敗會彈出錯誤提示。
提交刷卡支付接口的地址如下。
https:// api.mch.weixin.qq.com/pay/micropay
提交刷卡支付時,POST數據示例如下。
<xml> <appid>wx2421b1c4370ec43b</appid> <attach>訂單額外描述</attach> <auth_code>120269300684844649</auth_code> <body>刷卡支付測試</body> <device_info>1000</device_info> <goods_tag></goods_tag> <mch_id>10000100</mch_id> <nonce_str>8aaee146b1dee7cec9100add9b96cbe2</nonce_str> <out_trade_no>1415757673</out_trade_no> <spbill_create_ip>14.17.22.52</spbill_create_ip> <time_expire></time_expire> <total_fee>1</total_fee> <sign>C29DB7DB1FD4136B84AE35604756362C</sign> </xml>
上述數據的參數說明如表17-4所示。
表17-4 刷卡支付接口的參數說明
正確創建時,返回的數據示例如下。
<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> <appid><![CDATA[wx2421b1c4370ec43b]]></appid> <mch_id><![CDATA[10000100]]></mch_id> <device_info><![CDATA[1000]]></device_info> <nonce_str><![CDATA[GOp3TRyMXzbMlkun]]></nonce_str> <sign><![CDATA[D6C76CB785F07992CDE05494BB7DF7FD]]></sign> <result_code><![CDATA[SUCCESS]]></result_code> <openid><![CDATA[oUpF8uN95-Ptaags6E_roPHg7AG0]]></openid> <is_subscribe><![CDATA[Y]]></is_subscribe> <trade_type><![CDATA[MICROPAY]]></trade_type> <bank_type><![CDATA[CCB_DEBIT]]></bank_type> <total_fee>1</total_fee> <coupon_fee>0</coupon_fee> <fee_type><![CDATA[CNY]]></fee_type> <transaction_id><![CDATA[1008450740201411110005820873]]></transaction_id> <out_trade_no><![CDATA[1415757673]]></out_trade_no> <attach><![CDATA[訂單額外描述]]></attach> <time_end><![CDATA[20141111170043]]></time_end> </xml>
上述數據的參數說明如表17-5所示。
表17-5 刷卡支付接口返回參數說明
刷卡支付的類的實現代碼如下。
1 /** 2 * 刷卡支付接口類 3 */ 4 class MicroPay_pub extends Wxpay_client_pub 5 { 6 function __construct 7 { 8 // 設置接口鏈接 9 $this->url = "https:// api.mch.weixin.qq.com/pay/micropay"; 10 // 設置curl超時時間 11 $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; 12 } 13 14 /** 15 * 生成接口參數XML 16 */ 17 function createXml 18 { 19 try 20 { 21 // 檢測必填參數 22 if($this->parameters["out_trade_no"] == null){ 23 throw new SDKRuntimeException("缺少統一支付接口必填參數out_trade_no!". "<br>"); 24 }elseif($this->parameters["body"] == null){ 25 throw new SDKRuntimeException("缺少統一支付接口必填參數body!"."<br>"); 26 }elseif ($this->parameters["total_fee"] == null ) { 27 throw new SDKRuntimeException("缺少統一支付接口必填參數total_fee!". "<br>"); 28 }elseif ($this->parameters["auth_code"] == null) { 29 throw new SDKRuntimeException("缺少統一支付接口必填參數auth_code!". "<br>"); 30 } 31 $this->parameters["appid"] = WxPayConf_pub::APPID; // 公眾賬號ID 32 $this->parameters["mch_id"] = WxPayConf_pub::MCHID;// 商戶號 33 $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];// 終端IP 34 $this->parameters["nonce_str"] = $this->createNoncestr;// 隨機字符串 35 $this->parameters["sign"] = $this->getSign($this->parameters);// 簽名 36 // var_dump($this->parameters); 37 return $this->arrayToXml($this->parameters); 38 }catch (SDKRuntimeException $e) 39 { 40 die($e->errorMessage); 41 } 42 } 43 }
而調用刷卡支付的時候,只需要傳入金額以及用戶提供授權碼即可。其實現代碼如下。
// 使用刷卡支付接口 $microPay = new MicroPay_pub; // 設置刷卡支付接口參數 $microPay->setParameter("body","方倍商戶掃碼支付"); ???// 商品描述 $microPay->setParameter("out_trade_no", "$out_trade_no");// 商戶訂單號 $microPay->setParameter("total_fee", $total_fee); ?// 總金額 $microPay->setParameter("auth_code", $authcode); // 授權碼 $microPay->setParameter("attach", $unionid); // 附加數據,此處為方倍平台的unionid // 獲取刷卡支付接口結果 $microPayResult = $microPay->getResult;