一、说明
由于本人在开发ios订阅范例支付接入的时间,碰到了许多坑,也查了不少资料,逐步美满了整个ios订阅支付服务端接入的功能,在这里写下总结和一些注意事项的纪录,方便将来需要重新接入或者避免一些不必要的坑,这里重要讲的是服务端的接入。
二、接入原因
客户端实现ios订阅支付后,支付成功会返回一个收据(一大串随机字符串),需要哀求苹果服务器的接口来校验该收据的真实性,同时校验完成后苹果服务器也会发通知告诉我们的本身的服务器(ios后台配置的回调地点),这个时间就需要服务端来接入苹果服务器并处置惩罚该支付逻辑。这里不使用客户端直接接入苹果服务器校验收据接口的原因大概就是因为订阅范例需要共享密钥,防止该密钥暴露,同时服务器接入能更好处置惩罚逻辑。
三、接入必要配置
1.订阅范例需要从苹果后台获取一个共享密钥
2.需要在苹果后台配置好回调地点
3.需要配置订阅产物(周期,代价,我本身没什么相识,这个需要注意的是订阅有按组分,同一个组和不同组的产物是有区别的,例如同一个组的不同产物进行购买,他们的原始订单大概雷同)
四、接入必要资源
简体中文文档 - Apple Developer
校验收据:通过 App Store 验证收据 - 简体中文文档 - Apple Developer
回调通知:启用 App Store 服务器通知 - 简体中文文档 - Apple Developer
退款处置惩罚:处置惩罚退款通知 - 简体中文文档 - Apple Developer
收据校验沙箱环境:https://sandbox.itunes.apple.com/verifyReceipt
收据校验生产环境:https://buy.itunes.apple.com/verifyReceipt
五、接入流程
1.服务端接入apple校验收据接口
注意事项:防止雷同数据重复校验或者雷同哀求大量哀求导致的不稳定因素,建议使用时间戳校验防备,不要将共享密钥暴露在客户端,共享密钥同时也可以对回调通知进行校验,校验收据后不要做为订单处置惩罚,最好齐备以回调为主。
接口地点:https://buy.itunes.apple.com/verifyReceipt
接口范例:POST
接口格式:appliaction/json
功能说明:该接口重要是用来恢复订阅和支付校验,校验完成会有回调通知到商户服务器
哀求参数:
官方:Apple Developer Documentation
- {
- "password":"这里是共享密钥",
- "receipt-data":"这里是收据的字符串"
- }
复制代码 返回结果:
状态码:Apple Developer Documentation
失败:
成功:
成功结果参数文档:responseBody | Apple Developer Documentation
重要参数说明:
- {
- "environment": "Production", //请求环境
- "receipt": {
- "receipt_type": "Production",
- "adam_id": xxx,
- "app_item_id": xxx,
- "bundle_id": "com.xxx",
- "application_version": "2",
- "download_id": xxx,
- "version_external_identifier": xxx,
- "receipt_creation_date": "2022-10-24 07:14:36 Etc/GMT",
- "receipt_creation_date_ms": "1666595676000",
- "receipt_creation_date_pst": "2022-10-24 00:14:36 America/Los_Angeles",
- "request_date": "2023-02-24 03:29:47 Etc/GMT",
- "request_date_ms": "1677209387760",
- "request_date_pst": "2023-02-23 19:29:47 America/Los_Angeles",
- "original_purchase_date": "2022-07-04 03:24:29 Etc/GMT",
- "original_purchase_date_ms": "1656905069000",
- "original_purchase_date_pst": "2022-07-03 20:24:29 America/Los_Angeles",
- "original_application_version": "2",
- "in_app": [ //所有订单都可能存在,主要是非订阅订单
- {
- "quantity": "1",
- "product_id": "xxxx",
- "transaction_id": "xxxx",
- "original_transaction_id": "xxx",
- "purchase_date": "2022-07-04 03:25:29 Etc/GMT",
- "purchase_date_ms": "1656905129000",
- "purchase_date_pst": "2022-07-03 20:25:29 America/Los_Angeles",
- "original_purchase_date": "2022-07-04 03:25:30 Etc/GMT",
- "original_purchase_date_ms": "1656905130000",
- "original_purchase_date_pst": "2022-07-03 20:25:30 America/Los_Angeles",
- "expires_date": "2022-07-11 03:25:29 Etc/GMT",
- "expires_date_ms": "1657509929000",
- "expires_date_pst": "2022-07-10 20:25:29 America/Los_Angeles",
- "web_order_line_item_id": "xxx",
- "is_trial_period": "false",
- "is_in_intro_offer_period": "false",
- "in_app_ownership_type": "PURCHASED"
- }
- ]
- },
- "latest_receipt_info": [ //订单订阅集合,从上往下订单时间倒序,第一个为最新的支付订单
- {
- "quantity": "1",
- "product_id": "xxx", //订单产品id,apple后台支付配置的参数
- "transaction_id": "xxx", //apple当前订单号
- "original_transaction_id": "xxx", //apple原始订单号,该用户在该产品第一笔支付订单,可当做同一批订单朔源
- "purchase_date": "2022-11-25 08:19:27 Etc/GMT", //订单时间
- "purchase_date_ms": "1669364367000",
- "purchase_date_pst": "2022-11-25 00:19:27 America/Los_Angeles",
- "original_purchase_date": "2022-07-04 03:25:30 Etc/GMT", //最初购买时间
- "original_purchase_date_ms": "1656905130000",
- "original_purchase_date_pst": "2022-07-03 20:25:30 America/Los_Angeles",
- "expires_date": "2023-11-25 08:19:27 Etc/GMT", //订阅过期时间
- "expires_date_ms": "1700900367000",
- "expires_date_pst": "2023-11-25 00:19:27 America/Los_Angeles",
- "web_order_line_item_id": "xxx",
- "is_trial_period": "false", //是否是试用订单
- "is_in_intro_offer_period": "false",
- "in_app_ownership_type": "PURCHASED",
- "subscription_group_identifier": "xxx"
- }
- ],
- "latest_receipt": "------------加密串------------",
- "pending_renewal_info": [
- {
- "auto_renew_product_id": "xxx",
- "product_id": "xxx",
- "original_transaction_id": "xxx",
- "auto_renew_status": "0"
- }
- ],
- "status": 0 //状态成功
- }
复制代码
2.apple支付回调通知接入
回调通知配置:苹果后台可配置生产环境和沙箱环境的APPLE回调通知地点
接入文档:responseBodyV1 | Apple Developer Documentation
提示:接入apple支付回调apple提供了v1和v2两种范例,v2需要多一步加解密的操作
功能说明:该通知重要是订单状态的变更等,包括续订,付费通知,取消订阅,退款等通知
可进行订单处置惩罚
通知范例说明:
| 首次购买 | | 退款 | | 取消订阅 | | 主动续订 | | 应用界面或appstore手动续订 | | 恢复订阅 | 回调哀求参数示例
- {
- "notification_type":"DID_RENEW", //通知类型,该类型为续订通知
- "auto_renew_product_id":"xxx",
- "password":"xxx", //共享密钥,可以验证是否真实通知
- "environment":"Sandbox",
- "original_transaction_id":xxx,
- "unified_receipt":{
- "status":0,
- "environment":"Sandbox",
- "latest_receipt_info":[ //第一笔为最新的通知订单
- {
- "quantity":"1",
- "product_id":"xxx",
- "transaction_id":"xxx", //当前订单号
- "purchase_date":"2022-06-08 09:51:55 Etc/GMT",
- "purchase_date_ms":"1654681915000",
- "purchase_date_pst":"2022-06-08 02:51:55 America/Los_Angeles",
- "original_purchase_date":"2022-06-08 04:36:20 Etc/GMT",
- "original_purchase_date_ms":"1654662980000",
- "original_purchase_date_pst":"2022-06-07 21:36:20 America/Los_Angeles",
- "expires_date":"2022-06-08 09:54:55 Etc/GMT",
- "expires_date_ms":"1654682095000",
- "expires_date_pst":"2022-06-08 02:54:55 America/Los_Angeles",
- "web_order_line_item_id":"xxx",
- "is_trial_period":"false",
- "is_in_intro_offer_period":"false",
- "original_transaction_id":"xxx", //原始订单号,该产品首次的订单号
- "in_app_ownership_type":"PURCHASED",
- "subscription_group_identifier":"xxx"
- },
- {
- "quantity":"1",
- "product_id":"xxx",
- "transaction_id":"xxx",
- "purchase_date":"2022-06-08 09:48:03 Etc/GMT",
- "purchase_date_ms":"xxx",
- "purchase_date_pst":"2022-06-08 02:48:03 America/Los_Angeles",
- "original_purchase_date":"2022-06-08 04:36:20 Etc/GMT",
- "original_purchase_date_ms":"1654662980000",
- "original_purchase_date_pst":"2022-06-07 21:36:20 America/Los_Angeles",
- "expires_date":"2022-06-08 09:51:03 Etc/GMT",
- "expires_date_ms":"1654681863000",
- "expires_date_pst":"2022-06-08 02:51:03 America/Los_Angeles",
- "web_order_line_item_id":"xxx",
- "is_trial_period":"false",
- "is_in_intro_offer_period":"false",
- "original_transaction_id":"xxx",
- "in_app_ownership_type":"PURCHASED",
- "subscription_group_identifier":"xxx"
- }
- ],
- "pending_renewal_info":[
- {
- "auto_renew_status":"1",
- "auto_renew_product_id":"xxx",
- "product_id":"xxx",
- "original_transaction_id":"xxx"
- }
- ]
- },
- "bvrs":"xxx",
- "bid":"com.xxx",
- "auto_renew_status":"true"
- }
复制代码 六、接入问题说明
1.latest_receipt_info聚集订单按时间倒序排序,以是第一笔为最新的订单
2.ios收据为账户维度,即收据和通知获取的数据为该账户的历史订单聚集,非当前支付
3.ios订单未返回金额等参数,需要本身做处置惩罚
4.ios退款通知不支持订阅范例
5.apple支付为全球性支付,需要本身处置惩罚货币转换
6.同一个产物包括同产物组,在同一个账户支付会生成一个原始订单号,可根据该订单号溯源
7.订单校验只适用于恢复订阅和收据校验,详细支付处置惩罚按通知为主
8.状态码21007为测试环境的收据哀求到了正式环境
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |