uniapp对接苹果内购

前端对接之前要确保客户已经在苹果后台(https://appstoreconnect.apple.com/)完善了协议(如下图),这两个协议的状态必须为 有效!!!!!!!否则调用iap.requestOrder获取订单时就会一直返回订单信息错误;还需要添加并完善虚拟商品,以及添加沙盒测试人员。这是写代码之前的所有准备工作

 判断是否是苹果系统,点击支付的时候调用initIAP(id)函数即可

形参id说明:此id为苹果后台虚拟商品的id,通过后端接口获取

        initIAP(id) {
				let that = this
				plus.payment.getChannels((channels) => {
					for (var i in channels) {
						var channel = channels[i];
						if (channel.id == 'appleiap') {
							let iap = channel;
							//支付参数
							console.log(id, "商品id");
							iap.requestOrder([id], function(orderList) {
								//必须调用此方法才能进行 iap 支付 
								console.log('requestOrder success666: ' + JSON.stringify(orderList))
								that.applePay(orderList[0])
							}, function(e) {
								console.log('requestOrder failed: ' + JSON.stringify(e));
								// console.log('暂不支持苹果 iap 支付')
							});
						}
					}
				}, function(e) {
					console.log("获取iap支付通道失败:" + e.message);
				});
			},
applePay(para) {
				let that = this
				console.log(para)
				uni.requestPayment({
					provider: 'appleiap',
					orderInfo: {
						productid: para.productid,
						username: 'appusername', // 用户标识
						optimize: true // 设置 optimize: true 解决丢单问题  
					},
					/* 
					orderInfo可传参数一览
					productid: (String 类型 )(必填) 商品的标识,你在苹果那里添加的商品标识 
					username: (String 类型 )(可选) 购买用户名称,我喜欢在这里传入订单号,这样支付成功时就可以获得订单号 
					quantity: (String 类型 )(可选) 商品数量,默认为 "1" 
					optimize:(Boolean)(可选) HX 3.1.10 版本新增参数,解决用户未绑定支付方式造成的丢单问题; 
					*/
					success: (e) => {
						// uni.LTY.loaded();
						//这里把参数返回服务器进行二次验证 
						//这里再次验证是否订单真实有效
						const params = {
							info: e.transactionReceipt,
							order_id: that.order_id
						}
						//(换成自己项目的接口即可,请求参数根据后端而定)
						that.$httpObj.ApplePay(params).then((res) => {
							console.log(res, "请求接口返回的信息");
							that.$util.Tips({
								title: res.info
							})
							// 支付成功跳转个人中心
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/tabbar/my'
								})
							}, 700)
						}).catch((err) => {
							console.log(err, "错误信息");
						})
					},
					fail: (e) => {
						console.log(e);
						// uni.LTY.loaded();
						console.log(e, "支付错误");
					},
					complete: () => {
						// 购买成功

						console.log("payment结束");
					}


				})
			},

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值