ios应用内支付

准备前端代码

  1. 获取支付通道 (uni.getProvider)
uni.getProvider({
  service: 'payment',
  success: (res) => {
    const iapChannel = res.providers.find((channel) => {
      return (channel.id === 'appleiap')
    })
  }
});
  1. 通过支付通道获取产品列表 (iapChannel.requestProduct)
	getProduct(productIds) {
		return new Promise((resolve, reject) => {
			this._channel.requestProduct(
				productIds || this._productIds,
				(res) => {
					resolve(res);
				},
				(err) => {
					reject(err);
				}
			);
		});
	}
  1. 检查是否存在未关闭的订单 (iapChannel.restoreCompletedTransactions, 可选在合适的时机检查)
	async restore() {
		// 检查上次用户已支付且未关闭的订单,可能出现原因:首次绑卡,网络中断等异常
		let _this = this
		try {
			// 从苹果服务器检查未关闭的订单,可选根据 username 过滤,和调用支付时透传的值一致
			const transactions = await this.restoreCompletedTransactions({});
			if (!transactions.length) {
				return;
			}
			for (const transaction of transactions) {
				switch (transaction.transactionState) {
					case IapTransactionState.purchased:
							//这里写上传数据到服务器的代码
							console.log('成功');
							_this.finishTransaction(transaction);
						}
						break;
					case IapTransactionState.failed:
						// 关闭未支付的订单
						_this.finishTransaction(transaction);
						break;
					default:
						break;
				}
			};
		} catch (e) {
			uni.showModal({
				content: e.message,
				showCancel: false
			});
		} finally {

		}
	}
  1. 请求支付,传递产品信息 (uni.requestPayment)
	requestPayment(orderInfo) {
		return new Promise((resolve, reject) => {
			uni.requestPayment({
				provider: 'appleiap',
				orderInfo: orderInfo,
				success: (res) => {
					resolve(res);
				},
				fail: (err) => {
					reject(err);
				}
			});
		});
	}
  1. 客户端接收苹果返回的支付票据发送到服务器,在服务器请求苹果服务器验证支付是否有效
  2. 服务器验证票据有效后在客户端关闭订单 (iapChannel.finishTransaction)

服务器端处理

前端传过来transactionReceipt以及透传参数(一般是自己的订单号), 我们拿着transactionReceipt请求苹果的服务器即可,请求苹果服务器会返回该支付的一切信息,包括支付订单号,支付时间,支付数量等

如果iOS支付遇到问题实在解决不了,可以联系我帮忙解决,前端后端都可以解决(添加的时候一定要备注咨询iOS支付问题)

在这里插入图片描述

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值