【无标题】uniapp实现苹果内购功能

1.获取支付环境

// 获取支付通道
getChannels() {
    let that = this
    plus.payment.getChannels(res => {
    let channel = res.find(i => i.id === 'appleiap')
    that.iap = channel ? channel : null
    // 这一步是获取支付参数列表,也就是你苹果后台申请的商品列表 requestOrder
    that.requestOrder()
},  function(e) {
        plus.nativeUI.alert("获取支付通道失败,请稍后重试。", function() {}, "提示");
    })
},

2.获取内购项目列表

// 获取内购项目列表
requestOrder() {
    uni.showLoading({
        title: '支付环境检测中......'
    })
    this.vipRuleList = []
    this.iap.requestOrder(this.ids,res => {
        uni.hideLoading()
        this.vipRuleList = res
        this.btnFlag = true
        uni.hideLoading()
    },
    (errormsg) => {
        plus.nativeUI.alert("获取应用内购项目失败,请稍后重试。", function(e) {}, "提示");
            uni.hideLoading()
        }
    )
},

3.查询订单并且关闭订单 然后拉起支付

            /* ios内购 */
			requestPayment() {
				console.log(this.ids[this.index],"订单ID 商品ID");
				uni.showLoading({
					title: '支付中...'
				})
				let that = this
				//检测是否有未关闭订单 或者 未支付的订单 若有 关闭订单
				that.iap.restoreCompletedTransactions({
					manualFinishTransaction: true,
					username: ''
				}, (res) => {
					console.log(res,"未关闭的订单");
					if (res.length > 0) {
						that.iap.finishTransaction(res[0], (reslut) => {
							console.log(reslut, '关闭成功');
						})
					}
				}, (err) => {
					console.log(err);
				})
				uni.requestPayment({
					provider: 'appleiap',
					orderInfo: {
						productid: this.ids[this.index],
						manualFinishTransaction: false,
						// username:uni.getStorageSync('userinfo').user_id
					},
					success: (e) => {
						console.log(e, "支付结果返回");
						console.log('goods',this.vipRuleList[this.index]);
                        // 请求后台接口 并且完成支付后的逻辑
						// 后面的逻辑根据自己的需求来 我这就省略了

					},
					fail: (e) => {
						uni.hideLoading()
						console.log(e, "支付失败回调");
						uni.showModal({
							content: "支付失败",
							showCancel: false,
							success() {
								uni.navigateBack()
							}
						})
					},
					complete: () => {
						uni.hideLoading()
						this.loading = false;
					}
				})
			},

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值