let applePayMethods = {
    data() {
        return {
            iap: {},
        }
    },
    onShow() {
        if (uni.getStorageSync('applePayParam')) {
            uni.showLoading({
                mask:true,
                message:'验证交易信息中...'
            })
            this.verifyPayment()
        }
    },
    onLoad() {
        this.getChannels()
    },
    methods: {
        getChannels() {
            plus.payment.getChannels((channels) => {
                for (var i in channels) {
                    var channel = channels[i];
                    if (channel.id === 'appleiap') {
                        this.iap = channel
                        return
                    }
                }
            }, function(e) {
                uni.showModal({
                    content: "获取iap支付通道失败:" + e.message
                })
            });
        },
        applePay(productid, OrderNum, opType) {
            uni.showLoading({
                mask:true,
                message:'请求中...'
            })
            this.iap.requestOrder([productid], (e) => {
                plus.payment.request(this.iap, {
                    productid,
                    quantity: 1,
                    username: this.vuex_user.id+'|'+productid+'|'+(new Date().valueOf()),
                    manualFinishTransaction: true,
                }, (result) => {
                    let applePayParam = {
                        OrderNum,
                        receiptData: result.transactionReceipt,
                        transactionID: result.transactionIdentifier,
                        type: 'test',
                        opType, //1、广告推广 2、开通vip 3、发布企业信息
                    }
                    uni.setStorageSync('applePayParam',applePayParam)
                    uni.setStorageSync('finishTransaction',result)
                    this.verifyPayment()
                }, (e) => {
                    console.log('err', e)
                    uni.showModal({
                        content:'取消交易'
                    })
                    uni.removeStorageSync('applePayParam')
                    uni.removeStorageSync('finishTransaction')
                    uni.hideLoading()
                });
            }, function(e) {
                console.log('err', e)
                uni.showModal({
                    content:'取消交易'
                })
                uni.hideLoading()
            });
        },
        verifyPayment(){
            console.log(uni.getStorageSync('applePayParam'),'verifyPayment->applePayParam');
            this.$u.api.applePay(uni.getStorageSync('applePayParam')).then(res => {
                this.iap.finishTransaction(uni.getStorageSync('finishTransaction'), () => {
                    uni.hideLoading()
                    uni.removeStorageSync('applePayParam')
                    this.$u.toast('支付成功!')
                }, (e) => {
                    uni.hideLoading()
                    uni.showModal({
                        content: '关闭订单交易失败:' + JSON.stringify(e)
                    })
                })
            })
        }
    }
}

 

 posted on 2022-10-20 16:06  laremehpe  阅读(898)  评论(0编辑  收藏  举报