我这里说下我对苹果内购的理解,理解不正确,多指教。
对于我这种不懂苹果开发的程序员来说,用uni-app开发一个苹果APP,难点卡在了苹果内购,微信支付宝支付都是先下单,再付款,修改订单状态
而苹果内购有点不同,他的流程是先付款,再服务器二次验证,验证成功写入自己的数据库。
找了很多资料,后来hbuilder里的一个工作人员告诉我文档里有部分苹果内购代码,我试了下,可以跑起来。可是案例里uni.requestPayment()返回的只有状态,没有校验数据。就再次的看文档,5+文档。结合2个文档,后来通过5+实现了苹果内购,为了让其他的开放人员少走坑,这里把我测试成功的例子发布下。
<view>
<view class="uni-list">
<radio-group @change="applePriceChange">
<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in priceList" :key="index">
{{item.text}}
<radio :value="item.value" :checked="item.checked" />
</label>
</radio-group>
</view>
<view class="uni-padding-wrap">
<button class="ipaPayBtn" @click="requestPayment" :loading="loading" :disabled="disabled">确认支付</button>
</view>
</view>
</view>
let iapChannel = null,
productId = 'chongzhi_2019_1_14_168',
productIds = ['chongzhi_2019_1_14_168', 'chongzhi_2019_1_14_288','chongzhi_2019_1_14_588','chongzhi_2019_1_14_898'];
export default {
data() {
return {
loading: false,
disabled: true,
priceList: [{
value: 'chongzhi_2019_1_14_168',
text: '支付1元',
checked: true
}, {
value: 'chongzhi_2019_1_14_288',
text: '支付2元',
checked: false
},{
value: 'chongzhi_2019_1_14_588',
text: '支付3元',
checked: false
}, {
value: 'chongzhi_2019_1_14_898',
text: '支付4元',
checked: false
}]
}
},
onLoad: function() {
// 扩展API加载完毕,现在可以正常调用扩展API
var that = this;
plus.payment.getChannels(function(channels){
console.log(JSON.stringify(channels) );
for (var i in channels) {
var channel = channels[i];
if (channel.id === 'appleiap') {
iapChannel = channel;
//获取服务器产品
that.requestOrder();
}
}
if(!iapChannel){
console.log("不支持苹果内购");
}
}, false);
},
methods: {
//获取服务器产品
requestOrder() {
var that = this;
uni.showLoading({
title:'检测支付环境...'
});
iapChannel.requestOrder(productIds, (orderList) => { //必须调用此方法才能进行 iap 支付
that.disabled = false;
uni.hideLoading();
console.log('requestOrder success: ' + JSON.stringify(orderList));
}, (e) => {
that.disabled = false;
uni.hideLoading();
console.log('requestOrder failed: ' + JSON.stringify(e));
});
},
//点击支付
requestPayment(event) {
var that = this;
that.loading = true;
var statement = {
productid: productId
}
plus.payment.request(iapChannel, statement, function(e){
that.loading = false;
//这里的e包括了验证数据,可以发送到服务器端进行验证
console.log(JSON.stringify(e));
}, function(){
that.loading = false;
console.log("支付失败")
});
},
applePriceChange(e) {
productId = e.detail.value;
}
}
}
20 个评论
要回复文章请先登录或注册
7***@qq.com
1***@qq.com
2***@qq.com
9***@qq.com
风云杭州
自用仓库
自用仓库
1***@qq.com
90后菜鸟
Stalker丶