苹果支付需要在苹果后台设置沙盒模拟账号,在手机上登录沙盒账号
在utils下创建js文件
//苹果支付
apple(payid, orderid , type) {
/* 1苹果支付码,字符串
2订单id后面抛回去 苹果支付不能获取支付结果,要在支付完调接口修改订单状态
3类型,主要给自己判断在哪里支付的
*/
let that = this
plus.payment.getChannels((channels) => {
// console.log(channels);
for (var i in channels) {
var channel = channels[i];
if (channel.id == 'appleiap') {
// console.log("苹果")
let iap = channel;
//支付参数
iap.requestOrder([payid], function(orderList) {
//必须调用此方法才能进行 iap 支付
console.log('requestOrder success666: ' + JSON.stringify(orderList))
that.applePay(orderList[0] , orderid , type)
}, function(e) {
// console.log('requestOrder failed: ' + JSON.stringify(e));
// console.log('暂不支持苹果 iap 支付')
});
}
}
}, function(e) {
console.log("获取iap支付通道失败:" + e.message);
});
},
applePay(para , orderid , type) {
setTimeout(()=>{
uni.hideToast()
},1000)
console.log(para)
uni.requestPayment({
provider: 'appleiap',
orderInfo: {
productid: para.productid,
username: orderid, // 用户标识
optimize: true // 设置 optimize: true 解决丢单问题
},
/*
orderInfo可传参数一览
productid: (String 类型 )(必填) 商品的标识,你在苹果那里添加的商品标识
username: (String 类型 )(可选) 购买用户名称,我喜欢在这里传入订单号,这样支付成功时就可以获得订单号
quantity: (String 类型 )(可选) 商品数量,默认为 "1"
optimize:(Boolean)(可选) HX 3.1.10 版本新增参数,解决用户未绑定支付方式造成的丢单问题;
*/
success: (es) => {
console.log(es)
if(type && type == 'wxCode'){
//参数调接口改变订单状态
uni.$emit('paymentSuccess', {
TransactionID: es.transactionIdentifier,
Payload: es.transactionReceipt,
orderId: orderid,
tips: '微信号'
})
}else if(type && type == 'vipPayment'){
uni.$emit('paymentSuccess', {
TransactionID: es.transactionIdentifier,
Payload: es.transactionReceipt,
orderId: orderid,
tips: '置顶'
})
}else {
uni.$emit('paymentSuccess', {
TransactionID: es.transactionIdentifier,
Payload: es.transactionReceipt,
orderId: orderid,
})
}
},
fail: (err) => {
console.log(err);
com.msg('取消支付')
},
complete: () => {
// console.log("payment结束");
}
})
},
在页面使用
// 1. 引入支付js文件
import payment from "@/utils/payment.js";
// 2. onLoad里 获取苹果支付回调,调接口
uni.$off('paymentSuccess').$on('paymentSuccess', function(res) {
uni.showToast({
icon: 'loading',
duration: 3000
})
applePaycontactTeam(res).then(pay => {
uni.hideToast()
this.getdata() //重调渲染页面
})
}.bind(this))
// 3. 接口内调苹果支付 很慢,加一个加载效果
uni.showToast({
title: '正在连接App Store,请稍等',
icon: 'loading',
duration: 10000
})
payment.apple(this.payPrice.encoding, this.orderId)