HBuilder 基座已實現H5 plus的支付API,現已集成支付寶快捷支付平台。使用支付功能前必須在支付寶網站開通”快捷支付“服務,並配置服務器生成訂單信息。
開發指導
支付流程如下:
plus API使用步驟:
1. 調用plus.payment.getChannels()獲取系統支持的支付通道;
2. 調用plus.payment.request()發起支付請求。
示例代碼
var channel=null;
// 1. 獲取支付通道
function plusReady(){
// 獲取支付通道
plus.payment.getChannels(function(channels){
channel=channels[0];
},function(e){
alert("獲取支付通道失敗:"+e.message);
});
}
document.addEventListener('plusready',plusReady,false);
var ALIPAYSERVER='http://demo.dcloud.net.cn/helloh5/payment/alipay.php?total=';
var WXPAYSERVER='http://demo.dcloud.net.cn/helloh5/payment/wxpay.php?total=';
// 2. 發起支付請求
function pay(id){
// 從服務器請求支付訂單
var PAYSERVER='';
if(id=='alipay'){
PAYSERVER=ALIPAYSERVER;
}else if(id=='wxpay'){
PAYSERVER=WXPAYSERVER;
}else{
plus.nativeUI.alert("不支持此支付通道!",null,"捐贈");
return;
}
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function(){
switch(xhr.readyState){
case 4:
if(xhr.status==200){
plus.payment.request(channel,xhr.responseText,function(result){
plus.nativeUI.alert("支付成功!",function(){
back();
});
},function(error){
plus.nativeUI.alert("支付失敗:" + error.code);
});
}else{
alert("獲取訂單信息失敗!");
}
break;
default:
break;
}
}
xhr.open('GET',PAYSERVER);
xhr.send();
}
注:第二步中獲取到的訂單數據需要在服務器通過支付寶平台獲取PID和密鑰生成。
HBuilder應用配置
分享插件添加方法具體步驟如下:
1. 點擊manifest.json文件的“代碼視圖”,在permissions節點下添加Payment節點:
- 在plus -> distribute -> plugins 節點下添加payment節點:

alipay節點下配置支付寶相關信息
scheme值為iOS平台調用支付寶的“快捷支付”應用返回時用到的標識,推薦使用小寫字符串。
weixin節點下配置微信支付相關信息
appid值為在微信開放平台申請應用的AppID值。
appleiap節點下配置蘋果應用內支付相關信息
在HBuilder基座中無法使用此支付通道,需要提交雲端打包才可使用。
支付寶功能申請
- 登錄支付寶賬號,簽約申請“移動快捷支付”功能,操作流程參考:
- 支付寶幫助中心 https://cshall.alipay.com/enterprise/index.htm
- 獲取PID,參考教程:
獲取合作者身份ID https://help.alipay.com/support/help_detail.htm?help_id=396880&keyword=%B2%E9%D1%AF - 生成密鑰(公鑰和私鑰),並提交到支付寶,參考教程:
生成RSA密鑰 https://help.alipay.com/support/help_detail.htm?help_id=397433&keyword=%C3%DC%D4%BF
上傳公鑰 https://help.alipay.com/support/help_detail.htm?help_id=477353&keyword=%C9%CC%BB%A7%B9%AB%D4%BF
服務器生成訂單示例(PHP)
參考開源示例代碼github-支付寶 https://github.com/dcloudio/H5P.Server/tree/master/payment/alipay
C#生成支付寶訂單示例 http://ask.dcloud.net.cn/article/197
微信支付功能申請
-
使用微信支付功能需到微信開放平台申請移動應用並開通支付功能
微信APP支付接入商戶服務中心 https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN
申請應用后可以獲取AppID和AppSecret值。 -
開通支付功能后可獲取支付業務服務器配置數據
PARTNER:財付通商戶號
PARTNER_KEY:財付通密鑰
PAYSIGNKEY:支付簽名密鑰
參考開源示例代碼github-微信支付 https://github.com/dcloudio/H5P.Server/tree/master/payment/wxpayv3
應用內支付(IAP)使用說明
IOS 應用內支付接口使用說明 http://ask.dcloud.net.cn/article/497
