IPA苹果支付使用说明
1、您需要先在 iTunes Connet 应用后台,创建好 App 内购买项目,设置好税务和银行卡等配置信息,具体操作请自行查询文档教程;推荐阅读 App 内购买项目配置流程 2、HBuilderX 内置的调试基座默认不带IAP支付通道,如果需要调试 IAP 需要创建自定义调试基座,需要在 manifest->App模块配置->Payment(支付)->勾选 Apple应用内支付
下面对应用内支付的使用方法进行说明,文中API的参数及使用实例请参考 plus.payment
苹果支付接口使用方法
1、首先调用 plus.payment.getChannels 获取支付通道,IAP 支付通道的 ID为"appleiap"
2、调用ID为"appleiap"的PaymentChannel 对象的 requestOrder 方法,向 AppStore 请求有效的商品详情,注意:调用 plus.payment.request 前必须先调用此接口
3、调用 plus.payment.request 方法发起支付请求, OrderStatementIAP 的参数为JSON对象,参数说明
- productid: (String 类型 )(必填) 商品的标识
- username: (String 类型 )(可选) 购买用户名称
- quantity: (String 类型 )(可选) 商品数量,默认为 “1”
- optimize:(Boolean)(可选) HX 3.1.10 版本新增参数,解决用户未绑定支付方式造成的丢单问题;
4、调用 restoreComplateRequest 恢复购买接口,此方法的作用说明
- 用于向 App Store发送请求获取已经购买商品(非消耗型项目和订阅项目)的支付信息;
- 获取之前某些原因造成的丢单的商品支付信息,注意:调用支付接口时需要设置 optimize:true;(比如,支付过程App意外退出,用户首次支付未绑定任何支付方式时造成的丢单情况)
示例
1、获取appleiap支付通道
getChannels(){
plus.payment.getChannels(function(channels) {
for (var i in channels) {
var channel = channels[i];
// 获取 id 为 'appleiap' 的 channel
if (channel.id === 'appleiap') {
this.iap = channel;
}
}
}, function(e) {
console.log("获取iap支付通道失败:" + e.message);
});
}
2、获取订单信息
// ids 为在苹果开发者后台配置的应用内购项目的标识集合
requestPaymentIos(id){
var ids = ['商品1', '商品2'];
// iap 为刚刚获取的`appleiap`支付通道
this.iap.requestOrder(ids, function(e) {
// 获取订单信息成功回调方法
console.log('requestOrder success: ' + JSON.stringify(e));
ids.map(item=>{
//这里的id是 iTunes Connet 应用后台创建App 内购买项目的id
if(item.productid == id){
var orderInfo = {
productid: item.productid, // 商品id
quantity: 1, // 数量,默认1
username: 'appusername', // 用户标识
optimize: true, // 设置 optimize: true 解决丢单问题
}
// 接下来就是调用plus.payment.request支付
this.iosPayment(orderInfo)
}
})
}, function(e) {
// 获取订单信息失败回调方法
console.log('requestOrder failed: ' + JSON.stringify(e));
});
}
3、支付
iosPayment(orderInfo){
// restoreFlag 标记,用于判断在页面显示的时候是否需要调用 restoreComplateRequest 方法
var restoreFlag = true;
// 调用支付接口时标记 restoreFlag = true , 实际应用请将标记存储在 storage 中
// 开始支付
plus.payment.request(this.iap, orderInfo, function(result) {
console.log("支付成功----", result)
restoreFlag = false;
//调用后端接口验证支付接口
},
function(e) {
console.log("支付失败----", e);
// 支付失败的时候需要调用一下 restoreComplateRequest 方法
this.restoreComplateRequest()
});
}
4、验证支付结果
verifyIosPay(result) {
const params = {
productId: result.payment.productid,
quantity: result.payment.quantity,
transactionDate: result.transactionDate,
transactionIdentifier: result.transactionIdentifier,
transactionReceipt: result.transactionReceipt,
orderId: 'orderId'
}
console.log('params---------', params)
payIosVerifyApi(params).then(res=>{
console.log('res', res)
if (res.code == 20000) {
this.$u.toast('支付成功')
} else {
this.$u.toast(res.message)
}
})
},
4、恢复购买方法(获取已购的非消耗性商品和订阅商品及丢单的商品包括消耗类型商品)
注意:如果用户在支付过程中退出了App,或其他情况造成的丢单的消耗类型商品在支付完成后,首次调用该接口可返回支付凭证
restoreComplateRequest() {
iap.restoreComplateRequest({}, function(results) {
this.requestPaymentIos(results[0].payment.productid)
// results 格式为数组存放恢复的IAP商品交易信息对象 IAPTransaction,需要将返回的支付凭证传给后端进行二次认证
});
}
5、在支付页面每次显示的时候判断是否需要调用 restoreComplateRequest()方法,确认是否有未完成或可恢复的订单
// 1. 5+app 在 resume 回调中调用 restoreComplateRequest
document.addEventListener('resume',function(){
if(restoreFlag) {
restoreComplateRequest()
}
},false);
// 2.uniapp 在页面 onShow 方法中调用 restoreComplateRequest
onShow() {
if(restoreFlag) {
restoreComplateRequest()
}
}
丢单恢复的说明
通过和用户联调我们发现在调用支付接口后,如果用户未绑定支付方式此时会触发支付失败回调方法,实际上用户可以跳转 AppStrore 绑卡然后继续支付,之前的逻辑在回调失败方法中框架会关闭订单,用户付完钱在回到App中也不会触发成功回调,这样就造成了丢单,解决方法就是在调用支付接口时添加optimize: true参数,并标记 restoreFlag = true;,支付成功回调中清除标记 restoreFlag = false; 然后在支付失败回调中框架就不会关闭订单了,并在页面显示的时候通过标记判断是否需要调用 restoreComplateRequest 方法,如果用户跳转App Store绑定支付方式付款成功后回到 App 就可以通过 restoreComplateRequest 方法恢复之前支付的订单信息,解决丢单的问题;