0%

苹果内购凭据校验

跟后端联调自动续订订阅商品的过程中,对方有问我相关的验证地址和错误码是啥意思,因此将苹果内购凭据验证相关的知识做了个整理,希望能帮助有需要的人。

苹果提供了两套环境,分别称为线上(生产)环境和沙河(测试)环境,分别对应着不同的地址:

参考链接

苹果的建议先使用线上环境验证,当返回 21007 时,再使用沙河环境验证,避免在审核期间和审核通过后切换验证地址!

调用验证接口需要使用 POST 方式请求,把需要验证的凭据以 JSON 的格式放到 body 体里,Content-Type 使用 application/json ,举例说明:

1
2
3
4
5
{
"receipt-data":"Base64字符串",
"password":"自动续订订阅商品才需要",
"exclude-old-transactions":"对于订阅的商品,不包含旧的交易信息?"
}

参考链接

苹果服务器的响应数据采用 JSON 格式,其结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{
"receipt": {
"receipt_type": "ProductionSandbox",
"adam_id": 0,
"app_item_id": 0,
"bundle_id": "com.debuly.Player",
"application_version": "3.18",
"download_id": 0,
"version_external_identifier": 0,
"receipt_creation_date": "2020-06-22 08:05:27 Etc/GMT",
"receipt_creation_date_ms": "1592813127000",
"receipt_creation_date_pst": "2020-06-22 01:05:27 America/Los_Angeles",
"request_date": "2020-09-16 03:38:07 Etc/GMT",
"request_date_ms": "1600227487083",
"request_date_pst": "2020-09-15 20:38:07 America/Los_Angeles",
"original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
"original_purchase_date_ms": "1375340400000",
"original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
"original_application_version": "1.0",
"in_app": [
{
"quantity": "1",
"product_id": "5154",
"transaction_id": "1000000633450491",
"original_transaction_id": "1000000633450491",
"purchase_date": "2020-03-02 07:26:22 Etc/GMT",
"purchase_date_ms": "1583133982000",
"purchase_date_pst": "2020-03-01 23:26:22 America/Los_Angeles",
"original_purchase_date": "2020-03-02 07:26:22 Etc/GMT",
"original_purchase_date_ms": "1583133982000",
"original_purchase_date_pst": "2020-03-01 23:26:22 America/Los_Angeles",
"is_trial_period": "false"
},
{
"quantity": "1",
"product_id": "10413",
"transaction_id": "1000000633349904",
"original_transaction_id": "1000000633349904",
"purchase_date": "2020-03-02 03:22:51 Etc/GMT",
"purchase_date_ms": "1583119371000",
"purchase_date_pst": "2020-03-01 19:22:51 America/Los_Angeles",
"original_purchase_date": "2020-03-02 03:22:51 Etc/GMT",
"original_purchase_date_ms": "1583119371000",
"original_purchase_date_pst": "2020-03-01 19:22:51 America/Los_Angeles",
"is_trial_period": "false"
},
{
"quantity": "1",
"product_id": "10414",
"transaction_id": "1000000633339108",
"original_transaction_id": "1000000633339108",
"purchase_date": "2020-03-02 02:24:39 Etc/GMT",
"purchase_date_ms": "1583115879000",
"purchase_date_pst": "2020-03-01 18:24:39 America/Los_Angeles",
"original_purchase_date": "2020-03-02 02:24:39 Etc/GMT",
"original_purchase_date_ms": "1583115879000",
"original_purchase_date_pst": "2020-03-01 18:24:39 America/Los_Angeles",
"is_trial_period": "false"
}
]
},
"environment": "Sandbox",
"status": 0
}
  • environment:生成凭据的环境,可能值有 Sandbox 和 Production

  • is-retryable:如果返回 1 意味着临时出错,需要稍后重试;返回 0 意味着出错了,不用重试,具体错误查看 status 字段

  • latest_receipt:最新的额自动续订订阅凭据,Base64编码的

  • latest_receipt_info:所有自动续订订阅交易信息

  • pending_renewal_info:即将更新的自动续订订阅数组

  • receipt:此次发送给苹果验证的凭据信息

  • status:状态码,当为 0 时,表示凭据通过校验,否则表示有错误,具体往下看

    参考链接

错误状态码

21000

1
2

The request to the App Store was not made using the HTTP POST request method.

21001

1
2

This status code is no longer sent by the App Store.

21002

1
2

The data in the `receipt-data` property was malformed or the service experienced a temporary issue. Try again.

21003

1
2

The receipt could not be authenticated.

21004

1
2

The shared secret you provided does not match the shared secret on file for your account.

21005

1
2

The receipt server was temporarily unable to provide the receipt. Try again.

21006

1
2

This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response. Only returned for iOS 6-style transaction receipts for auto-renewable subscriptions.

21007

1
2

This receipt is from the test environment, but it was sent to the production environment for verification.

21008

1
2

This receipt is from the production environment, but it was sent to the test environment for verification.

21009

1
2

Internal data access error. Try again later.

21010

1
2

The user account cannot be found or has been deleted.