若app有涉及虚拟产品的支付,必须使用苹果应用内支付,不允许使用微信支付宝等三方支付

import {
        Iap,
        IapTransactionState
      } from "@/utils/iap.js"
    const platform = uni.getSystemInfoSync().platform
    export default{
        data(){
            return{
                money:0,
                productId:'com.test.xxx.prod01',
                productList:[],
                appleSwitch:1
            }
        },
        async onLoad() {
            const pidList = await this.getProduct();
            console.log(pidList)
            if(platform=='ios' && this.appleSwitch){
                this._iap = new Iap({
                    products: pidList 
                })

                this.init();
            }

        },
        onShow() {
            if(platform=='ios' && this.appleSwitch){
                if (this._iap.ready) {
                    this.restore();
                }
            }

        },
        watch:{
            productList: {
                handler(newVal, oldVal) {
                    if (newVal.length>0) {
                        uni.hideLoading()
                    }
                },
                immediate: true
            },
        },
        methods:{
            getProduct(){
                return new Promise((resolve,reject)=>{
                    getProductid().then(res=>{
                        let prod = res.data.data.prod
                        this.amountList = prod
                        this.appleSwitch =  parseInt(res.data.data.appleSwitch)
                        const pidList = []
                        prod.forEach((item,index)=>{
                            pidList.push(item.pid)
                        })
                        resolve(pidList)
                    })
                })

            },
            async init() {
                uni.showLoading({
                    title: '支付环境准备...'
                });

                try {
                    // 初始化,获取iap支付通道
                    await this._iap.init();

                    // 从苹果服务器获取产品列表
                    this.productList = await this._iap.getProduct();
                    console.log(this.productList)
                    // uni.$u.toast('ios支付环境ok')
                    // setTimeout(()=>{
                    //  uni.hideLoading();
                    // },6000)
                    // 填充产品列表,启用界面
                    // this.disabled = false;
                } catch (e) {
                    console.log(e,1231232)
                    // uni.$u.toast(e.errMsg)
                    uni.showModal({
                        title: "init",
                        content: e.errMsg,
                        showCancel: false
                    });
                } finally {
                    uni.hideLoading();  
                }

                if (this._iap.ready) {
                    this.restore();
                }
            },
            async restore() {

                try {
                  // 从苹果服务器检查未关闭的订单,可选根据 username 过滤,和调用支付时透传的值一致
                  const transaction = await this._iap.restoreCompletedTransactions({
                    username: ""
                  });

                  if (!transaction.length) {
                    return;
                  }

                  switch (transaction.transactionState) {
                    case IapTransactionState.purchased:
                      // 用户已付款,在此处请求开发者服务器,在服务器端请求苹果服务器验证票据
                      let result = await this.validateApplePayResult({
                        transactionReceipt: transaction.transactionReceipt, // 不可作为订单唯一标识
                      });

                      // 验证通过,交易结束,关闭订单
                      if (result.code==201) {
                        await this._iap.finishTransaction(transaction);
                      }
                      break;
                    case IapTransactionState.failed:
                      // 关闭未支付的订单
                      await this._iap.finishTransaction(transaction);
                      break;
                    default:
                      break;
                  }
                } catch (e) {
                  uni.showModal({
                    content: e.message,
                    showCancel: false
                  });
                } finally {
                  uni.hideLoading();
                }
            },
            async toApplePay(){
                if(this.money==''){
                    return this.$u.toast('请选择充值金额');
                }
                console.log(this.productId)

                uni.showLoading({
                    title: '支付处理中...'
                });

                try {
                  // 从开发者服务器创建订单
                  const orderId = await this.createOrder();

                  // 请求苹果支付
                  const transaction = await this._iap.requestPayment({
                    productid: this.productId,
                    manualFinishTransaction: true,
                    username: orderId //根据业务需求透传参数,关联用户和订单关系
                  });

                  // 在此处请求开发者服务器,在服务器端请求苹果服务器验证票据
                  const payresult = await this.validateApplePayResult({
                    orderId: orderId,
                    transactionReceipt: transaction.transactionReceipt,
                  });

                  if(payresult.code==200){
                      uni.hideLoading()
                      uni.$u.toast("支付成功!")

                      // 验证成功后关闭订单
                      await this._iap.finishTransaction(transaction);
                      setTimeout(()=>{
                          uni.navigateBack()
                      },2000)
                  }else{
                      uni.hideLoading()
                      uni.$u.toast("支付失败!")
                  }

                  // 支付成功
                } catch (e) {
                    console.log(e,'apple支付结果')
                  uni.showModal({
                    content: e.message,
                    showCancel: false
                  });
                } finally {
                  uni.hideLoading();
                }
            },
            async createOrder(){
                return new Promise((resolve,reject)=>{
                    setRechargeOrd({
                        pay_type:3,
                        money:this.money
                    }).then(res=>{
                        resolve(res.data.data)
                    })
                })
            },
            async validateApplePayResult(param){
                return new Promise((resolve,reject)=>{
                    checkReceipt(param).then(res=>{
                        console.log('校验票据',res)
                        resolve(res.data.data)
                    })
                })

            },

        }
    }