微信支付开发(7) 刷卡支付
關(guān)鍵字:微信支付 微信支付v3 刷卡支付?統(tǒng)一支付 prepay_id?
作者:方倍工作室
本文介紹微信支付下的刷卡支付的開發(fā)過程。微信刷卡支付是指用戶打開微信錢包的刷卡的界面,商戶掃碼后提交完成支付的支付過程。
?
?
一、刷卡支付API
接口地址
https://api.mch.weixin.qq.com/pay/micropay是否需要證書
不需要。
輸入?yún)?shù)
| 公眾賬號(hào)ID | appid | 是 | String(32) | wx8888888888888888 | 微信分配的公眾賬號(hào)ID(企業(yè)號(hào)corpid即為此appId) |
| 商戶號(hào) | mch_id | 是 | String(32) | 1900000109 | 微信支付分配的商戶號(hào) |
| 設(shè)備號(hào) | device_info | 否 | String(32) | 013467007045764 | 終端設(shè)備號(hào)(商戶自定義,如門店編號(hào)) |
| 隨機(jī)字符串 | nonce_str | 是 | String(32) | 5K8264ILTKCH16CQ2502SI8ZNMTM67VS | 隨機(jī)字符串,不長(zhǎng)于32位。推薦隨機(jī)數(shù)生成算法 |
| 簽名 | sign | 是 | String(32) | C380BEC2BFD727A4B6845133519F3AD6 | 簽名,詳見簽名生成算法 |
| 商品描述 | body | 是 | String(128) | image形象店-深圳騰大- QQ公仔 | 商品簡(jiǎn)單描述,該字段須嚴(yán)格按照規(guī)范傳遞,具體請(qǐng)見參數(shù)規(guī)定 |
| 商品詳情 | detail | 否 | String(6000) | { "goods_detail":[ { "goods_id":"iphone6s_16G", "wxpay_goods_id":"1001", "goods_name":"iPhone6s 16G", "goods_num":1, "price":528800, "goods_category":"123456", "body":"蘋果手機(jī)" }, { "goods_id":"iphone6s_32G", "wxpay_goods_id":"1002", "goods_name":"iPhone6s 32G", "quantity":1, "price":608800, "goods_category":"123789", "body":"蘋果手機(jī)" } ] } | 商品詳細(xì)列表,使用Json格式,傳輸簽名前請(qǐng)務(wù)必使用CDATA標(biāo)簽將JSON文本串保護(hù)起來。 goods_detail []: |
| 附加數(shù)據(jù) | attach | 否 | String(127) | 說明 | 附加數(shù)據(jù),在查詢API和支付通知中原樣返回,該字段主要用于商戶攜帶訂單的自定義數(shù)據(jù) |
| 商戶訂單號(hào) | out_trade_no | 是 | String(32) | 1217752501201407033233368018 | 商戶系統(tǒng)內(nèi)部的訂單號(hào),32個(gè)字符內(nèi)、可包含字母,其他說明見商戶訂單號(hào) |
| 商品詳情 | detail | 否 | String(8192) | 與提交數(shù)據(jù)一致 | 實(shí)際提交的返回 |
| 訂單金額 | total_fee | 是 | Int | 888 | 訂單總金額,單位為分,只能為整數(shù),詳見支付金額 |
| 貨幣類型 | fee_type | 否 | String(16) | CNY | 符合ISO4217標(biāo)準(zhǔn)的三位字母代碼,默認(rèn)人民幣:CNY,其他值列表詳見貨幣類型 |
| 終端IP | spbill_create_ip | 是 | String(16) | 8.8.8.8 | 調(diào)用微信支付API的機(jī)器IP |
| 商品標(biāo)記 | goods_tag | 否 | String(32) | ? | 商品標(biāo)記,代金券或立減優(yōu)惠功能的參數(shù),說明詳見代金券或立減優(yōu)惠 |
| 指定支付方式 | limit_pay | 否 | String(32) | no_credit | no_credit--指定不能使用信用卡支付 |
| 授權(quán)碼 | auth_code | 是 | String(128) | 120061098828009406 | 掃碼支付授權(quán)碼,設(shè)備讀取用戶微信中的條碼或者二維碼信息 |
舉例如下:
<xml><appid>wx2421b1c4370ec43b</appid><attach>訂單額外描述</attach><auth_code>120269300684844649</auth_code><body>刷卡支付測(cè)試</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>注:參數(shù)值用XML轉(zhuǎn)義即可,CDATA標(biāo)簽用于說明數(shù)據(jù)不被XML解析器解析。
返回結(jié)果
| 返回狀態(tài)碼 | return_code | 是 | String(16) | SUCCESS | SUCCESS/FAIL? 此字段是通信標(biāo)識(shí),非交易標(biāo)識(shí),交易是否成功需要查看result_code來判斷 |
| 返回信息 | return_msg | 否 | String(128) | 簽名失敗 | 返回信息,如非空,為錯(cuò)誤原因? 簽名失敗? 參數(shù)格式校驗(yàn)錯(cuò)誤 |
當(dāng)return_code為SUCCESS的時(shí)候,還會(huì)包括以下字段:
| 公眾賬號(hào)ID | appid | 是 | String(32) | wx8888888888888888 | 調(diào)用接口提交的公眾賬號(hào)ID |
| 商戶號(hào) | mch_id | 是 | String(32) | 1900000109 | 調(diào)用接口提交的商戶號(hào) |
| 設(shè)備號(hào) | device_info | 否 | String(32) | 013467007045764 | 調(diào)用接口提交的終端設(shè)備號(hào), |
| 隨機(jī)字符串 | nonce_str | 是 | String(32) | 5K8264ILTKCH16CQ2502SI8ZNMTM67VS | 微信返回的隨機(jī)字符串 |
| 簽名 | sign | 是 | String(32) | C380BEC2BFD727A4B6845133519F3AD6 | 微信返回的簽名,詳見簽名生成算法 |
| 業(yè)務(wù)結(jié)果 | result_code | 是 | String(16) | SUCCESS | SUCCESS/FAIL |
| 錯(cuò)誤代碼 | err_code | 否 | String(32) | SYSTEMERROR | 詳細(xì)參見錯(cuò)誤列表 |
| 錯(cuò)誤代碼描述 | err_code_des | 否 | String(128) | 系統(tǒng)錯(cuò)誤 | 錯(cuò)誤返回的信息描述 |
當(dāng)return_code 和result_code都為SUCCESS的時(shí),還會(huì)包括以下字段:
| 用戶標(biāo)識(shí) | openid | 是 | String(128) | Y | 用戶在商戶appid 下的唯一標(biāo)識(shí) |
| 是否關(guān)注公眾賬號(hào) | is_subscribe | 是 | String(1) | Y | 用戶是否關(guān)注公眾賬號(hào),僅在公眾賬號(hào)類型支付有效,取值范圍:Y或N;Y-關(guān)注;N-未關(guān)注 |
| 交易類型 | trade_type | 是 | String(16) | MICROPAY | 支付類型為MICROPAY(即掃碼支付) |
| 付款銀行 | bank_type | 是 | String(16) | CMC | 銀行類型,采用字符串類型的銀行標(biāo)識(shí),值列表詳見銀行類型 |
| 貨幣類型 | fee_type | 否 | String(16) | CNY | 符合ISO 4217標(biāo)準(zhǔn)的三位字母代碼,默認(rèn)人民幣:CNY,其他值列表詳見貨幣類型 |
| 訂單金額 | total_fee | 是 | Int | 888 | 訂單總金額,單位為分,只能為整數(shù),詳見支付金額 |
| 現(xiàn)金支付貨幣類型 | cash_fee_type | 否 | String(16) | CNY | 符合ISO 4217標(biāo)準(zhǔn)的三位字母代碼,默認(rèn)人民幣:CNY,其他值列表詳見貨幣類型 |
| 現(xiàn)金支付金額 | cash_fee | 是 | Int | 100 | 訂單現(xiàn)金支付金額,詳見支付金額 |
| 微信支付訂單號(hào) | transaction_id | 是 | String(32) | 1217752501201407033233368018 | 微信支付訂單號(hào) |
| 商戶訂單號(hào) | out_trade_no | 是 | String(32) | 1217752501201407033233368018 | 商戶系統(tǒng)的訂單號(hào),與請(qǐng)求一致。 |
| 商家數(shù)據(jù)包 | attach | 否 | String(128) | 123456 | 商家數(shù)據(jù)包,原樣返回 |
| 支付完成時(shí)間 | time_end | 是 | String(14) | 20141030133525 | 訂單生成時(shí)間,格式為yyyyMMddHHmmss,如2009年12月25日9點(diǎn)10分10秒表示為20091225091010。詳見時(shí)間規(guī)則 |
舉例如下:
<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>?
二、刷卡支付類實(shí)現(xiàn)
在微信支付原來的微信支付類文件中,仿照統(tǒng)一支付類的方式,添加刷卡支付類如下:
/*** 刷卡支付接口類*/ class MicroPay_pub extends Wxpay_client_pub { function __construct() {//設(shè)置接口鏈接$this->url = "https://api.mch.weixin.qq.com/pay/micropay";//設(shè)置curl超時(shí)時(shí)間$this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;}/*** 生成接口參數(shù)xml*/function createXml(){try{//檢測(cè)必填參數(shù)if($this->parameters["out_trade_no"] == null){throw new SDKRuntimeException("缺少統(tǒng)一支付接口必填參數(shù)out_trade_no!"."<br>");}elseif($this->parameters["body"] == null){throw new SDKRuntimeException("缺少統(tǒng)一支付接口必填參數(shù)body!"."<br>");}elseif ($this->parameters["total_fee"] == null ) {throw new SDKRuntimeException("缺少統(tǒng)一支付接口必填參數(shù)total_fee!"."<br>");}elseif ($this->parameters["auth_code"] == null) {throw new SDKRuntimeException("缺少統(tǒng)一支付接口必填參數(shù)auth_code!"."<br>");}$this->parameters["appid"] = WxPayConf_pub::APPID;//公眾賬號(hào)ID$this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商戶號(hào)$this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//終端ip $this->parameters["nonce_str"] = $this->createNoncestr();//隨機(jī)字符串$this->parameters["sign"] = $this->getSign($this->parameters);//簽名// var_dump($this->parameters);return $this->arrayToXml($this->parameters);}catch (SDKRuntimeException $e){die($e->errorMessage());}} }原有的基礎(chǔ)類和請(qǐng)求類也列出如下:
/*** 所有接口的基類*/ class Common_util_pub {function __construct() {}function trimString($value){$ret = null;if (null != $value) {$ret = $value;if (strlen($ret) == 0) {$ret = null;}}return $ret;}/*** 作用:產(chǎn)生隨機(jī)字符串,不長(zhǎng)于32位*/public function createNoncestr( $length = 32 ) {$chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str ="";for ( $i = 0; $i < $length; $i++ ) { $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); } return $str;}/*** 作用:格式化參數(shù),簽名過程需要使用*/function formatBizQueryParaMap($paraMap, $urlencode){$buff = "";ksort($paraMap);foreach ($paraMap as $k => $v){if($urlencode){$v = urlencode($v);}//$buff .= strtolower($k) . "=" . $v . "&";$buff .= $k . "=" . $v . "&";}$reqPar;if (strlen($buff) > 0) {$reqPar = substr($buff, 0, strlen($buff)-1);}return $reqPar;}/*** 作用:生成簽名*/public function getSign($Obj){foreach ($Obj as $k => $v){$Parameters[$k] = $v;}//簽名步驟一:按字典序排序參數(shù)ksort($Parameters);$String = $this->formatBizQueryParaMap($Parameters, false);//echo '【string1】'.$String.'</br>';//簽名步驟二:在string后加入KEY$String = $String."&key=".WxPayConf_pub::KEY;//echo "【string2】".$String."</br>";//簽名步驟三:MD5加密$String = md5($String);//echo "【string3】 ".$String."</br>";//簽名步驟四:所有字符轉(zhuǎn)為大寫$result_ = strtoupper($String);//echo "【result】 ".$result_."</br>";return $result_;}/*** 作用:array轉(zhuǎn)xml*/function arrayToXml($arr){$xml = "<xml>";foreach ($arr as $key=>$val){if (is_numeric($val)){$xml.="<".$key.">".$val."</".$key.">"; }else$xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; }$xml.="</xml>";return $xml; }/*** 作用:將xml轉(zhuǎn)為array*/public function xmlToArray($xml){ //將XML轉(zhuǎn)為array $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $array_data;}/*** 作用:以post方式提交xml到對(duì)應(yīng)的接口url*/public function postXmlCurl($xml,$url,$second=30){ //初始化curl $ch = curl_init();//設(shè)置超時(shí)curl_setopt($ch, CURLOP_TIMEOUT, $second);//這里設(shè)置代理,如果有的話//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);curl_setopt($ch,CURLOPT_URL, $url);curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//設(shè)置headercurl_setopt($ch, CURLOPT_HEADER, FALSE);//要求結(jié)果為字符串且輸出到屏幕上curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//post提交方式curl_setopt($ch, CURLOPT_POST, TRUE);curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);//運(yùn)行curl$data = curl_exec($ch);curl_close($ch);//返回結(jié)果if($data){curl_close($ch);return $data;}else { $error = curl_errno($ch);echo "curl出錯(cuò),錯(cuò)誤碼:$error"."<br>"; echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>錯(cuò)誤原因查詢</a></br>";curl_close($ch);return false;}}/*** 作用:使用證書,以post方式提交xml到對(duì)應(yīng)的接口url*/function postXmlSSLCurl($xml,$url,$second=30){$ch = curl_init();//超時(shí)時(shí)間curl_setopt($ch,CURLOPT_TIMEOUT,$second);//這里設(shè)置代理,如果有的話//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);curl_setopt($ch,CURLOPT_URL, $url);curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//設(shè)置headercurl_setopt($ch,CURLOPT_HEADER,FALSE);//要求結(jié)果為字符串且輸出到屏幕上curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);//設(shè)置證書//使用證書:cert 與 key 分別屬于兩個(gè).pem文件//默認(rèn)格式為PEM,可以注釋curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');curl_setopt($ch,CURLOPT_SSLCERT, dirname(__FILE__).WxPayConf_pub::SSLCERT_PATH);//默認(rèn)格式為PEM,可以注釋curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');curl_setopt($ch,CURLOPT_SSLKEY, dirname(__FILE__).WxPayConf_pub::SSLKEY_PATH);//post提交方式curl_setopt($ch,CURLOPT_POST, true);curl_setopt($ch,CURLOPT_POSTFIELDS,$xml);$data = curl_exec($ch);//返回結(jié)果if($data){curl_close($ch);return $data;}else { $error = curl_errno($ch);echo "curl出錯(cuò),錯(cuò)誤碼:$error"."<br>"; echo "<a href='http://curl.haxx.se/libcurl/c/libcurl-errors.html'>錯(cuò)誤原因查詢</a></br>";curl_close($ch);return false;}}/*** 作用:打印數(shù)組*/function printErr($wording='',$err=''){print_r('<pre>');echo $wording."</br>";var_dump($err);print_r('</pre>');} }/*** 請(qǐng)求型接口的基類*/ class Wxpay_client_pub extends Common_util_pub {var $parameters;//請(qǐng)求參數(shù),類型為關(guān)聯(lián)數(shù)組public $response;//微信返回的響應(yīng)public $result;//返回參數(shù),類型為關(guān)聯(lián)數(shù)組var $url;//接口鏈接var $curl_timeout;//curl超時(shí)時(shí)間/*** 作用:設(shè)置請(qǐng)求參數(shù)*/function setParameter($parameter, $parameterValue){$this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue);}/*** 作用:設(shè)置標(biāo)配的請(qǐng)求參數(shù),生成簽名,生成接口參數(shù)xml*/function createXml(){$this->parameters["appid"] = WxPayConf_pub::APPID;//公眾賬號(hào)ID$this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商戶號(hào)$this->parameters["nonce_str"] = $this->createNoncestr();//隨機(jī)字符串$this->parameters["sign"] = $this->getSign($this->parameters);//簽名return $this->arrayToXml($this->parameters);}/*** 作用:post請(qǐng)求xml*/function postXml(){$xml = $this->createXml();$this->response = $this->postXmlCurl($xml,$this->url,$this->curl_timeout);return $this->response;}/*** 作用:使用證書post請(qǐng)求xml*/function postXmlSSL(){ $xml = $this->createXml();$this->response = $this->postXmlSSLCurl($xml,$this->url,$this->curl_timeout);return $this->response;}/*** 作用:獲取結(jié)果,默認(rèn)不使用證書*/function getResult() { $this->postXml();$this->result = $this->xmlToArray($this->response);return $this->result;} }?
?
三、發(fā)起支付
在程序中,獲得用戶的授權(quán)碼,并填入到$authcode參數(shù)中。授權(quán)碼就是條碼上的那一串18位純數(shù)字,以10、11、12、13、14、15開頭
其他參數(shù)則自動(dòng)生成或者手動(dòng)輸入指定。
調(diào)用函數(shù)如下所示
//全局引入微信支付類Vendor('Wxpay.WxPayPubHelper.WxPayPubHelper');//使用統(tǒng)一支付接口$microPay = new \MicroPay_pub();//設(shè)置統(tǒng)一支付接口參數(shù)$microPay->setParameter("body","方倍商戶刷卡支付");//商品描述$microPay->setParameter("out_trade_no", "$out_trade_no");//商戶訂單號(hào) $microPay->setParameter("total_fee", $total_fee);//總金額 $microPay->setParameter("auth_code", $authcode);//授權(quán)碼 //獲取統(tǒng)一支付接口結(jié)果$microPayResult = $microPay->getResult();//3. 異常判斷if (!isset($microPayResult["result_code"]) || ($microPayResult["result_code"] == "FAIL")) {$this->resRpcError(isset($microPayResult['result_code']) ? $microPayResult['err_code_des'] : $microPayResult['return_msg'], "21000");}?
轉(zhuǎn)載于:https://www.cnblogs.com/lanzhi/p/6467363.html
總結(jié)
以上是生活随笔為你收集整理的微信支付开发(7) 刷卡支付的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 场景服务只创建了 Service Dif
- 下一篇: div+css使多行文字垂直居中?