uniapp实现苹果支付流程

uniapp 苹果支付流程

1.配置文件中勾中Apple应用内支付

2.调用 plus.payment.getChannels 来获取支付通道

iphonepay() {
				const that = this
				uni.showLoading({
					title: '检测支付环境...',
					mask: true
				})
				plus.payment.getChannels((channels) => {
					for (var i in channels) {
                        // 判断是否苹果支付
						if (channels[i].id === 'appleiap') {
							that.iapChannel = channels[i]
							that.requestOrder()
						}
					}
				})
			}

3.调用 上一步拿到对象的requestOrder方法 该方法需要传入三个参数 requestOrder(‘苹果内购商品id’,成功回调,失败回调) 拿到订单数据 拿到订单数据的商品id

requestOrder() {
	const that = this
    // ['xxxxx'] 是平台申请拿到的内购商品的id 
	that.iapChannel.requestOrder(['xxxxxx''xxxxxx'], function(event) {
	uni.hideLoading()
	console.log(event)
	for (var index in event) {
		var OrderItem = event[index]
		console.log(OrderItem)
		that.topay(OrderItem.productid)
	}
}, function(erroemsg) {
		uni.hideLoading()
		uni.showToast({
		title: "获取支付通道失败:" + errormsg.message,
		icon: 'none'
	})
	})
	}

4.最后调用uni.requestPayment 拿到后端接口需要的交易id 校验体

  • 使用uni.requestPayment时需要传入的orderInfo 是Object对象类型
         topay(id) {
				const that = this
				uni.showLoading({
					title: '充值中请勿离开',
					mask: true
				})
				uni.requestPayment({
					provider: 'appleiap',
					orderInfo: {
						productid: id
					},
					success: (res => {
						uni.hideLoading()
						const orderId = this.orderId //这个订单id 是由后端返回的
						const transactionId = res.transactionIdentifier //交易id
						const payload = res.transactionReceipt //校验体
						upayfor({
							transactionId,
							payload,
							orderId
						}).then(res => {
							console.log(res)
							uni.showToast({
								title: '充值成功'
							})
							that.getUserInfo()
						})
					}),
					fail: (e) => {
						uni.hideLoading()
						uni.showModal({
							content: "支付失败",
							showCancel: false
						})
					}
})

官方文档 https://uniapp.dcloud.io/api/plugins/payment?id=orderinfo

第一次开发这个功能,有地方还理解不够,记录一下。整个过程中有遇到-100的问题 卡了半天 结果是沙盒测试申请的东西没有补充完整

感谢原文博主 :https://blog.csdn.net/wangwangli6/article/details/104892562/

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值