let iapChannel = null,
productId = 'Recharge_1',//默认苹果内购商品ID
productIds = ['Recharge_1', 'Recharge_2', 'Recharge_3','Recharge_4', 'Recharge_5', 'Recharge_6', 'Recharge_7'];//苹果内购商品
export default {
data(){
return{
}
},
onLoad: function() {
//苹果支付的流程
//1.首先调用plus.payment.getChannels获取支付通道,IAP支付通道的ID为“appleiap”
//2.调用ID为“appleiap”的PaymentChannel对象的requestOrder方法,像Appstore请求有效的商品详情。注意:IAP支付必须在调用payment.request方法之前,调用requestOrder方法,否则调用payment.request将会报错。
//3.调用plus.payment.request方法发起支付请求,传入statement的参数为JSON对象,
//可以设置如下参数
//productid String(必选)要支付的商品的标识(必须是调用requestOrder返回的有效的商品标识)
//username String(可选)购买商品用户的用户名
//quantity String (可选)购买商品的数量,如果不填写默认为1
//1.获取支付渠道
plus.payment.getChannels((channels) => {
for (var i in channels) {
var channel = channels[i];
//苹果支付
if (channel.id === 'appleiap') {
iapChannel = channel;
this.requestOrder();//关键
}
}
if(!iapChannel){
that.errorMsg()
}
}, (error) => {
that.errorMsg()
});
},
computed: {
},
mounted(){
},
methods:{
requestOrder() {
uni.showLoading({
title:'加载中...'
})
iapChannel.requestOrder(productIds, (orderList) => { //必须调用此方法才能进行 iap 支付
uni.hideLoading();
}, (e) => {
uni.hideLoading();
this.errorMsg()
});
},
setUserRechargeCoin(num,productId){
//用户是已登录状态
if(!this.user.id){
}else{
let that = this;
uni.showLoading({
//title: '处理中,...'
});
uni.requestPayment({
provider: 'appleiap',
orderInfo: {
productid: productId,
username: that.user.id
},
success: (e) => {
//e:返回的参数
let params={
orderInfo: JSON.stringify(e),
payFor: 2 //1:开通会员 2:充值 3:订阅 4:赠送
}
//1.未了防止掉单,建议先订单数据保存到本地
//2.请求客户服务器的业务接口,成功后删除,该本地数据
applePayNotify(params).then(res=>{
uni.hideLoading();
if(res.code == 0){
//2.删除保存在本地订单数据
uni.showModal({
content: "充值成功",
showCancel: false
})
}
})
},
fail: (e) => {
uni.showModal({
content: "充值失败,请重试",
showCancel: false
})
},
complete: () => {
uni.hideLoading();
}
})
}
}
}
}
苹果返回的参数例子:
苹果支付订单信息:{"payment":{"productid":"Recharge_CNY1","quantity":"1","username":"80204"},"transactionDate":"2020-03-14 13:40:22","transactionIdentifier":"1000000639069984","transactionReceipt":"ewoJInNpZ25hdHVyZSIgPSAiQTVxVDhCRHJWM3ZrcEY3TzBEZ0gwUXNvV2FPRHNKOVZrZW1uYjJtVFhaOXhkQnI2M1h4c0FydGFhS0lpR01Mc3M1T3NYNk9NK2taUGUxK21oQTVPQzVuOEFBMk9hNE9GVnlKZHB3V244RUNZQkRoL24zcFBFWVo0MFUvN1RrMkcvb3RUYk1FT01DRmpubVlzN041SldkbE5RdlYvTUFkeXc5RTAwd1AxVmdmaytLTFM5cll6Snl4cUpIeUFTWWwzeERJbS9nZ0xFYjI3ajBZanJWUGMvM3kvQjVuWThSdDRJMSsrZUdpWFJ5OEZackRsaC9LTlNueDU5RFNmakRiM1pXeXN5SlBHVU80ck05VHFqdmNSV2FWU2VGVXNzUHExaUc2OTRvdVZ0UWtIWGt4czdVTnlNVThod1dZbitTOTFDM3BBRnRrRU9KeExTVGprMnprT1J1MEFBQVdBTUlJRmZEQ0NCR1NnQXdJQkFnSUlEdXRYaCtlZUNZMHdEUVlKS29aSWh2Y05BUUVGQlFBd2daWXhDekFKQmdOVkJBWVRBbFZUTVJNd0VRWURWUVFLREFwQmNIQnNaU0JKYm1NdU1Td3dLZ1lEVlFRTERDTkJjSEJzWlNCWGIzSnNaSGRwWkdVZ1JHVjJaV3h2Y0dWeUlGSmxiR0YwYVc5dWN6RkVNRUlHQTFVRUF3dzdRWEJ3YkdVZ1YyOXliR1IzYVdSbElFUmxkbVZzYjNCbGNpQlNaV3hoZEdsdmJuTWdRMlZ5ZEdsbWFXTmhkR2x2YmlCQmRYUm9iM0pwZEhrd0hoY05NVFV4TVRFek1ESXhOVEE1V2hjTk1qTXdNakEzTWpFME9EUTNXakNCaVRFM01EVUdBMVV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REE2TWpJZ1JYUmpMMGROVkNJN0Nna2lhWE10ZEhKcFlXd3RjR1Z5YVc5a0lpQTlJQ0ptWVd4elpTSTdDZ2tpYjNKcFoybHVZV3d0Y0hWeVkyaGhjMlV0WkdGMFpTSWdQU0FpTWpBeU1DMHdNeTB4TkNBd05UbzBNRG95TWlCRmRHTXZSMDFVSWpzS0NTSmlhV1FpSUQwZ0ltTnZiUzU2YUc5dVoyOTFMbXBwYUdVaU93b0pJbkIxY21Ob1lYTmxMV1JoZEdVdGNITjBJaUE5SUNJeU1ESXdMVEF6TFRFeklESXlPalF3T2pJeUlFRnRaWEpwWTJFdlRHOXpYMEZ1WjJWc1pYTWlPd3A5IjsKCSJlbnZpcm9ubWVudCIgPSAiU2FuZGJveCI7CgkicG9kIiA9ICIxMDAiOwoJInNpZ25pbmctc3RhdHVzIiA9ICIwIjsKfQ==","transactionState":"1","errMsg":"requestPayment:ok"}
{"payment":{"productid":"Recharge_CNY6","quantity":"1",username:{"contentId":22,"subscribeMode":1,"price":100,"discountPrice":10,"amount":60}},"transactionDate":"2020-03-15 10:27:48","transactionIdentifier":"1000000639183629","transactionReceipt":"ewoJInNpZ25hdHVyZSIgPSAiQTByZkNHc3R5OU1CcGtvMThMRmwxTWxMVy8zT3Q1R2FHcWErTis1TFc4NEtGczNBNEtXSWUxM3RET3JWS0pJMFZIcEVXdkRpcFZMOEprb0kwY2VlWFBmbFhVOEpqd3lPSFZyTHZORHQwZHNvRmZTUWZwTHlZc0NCWHpDU01jZkdUNmxjM01kWGRJSFJ4b0UzOG90V1FNRXcxTGpJUUd4SnUwL2lIR0x6ZHcrY0tyM0FTYU9GR1Q4TUg3RURwOWZBWlNKOUF4dU1XYlJHZk5NUlJRV0QwcHJ6alhFT3lUYlY2Z2EvOGRsZHlHT0M4OW5MT25uNmlCRnl5ZDRzNHA2eWpuVWpNOGF0NjNMYkR4aWdTQXViZGFHVnpPYWd0SkdKNk9FTldOWVh4VExUOWNOLytWalU0YVpEcDFPWHcw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