目录
前言
最近开发公司苹果的包月商品支付,假设用户购买的包月商品,在自动续费管理页面修改包季商品,后端接口如何收到通知呢???
通过查看官方文档了解,在开发者账户确实可以配服务器的通知地址,至于怎么配服务起通知这个pass吧,相信大家都可以找到
如果找不到可以参考这个地址:https://help.apple.com/app-store-connect/#/dev0067a330b,里面怎么已经介绍的很详细了。
下面讲讲怎么样的接口能收到app store的通知,首先说需要写一个什么样的接口吧,其次再说说通知的消息类型。
一、需要怎么样的接口呢?
官方原文:
To receive server notifications from the App Store, provide your server URL in App Store Connect. For instructions see Enter a URL for App Store Server Notifications. Ensure that your server establishes a secure connection with the App Store as described in Preventing Insecure Network Connections.
Upon receiving a server notification, respond to the App Store with an HTTP status code of 200
if the post was successful. If the post was unsuccessful, send HTTP 50x
or 40x
to have the App Store retry the notification.
大概意思如下:苹果服务器需要配一个通知的地址,前提这个地址还需要是安全的,那就是https呗,还有一个关键信息就是苹果的通知还有重试机制,需要重试50x或40x的错误码,200表示服务起已经成功通知不需要重试。responseBody
类型是Content-Type: application/json,还是没说请求是GET还是POST,别着急看下面的这原文
官方原文:
When you enable your App Store Server Notifications URL on App Store Connect, you automatically receive server notifications on your secure server. The App Store sends notifications as JSON objects for a number of subscription events. These JSON events, documented in responseBody, contain fields and information you can use to react to transactions according to your business logic. For information on configuring and using server notifications, see Enabling Server-to-Server Notifications.
Receive Server-to-Server Notifications
The App Store delivers JSON objects via an HTTP POST to your server for notable subscription events. Your server is responsible for parsing, interpreting, and responding to all server-to-server notification posts.
The server-to-server notification is an HTTP POST. The body of the POST contains the data elements described in responseBody. The App Store posts notifications for subscription events represented by the values of the notification_type
field.
我说一下咱们关心的重点吧
首先App Store通过HTTP POST将JSON对象传递给您的服务器,responseBody中包含JSON数据。notification_type表示
订阅事件的通知类型。那下面关系的就是notification_type有几种通知类型呢。类型如下:
CANCEL
Indicates that either Apple customer support canceled the subscription or the user upgraded their subscription. The cancellation_date
key contains the date and time of the change.
DID_CHANGE_RENEWAL_PREF
Indicates the customer made a change in their subscription plan that takes effect at the next renewal. The currently active plan is not affected.
事件大概意思用户改变自动续费的计划,在下个自动续费周期生效,当前计划为立即生效
DID_CHANGE_RENEWAL_STATUS
Indicates a change in the subscription renewal status. Check auto_renew_status_change_date_ms
and auto_renew_status
in the JSON response to know the date and time of the last status update and the current renewal status.
DID_FAIL_TO_RENEW
Indicates a subscription that failed to renew due to a billing issue. Check is_in_billing_retry_period
to know the current retry status of the subscription, and grace_period_expires_date
to know the new service expiration date if the subscription is in a billing grace period.
DID_RECOVER
Indicates a successful automatic renewal of an expired subscription that failed to renew in the past. Check expires_date
to determine the next renewal date and time.
INITIAL_BUY
Occurs at the user's initial purchase of the subscription. Store latest_receipt
on your server as a token to verify the user’s subscription status at any time by validating it with the App Store.
INTERACTIVE_RENEWAL
Indicates the customer renewed a subscription interactively, either by using your app’s interface, or on the App Store in the account's Subscriptions settings. Make service available immediately.
RENEWAL
Indicates a successful automatic renewal of an expired subscription that failed to renew in the past. Check expires_date
to determine the next renewal date and time.
REFUND
Indicates that App Store successfully refunded a transaction. The cancellation_date_ms
contains the timestamp of the refunded transaction; the original_transaction_id
and product_id
identify the original transaction and product, and cancellation_reason
contains the reason.
好了到这基本应该怎么找到写服务器接受通知的地址了吧,下面例子仅供参考:
二. 接受通知接口地址
jesery接口代码如下(示例):
private static final String RESP = "";
@POST
@Path("/callback")
@Produces(value = "text/plain")
public String notify(String body) {
logger.info("callback from mac iap data {}", body);
JSONObject j = JSON.parseObject(body);
if ("DID_CHANGE_RENEWAL_PREF".equals(j.getString("notification_type"))) {
return RESP;
}
}
/*if (!"PROD".equals(j.getString("environment"))) {
return RESP;
}*/
if (!"true".equals(j.getString("auto_renew_status"))) {
return RESP;
}
if ("INTERACTIVE_RENEWAL".equals(j.getString("notification_type"))
|| "RENEWAL".equals(j.getString("notification_type"))) {
return RESP;
}
return RESP;
}