編輯:關於Android編程
本文介紹微信支付下的刷卡支付的開發過程。微信刷卡支付是指用戶打開微信錢包的刷卡的界面,商戶掃碼後提交完成支付的支付過程。


https://api.mch.weixin.qq.com/pay/micropay
是否需要證書
不需要。
商品詳細列表,使用Json格式,傳輸簽名前請務必使用CDATA標簽將JSON文本串保護起來。
goods_detail []:
└ goods_id String 必填 32 商品的編號
└ wxpay_goods_id String 可選 32 微信支付定義的統一商品編號
└ goods_name String 必填 256 商品名稱
└ goods_num Int 必填 商品數量
└ price Int 必填 商品單價,單位為分
└ goods_category String 可選 32 商品類目ID
└ body String 可選 1000 商品描述信息
實際提交的返回
訂單金額 total_fee 是 Int 888 訂單總金額,單位為分,只能為整數,詳見支付金額 貨幣類型 fee_type 否 String(16) CNY 符合ISO4217標准的三位字母代碼,默認人民幣:CNY,其他值列表詳見貨幣類型 終端IP spbill_create_ip 是 String(16) 8.8.8.8 調用微信支付API的機器IP 商品標記 goods_tag 否 String(32) 商品標記,代金券或立減優惠功能的參數,說明詳見代金券或立減優惠 指定支付方式 limit_pay 否 String(32) no_credit no_credit--指定不能使用信用卡支付 授權碼 auth_code 是 String(128) 120061098828009406 掃碼支付授權碼,設備讀取用戶微信中的條碼或者二維碼信息舉例如下:
wx2421b1c4370ec43b 訂單額外描述 120269300684844649刷卡支付測試 1000 10000100 8aaee146b1dee7cec9100add9b96cbe2 1415757673 14.17.22.52 1 C29DB7DB1FD4136B84AE35604756362C
注:參數值用XML轉義即可,CDATA標簽用於說明數據不被XML解析器解析。
當return_code為SUCCESS的時候,還會包括以下字段:
當return_code 和result_code都為SUCCESS的時,還會包括以下字段:
舉例如下:
1 0
在微信支付原來的微信支付類文件中,仿照統一支付類的方式,添加刷卡支付類如下:
/**
* 刷卡支付接口類
*/
class MicroPay_pub extends Wxpay_client_pub
{
function __construct()
{
//設置接口鏈接
$this->url = "https://api.mch.weixin.qq.com/pay/micropay";
//設置curl超時時間
$this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT;
}
/**
* 生成接口參數xml
*/
function createXml()
{
try
{
//檢測必填參數
if($this->parameters["out_trade_no"] == null){
throw new SDKRuntimeException("缺少統一支付接口必填參數out_trade_no!"."
");
}elseif($this->parameters["body"] == null){
throw new SDKRuntimeException("缺少統一支付接口必填參數body!"."
");
}elseif ($this->parameters["total_fee"] == null ) {
throw new SDKRuntimeException("缺少統一支付接口必填參數total_fee!"."
");
}elseif ($this->parameters["auth_code"] == null) {
throw new SDKRuntimeException("缺少統一支付接口必填參數auth_code!"."
");
}
$this->parameters["appid"] = WxPayConf_pub::APPID;//公眾賬號ID
$this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商戶號
$this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR'];//終端ip
$this->parameters["nonce_str"] = $this->createNoncestr();//隨機字符串
$this->parameters["sign"] = $this->getSign($this->parameters);//簽名
// var_dump($this->parameters);
return $this->arrayToXml($this->parameters);
}catch (SDKRuntimeException $e)
{
die($e->errorMessage());
}
}
}
原有的基礎類和請求類也列出如下:
/**
* 所有接口的基類
*/
class Common_util_pub
{
function __construct() {
}
function trimString($value)
{
$ret = null;
if (null != $value)
{
$ret = $value;
if (strlen($ret) == 0)
{
$ret = null;
}
}
return $ret;
}
/**
* 作用:產生隨機字符串,不長於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;
}
/**
* 作用:格式化參數,簽名過程需要使用
*/
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;
}
//簽名步驟一:按字典序排序參數
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//echo '【string1】'.$String.'
';
//簽名步驟二:在string後加入KEY
$String = $String."&key=".WxPayConf_pub::KEY;
//echo "【string2】".$String."
";
//簽名步驟三:MD5加密
$String = md5($String);
//echo "【string3】 ".$String."
";
//簽名步驟四:所有字符轉為大寫
$result_ = strtoupper($String);
//echo "【result】 ".$result_."
";
return $result_;
}
/**
* 作用:array轉xml
*/
function arrayToXml($arr)
{
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."<!--{cke_protected}{C}%3C!%2D%2D%22.%24key.%22%2D%2D%3E-->";
}
else
$xml.="<".$key."><!--{cke_protected}{C}%3C!%2D%2D%5BCDATA%5B%22.%24val.%22%5D%5D%2D%2D%3E--><!--{cke_protected}{C}%3C!%2D%2D%22.%24key.%22%2D%2D%3E-->";
}
$xml.="</xml>";
return $xml;
}
/**
* 作用:將xml轉為array
*/
public function xmlToArray($xml)
{
//將XML轉為array
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
/**
* 作用:以post方式提交xml到對應的接口url
*/
public function postXmlCurl($xml,$url,$second=30)
{
//初始化curl
$ch = curl_init();
//設置超時
curl_setopt($ch, CURLOP_TIMEOUT, $second);
//這裡設置代理,如果有的話
//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);
//設置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求結果為字符串且輸出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//運行curl
$data = curl_exec($ch);
curl_close($ch);
//返回結果
if($data)
{
curl_close($ch);
return $data;
}
else
{
$error = curl_errno($ch);
echo "curl出錯,錯誤碼:$error"."
";
echo "<a data-cke-saved-href="http://curl.haxx.se/libcurl/c/libcurl-errors.html" href="http://curl.haxx.se/libcurl/c/libcurl-errors.html">錯誤原因查詢</a>
";
curl_close($ch);
return false;
}
}
/**
* 作用:使用證書,以post方式提交xml到對應的接口url
*/
function postXmlSSLCurl($xml,$url,$second=30)
{
$ch = curl_init();
//超時時間
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
//這裡設置代理,如果有的話
//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);
//設置header
curl_setopt($ch,CURLOPT_HEADER,FALSE);
//要求結果為字符串且輸出到屏幕上
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
//設置證書
//使用證書:cert 與 key 分別屬於兩個.pem文件
//默認格式為PEM,可以注釋
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, dirname(__FILE__).WxPayConf_pub::SSLCERT_PATH);
//默認格式為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);
//返回結果
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "curl出錯,錯誤碼:$error"."
";
echo "<a data-cke-saved-href="http://curl.haxx.se/libcurl/c/libcurl-errors.html" href="http://curl.haxx.se/libcurl/c/libcurl-errors.html">錯誤原因查詢</a>
";
curl_close($ch);
return false;
}
}
/**
* 作用:打印數組
*/
function printErr($wording='',$err='')
{
print_r('
'); echo $wording."
"; var_dump($err); print_r('
'); } } /** * 請求型接口的基類 */ class Wxpay_client_pub extends Common_util_pub { var $parameters;//請求參數,類型為關聯數組 public $response;//微信返回的響應 public $result;//返回參數,類型為關聯數組 var $url;//接口鏈接 var $curl_timeout;//curl超時時間 /** * 作用:設置請求參數 */ function setParameter($parameter, $parameterValue) { $this->parameters[$this->trimString($parameter)] = $this->trimString($parameterValue); } /** * 作用:設置標配的請求參數,生成簽名,生成接口參數xml */ function createXml() { $this->parameters["appid"] = WxPayConf_pub::APPID;//公眾賬號ID $this->parameters["mch_id"] = WxPayConf_pub::MCHID;//商戶號 $this->parameters["nonce_str"] = $this->createNoncestr();//隨機字符串 $this->parameters["sign"] = $this->getSign($this->parameters);//簽名 return $this->arrayToXml($this->parameters); } /** * 作用:post請求xml */ function postXml() { $xml = $this->createXml(); $this->response = $this->postXmlCurl($xml,$this->url,$this->curl_timeout); return $this->response; } /** * 作用:使用證書post請求xml */ function postXmlSSL() { $xml = $this->createXml(); $this->response = $this->postXmlSSLCurl($xml,$this->url,$this->curl_timeout); return $this->response; } /** * 作用:獲取結果,默認不使用證書 */ function getResult() { $this->postXml(); $this->result = $this->xmlToArray($this->response); return $this->result; } }
在程序中,獲得用戶的授權碼,並填入到$authcode參數中。授權碼就是條碼上的那一串18位純數字,以10、11、12、13、14、15開頭
其他參數則自動生成或者手動輸入指定。
調用函數如下所示
//全局引入微信支付類
Vendor('Wxpay.WxPayPubHelper.WxPayPubHelper');
//使用統一支付接口
$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);//授權碼
//獲取統一支付接口結果
$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");
}
Android進階——Material Design新控件之TextInputLayout
引言Android L之前,很多時候我們在使用EditText輸入時,希望給及時糾正用戶錯誤的格式輸入,常常會監聽一些事件,然後給出一些提示語言,而顯示提示語言的有可能是
Android For JNI(一)——JNI的概念以及C語言開發工具dev-c++,編寫你的第一個C語言程序,使用C啟動JAVA程序
Android For JNI(一)——JNI的概念以及C語言開發工具dev-c++,編寫你的第一個C語言程序 當你的Android之旅一步步的深
學習Android之第五個小程序騰訊微博樣式(Listview的使用)
效果圖: public SimpleAdapter(Context context, List extends Map data, int resou
Android 開發:繪制多條線,帶緩存,帶觸控的圖表(基金圖表的實現)
對之前的幾篇文章裡的model進行補充後期會把這個功能類,添加到這個框架裡,有興趣的可以下載下來看,這個框架會經常更新:public class BaseFundChar