【无标题】uni-app ios 内购详细流程(开箱即用)

ios 内购详情解决 二次支付 免费提示等问题

	onShow(){
		//#ifdef APP-PLUS
			let platform = uni.getSystemInfoSync().platform
			//判断在ios 情况下 调用 当前方法
			if (platform == 'ios') {
				iosVip().then((res) => { // 获取 ios 套餐 
					this.platform = 'ios'
					this.vipList = res.data.rows
					res.data.rows.map((item) => {
						// data 定义变量 productIds:[] 获取 所有ios 内购 标识 
						this.productIds.push(item.productDesc)
					})
				}).then(() => {
					uni.showLoading({
						title: "正在检测支付环境……" ,
					})
					// 检测 支付环境 
					this.iphonepay()
				})
			} 
			//#endif
	},
	methods:{
		iphonepay() {
			let that = this
			plus.payment.getChannels((res) => {
				let channel = res.find(i => i.id === 'appleiap')
				that.iapChannel = channel ? channel : null
				console.log(channel, 'channel');
				that.requestOrder()
			})
		},
		//获取支付通道 使用所有的产品列表去检测 that.productIds 这个自己定义获取
		requestOrder() {
			let that = this
			that.iapChannel.requestProduct(that.productIds, function(event) {
				console.log(event, '检测完成');
				uni.hideLoading()
			}, function(erroemsg) {
				uni.hideLoading()
				uni.showToast({
					title: "获取支付通道失败:" + errormsg.message,
					icon: 'none'
				})
			})
		},
		// 支付 调用 重点 
		iosPay(){
			let that = this
			//检测是否有未关闭订单 或者 未支付的订单 若有 关闭订单
			that.iapChannel.restoreCompletedTransactions({
					manualFinishTransaction: true,
					username: ''
				}, (res) => {
					if (res.length > 0) {
						that.iapChannel.finishTransaction(res[0], (reslut) => {
							console.log(reslut, 'reslut');
						})
					}
				}, (err) => {
					console.log(err);
				})
			},
			//orderInfo 看你自己 重点是productid 我这边就不展示我调用的接口了
			let orderInfo = {
				manualFinishTransaction: false, //true为手动关闭订单,false为自动关闭订单
				productid: result.data.productDesc, //档位id
			}
			uni.showLoading({
				title: '充值中请勿离开',
				// mask: true
			})

			uni.requestPayment({
				provider: 'appleiap',
				orderInfo: orderInfo,
				success: (e) => {
				let code = JSON.parse(JSON.stringify(e.payment.quantity))
					if (code == 1) { // 支付成功
						console.log(obj, 'result');
						iosPayNotify(obj.data.orderNo).then((key) => { // 成功支付将数据返回给后台
							if (key.code == 200) {
								userInfo().then((reslut) => {// 更新用户数据
									uni.setStorage({
										key: 'userInfo',
										data: reslut.data,
									});
									uni.hideLoading();
								})
							}
						})
						// 因为 ios 支付 是调用的外网可能是丢单什么的 不关闭订单 需要自己 在支付成功的时候收到去关闭一下
						that.iapChannel.finishTransaction(e, (res) => {
								console.log('成功关闭');
							})
						}
					},
				fail: (err) => {
					uni.hideLoading();
						console.log(err, 'err');
				},
			})
		

	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值