uni-app 苹果内购支付及获取苹果支付成功信息

先在头部定义商品id 全部商品id (在苹果商店申请的真实商品id)

let iapChannel = null;
var productId = 'lvbi_a';
var productIds = ['lvbi_a'];

进入页面就判断

//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()
            });
requestOrder() {
    iapChannel.requestOrder(productIds,     
              (orderList) => { //必须调用此方法才能进行 iap 支付
        uni.hideLoading();
    }, (e) => {
        uni.hideLoading();
            this.errorMsg()
    });
},        

调接口获取真实的商品id

                        // 获取页面内容
            getAppleProducts() {
                AppleProducts({
                    openid:this.openid,
                    token:this.tpken
                }).then(res =>{
                    if(res.code == 1){
                        this.data = res.data;
                        this.price = this.data.products[0];
                        productId = 
                                                this.data.products[0].product_id;
                        productIds =     
                                                this.data.products_ids;
                    }
                })
            },    

确定支付

topupprice() {
                let that = this;
                uni.showLoading({});
                uni.requestPayment({
                    provider: 'appleiap',
                    orderInfo: {
                        productid: productId
                    },
                    success: (e) => {
                        uni.hideLoading();
                         var  statement = {  
                                productid: productId  
                            }  
                            // 支付成功查询订单
                            plus.payment.request(iapChannel, statement, function(res){
                                paysuccers({
                                    data:{
                                        openid:that.openid,
                                        token:that.token,
                                        receipt_data:JSON.stringify(res)
                                    }
                                }).then(tow =>{
                                    uni.hideLoading();
                                    if(tow.code == 1){
                                        uni.showToast({
                                            title:tow.msg,
                                            icon:'none'
                                        })
                                    }else{
                                        uni.showToast({
                                            title:tow.msg,
                                            icon:'none'
                                        })
                                    }
                                })
                            }, function(){  
                                
                            });
                        
                    },
                    fail: (e) => {
                        uni.hideLoading();
                        uni.showModal({
                            content: "支付失败,原因为: " + e.errMsg,
                            showCancel: false
                        })
                    }
                })
            },

 

posted @ 2021-07-12 10:58  我有头盔  阅读(2260)  评论(0编辑  收藏  举报