IAP支付初识

IAP全称In-App Purchase,也可以叫内购。查看百度百科,IAP是一种智能移动终端应用程序的付费模式。大概的意思:用户在APP内通过付费,来享受APP内提供的服务或体验。

我不仅仅想总结一下苹果的IAP,还想反思一下支付要注意的细节。

从问题入手:如何确认苹果交易的唯一标识。想要做到支付的幂等性,每一笔订单都应该有一个唯一的标识。来避免出现类似这样的现象:用户支付了一次,服务端却创建了多个订单。

交易的唯一标识

我们使用服务端校验支付流程,每笔交易都通过服务器请求苹果服务器来完成校验。

IAP支付的流程

  1. 苹果IAP支付有一个“事务”的概念。当用户支付完成,苹果会回调APP,传递一个receipt的凭证。
  2. APP端本地校验receipt或者APP回传到自己Server端对其校验
  3. 校验通过后,APP端主动finish调该transaction

transaction理解

对于每一次支付,都会产生一个新的transaction,用来唯一标识该订单。客户端每次finish的对象也是它。

对于它是不是唯一的疑问,查阅了部分文档,有很多订阅型的产品的开发反馈:transaction在一段时间后可能会发生变化。但我查询的结果认为:transaction可以唯一确定一笔交易。

如下摘录苹果论坛的一段描述:

There are two transactionIdentifiers - the one that comes with the particular purchase and the one in the purchase receipt. Any call to updatedTransactions, including the call when you originally purchase the IAP, has a transaction.transactionIdentifier that is always unique. When you originally purchase an IAP or when you repurchase an IAP for free or when you restore an IAP the receipt will also contain the “unique” transaction_id of the original purchase transaction.transactionIdentifier.

receipt校验

服务端需要receipt-data去苹果服务器校验,但苹果网络响应可能不会很及时,有时候还会出现多个请求超时的情况。

未完待续…