需要做一下处理
如果是本地打包,需要在xcode配置,liblib..两个文件,feat??文件
需要在uni中manifest.json打开内购开关
需要在app store 中配置商品信息,并且需要添加银行卡等
案例:(可能会出现丢单,需要特别处理)
<template>
<view>
<view class="pay-list">
<view class="row" @click="handlePayment(index,row._id,row.price)" v-for="(row,index) in allPriceInfo" :key="index">
<view class="name">{{row.goods_name}}</view>
<view class="price">¥{{row.price}}</view>
</view>
</view>
<view class="subPay" @click="subPaynow">立即充值</view>
</view>
<template>
<script>
export default {
data() {
return {
orderId: null,
allPriceInfo: [{
"_id": '1001', //这个商品的id 一定要跟在app store connect配置的ID相同
"goods_name": "商品2",
"price": "12",
},
{
"_id": '1002',
"goods_name": "商品2",
"price": "12",
},
],
iapChannel: null,
productId: '1001', //默认苹果内购商品id 要提前在app store connect 配置 要支付的商品的标识(必须是调用uni.requestPayment返回的有效的商品标识)
productIds = ['1001', '1002', '1003', '1004', '1005', '1006']; //商品的id 列表,拿后端返回的id(需要和app store中所配置的id一致)
}
},
onLoad() {
// #ifdef APP-PLUS
//1.首先调用plus.payment.getChannels获取支付通道,IAP支付通道的ID为“appleiap”
plus.payment.getChannels((channels) => {
console.log("获取到channel" + JSON.stringify(channels))
this.iapChannel = channels.find(i => i.id === 'appleiap')
console.log("获取到iapChannel" + JSON.stringify(iapChannel))
if (this.iapChannel) {
this.requestOrder();
}
if (!this.iapChannel) {
this.errorMsg()
}
}, (error) => {
this.errorMsg()
});
},
methods: {
//苹果内支付
//2.调用ID为“appleiap”的PaymentChannel对象的requestOrder方法,像Appstore请求有效的商品详情。注意:IAP支付必须在调用payment.request方法之前,调用requestOrder方法,否则调用payment.request将会报错。
requestOrder() {
uni.showLoading({
title: '检测支付环境...'
})
iapChannel.requestOrder(this.productIds, (orderList) => { //必须调用此方法才能进行 iap 支付
console.log('requestOrder success666: ' + JSON.stringify(orderList));
uni.hideLoading();
}, (e) => {
console.log('requestOrder failed: ' + JSON.stringify(e));
uni.hideLoading();
this.errorMsg()
});
},
//选中价格的时候
handlePayment(index, id, price) {
this.orderId = id
},
//点击立即支付
//3.调用uni.requestPayment方法发起支付请求,
subPaynow(e) {
let _this = this
uni.showLoading({
title: '支付中...'
})
this.productId = _this.orderId
uni.requestPayment({
provider: 'appleiap',
orderInfo: {
productid: productId, //必填
quantity: 1, //选择的数量 可不填
username: '' //支付商户的名称 可不填
},
success: (e) => {
console.log('支付成功====', e)
// 支付成功去验单
uni.hideLoading()
},
fail: (e) => {
uni.hideLoading()
console.log('支付失败,原因为===', e.errMsg)
},
complete: () => {
uni.hideLoading()
console.log("payment结束")
},
})
},
errorMsg() {
uni.showModal({
content: "暂不支持苹果支付",
showCancel: false
})
},
}
}
</script>
暂时这样以后更新...