刷卡支付是用户展示微信钱包内的“收付款/向商家付款”(旧版微信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;