uniapp 苹果支付流程
1.配置文件中勾中Apple应用内支付
2.调用 plus.payment.getChannels 来获取支付通道
iphonepay() {const that = thisuni.showLoading({title: '检测支付环境...',mask: true})plus.payment.getChannels((channels) => {for (var i in channels) {// 判断是否苹果支付if (channels[i].id === 'appleiap') {that.iapChannel = channels[i]that.requestOrder()}}})}
3.调用 上一步拿到对象的requestOrder方法 该方法需要传入三个参数 requestOrder(‘苹果内购商品id’,成功回调,失败回调) 拿到订单数据 拿到订单数据的商品id
requestOrder() {const that = this// ['xxxxx'] 是平台申请拿到的内购商品的id that.iapChannel.requestOrder(['xxxxxx','xxxxxx'], function(event) {uni.hideLoading()console.log(event)for (var index in event) {var OrderItem = event[index]console.log(OrderItem)that.topay(OrderItem.productid)}
}, function(erroemsg) {uni.hideLoading()uni.showToast({title: "获取支付通道失败:" + errormsg.message,icon: 'none'})})}
4.最后调用uni.requestPayment 拿到后端接口需要的交易id 校验体
- 使用uni.requestPayment时需要传入的orderInfo 是Object对象类型
topay(id) {const that = thisuni.showLoading({title: '充值中请勿离开',mask: true})uni.requestPayment({provider: 'appleiap',orderInfo: {productid: id},success: (res => {uni.hideLoading()const orderId = this.orderId //这个订单id 是由后端返回的const transactionId = res.transactionIdentifier //交易idconst payload = res.transactionReceipt //校验体upayfor({transactionId,payload,orderId}).then(res => {console.log(res)uni.showToast({title: '充值成功'})that.getUserInfo()})}),fail: (e) => {uni.hideLoading()uni.showModal({content: "支付失败",showCancel: false})}
})
官方文档 https://uniapp.dcloud.io/api/plugins/payment?id=orderinfo
第一次开发这个功能,有地方还理解不够,记录一下。整个过程中有遇到-100的问题 卡了半天 结果是沙盒测试申请的东西没有补充完整
感谢原文博主 :https://blog.csdn.net/wangwangli6/article/details/104892562/