uniapp苹果内购总结


对于app内有虚拟物品交易的,一律需要接上苹果支付,
uniapp支付文档: https://uniapp.dcloud.net.cn/api/plugins/payment.html#requestpayment

主要流程如下:
1、调用plus.payment.getChannels获取支付通道IAP,IAP支付通道的ID为“appleiap”
2、调用ID为“appleiap”的PaymentChannel对象的requestOrder方法,向Appstore请求有效的商品详情。
3、 调用plus.payment.request方法发起支付请求,传入statement的参数为JSON对象,可以设置如下参数
productid String(必选)要支付的商品的标识(必须是调用requestOrder返回的有效的商品标识)
username String(可选)购买商品用户的用户名
quantity String (可选)购买商品的数量,如果不填写默认为1
调用后端接口校验订单信息

3.1获取支付通道:
3.2通过productid获取对应商品信息

1、创建App内购项目,设置好税务和银行卡等配置信息

登录iTunes Connect https://appstoreconnect.apple.com/ 应用后台, 可参考 https://help.apple.com/app-store-connect/#/devb57be10e7以及 详细设置过程: http://wap.qianfanyun.com/help/990
在这里插入图片描述在这里插入图片描述

2、HbuilderX 勾选Apple应用内支付

在这里插入图片描述

	export default {
		data() {
			return {
				iap: null, // 支付通道
				ids: ['day', 'quarter', 'year'], // 应用内购项目产品 ID 数组
				product_list: [], // 应用内购项目列表
				list_index: -1,// 商品列表选中的下标
				vipRuleList: [] //显示的可购买项目列表
			}
		},
		methods: {
			// 获取支付通道
			getChannels() {
				// #ifdef APP-PLUS
				plus.payment.getChannels(res => {
					self.pay_type = 'appleiap'
					let channel = res.find(i => i.id === 'appleiap')
					console.log("channel" + channel)
					this.iap = channel ? channel : null
					this.requestOrder()
				}, function(e) {
					plus.nativeUI.alert("获取支付通道失败,请稍后重试。", function() {}, "提示");
				})
				// #endif
			},
			// 获取内购项目列表
			requestOrder() {
				uni.showLoading({
					title: '检测支付环境...'
				})
				// #ifdef APP-PLUS
				this.vipRuleList = []
				this.iap.requestOrder(
					this.ids,
					res => {
						uni.hideLoading()
                          this.vipRuleList=res
					},
					(errormsg) => {
						uni.hideLoading()
						plus.nativeUI.alert("获取应用内购项目失败,请稍后重试。", function(e) {}, "提示");
						console.log(JSON.stringify(errormsg))

					}
				)
				// #endif
			},
			 
			/* ios内购 */
			requestPayment(order_id) {
				uni.showLoading({
					title: '支付中请勿关闭...'
				})
				uni.requestPayment({
					provider: 'appleiap',
					orderInfo: {
						productid: this.ids[this.list_index],
					   username: self.userInfo.id +"-"+ order_id //透传参数,一般用于标记订单和用户的关系,向苹果服务器二次验证票据时返回此字段
					},
					success: (e) => {
						console.log(e, '支付成功');
						uni.hideLoading()
						if (e.errMsg == 'requestPayment:ok') {
							//在此处请求开发者服务器,在服务器端请求苹果服务器验证票据
							self.validatePaymentResult({
								order_id,
								transactionIdentifier: e.transactionIdentifier,//交易唯一标识
								receipt_data: e.transactionReceipt
							})

						}
					},
					fail: (e) => {
						uni.hideLoading()
						console.log(e);
						uni.showModal({
							content: "支付失败,原因为: " + e.errMsg,
							showCancel: false
						})
					},
					complete: () => {
						uni.hideLoading()
						console.log("payment结束")
						this.loading = false;
					}
				})
			},
			async createIosOrder(list) {
				var info = self.vipRuleList[self.list_index];
			 	uni.showModal({
					content:"列表"+JSON.stringify(self.vipRuleList)+"-info"+JSON.stringify(info)+"-list_index"+(self.list_index==-1),
					showCancel: false
				})  
				/* if (self.list_index==-1) {
					return uni.$u.toast('请选择充值类型');
				} */
				if (!self.pay_type) {
					return uni.$u.toast('请选择支付方式')
				}
				uni.showLoading({})
				const res = await createIosOrder({
					price: info.price,
					vip_rule_id: info.id,
					pay_type: self.pay_type
				})
				this.requestPayment(res.orderInfo.order_id)

			},
			async validatePaymentResult(param) {
				const res = await validatePaymentResult(param)
				uni.showModal({
					content: "支付成功",
					showCancel: false
				})
			},
			 
			   
		},
		 
	}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值