配置苹果服务器通知URL后
在该API中获取苹果通知数据
版本2(version 2 notification)的通知,版本二通知是jwt编码实现
根据解码得到的notificationType
以下主要是处理notificationType为 CONSUMPTION_REQUEST 时需要
用户申请退款后,苹果通知服务器(类型:CONSUMPTION_REQUEST),我们需要提供用户的数据,以供协助“退款决策系统” 来判断可以给用户退款。
提供用户数据 请求苹果服务器API文档 官方文档:Apple Developer Documentation
API: https://api.storekit.itunes.apple.com/inApps/v1/transactions/consumption/
具体请求:
- //参数
- $request_data = [
- 'accountTenure' => '',//用户账户的年龄
- 'appAccountToken' => '',
- 'consumptionStatus' => '',//消耗状态 0:不确定;1:没消耗;2:部分消耗;3:完全消耗
- 'customerConsented' => true,//用户是否同意提供消费数据 true:同意
- 'deliveryStatus' => 0,//应用是否成功交付了正常运行的应用内购买
- 'lifetimeDollarsPurchased' => '',//计算用户所有充值的总金额
- 'lifetimeDollarsRefunded' => '',//计算用户所有退款的总金额
- 'platform' => 1,
- 'playTime' => 0,
- 'sampleContentProvided' => false,
- 'userStatus' => 1,
- ];
- $url = 'https://api.storekit.itunes.apple.com/inApps/v1/transactions/consumption/'.{originalTransactionId};
- $ch = curl_init($url);
- $data_query = json_encode($request_data);
-
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
- curl_setopt($ch, CURLOPT_HEADER, true);
-
- $token = $apple_jwt_token;
- if(!$token)
- {
- return;
- }
- $header = [
- "Authorization: Bearer $token",
- "Content-Type: application/json",
- ];
- curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data_query);
- curl_setopt($ch, CURLOPT_TIMEOUT, 3);
-
- $response = curl_exec($ch);
- // var_dump($response);
- //Response Codes 202 Accepted 视为请求成功
-
- if ($response === false){
-
- }
- curl_close($ch);
- return $response;
apple_jwt_token 需要获取Jwt 签名 的token;用python 获取
- import time, jwt
-
- # 1.JWT header
- header = {
- "alg": "ES256",
- "kid": "", # Apple Store 密钥Id
- "typ": "JWT"
- }
- # 2.JWT payload
- payload = {
- "iss": "", # Apple Store iss-id
- "iat": int(time.time()),
- "exp": int(time.time()) + 60*60,
- "aud": "appstoreconnect-v1",
- "bid": "" # Apple Store bid-id
- }
- # AuthKey_XXXXX.p8 为 Apple Store 密钥 Id 关联的私钥对令牌
- privatekey = open('./AuthKey_XXXXX.p8', 'r').read()
- # 3. JWT Token
- token = jwt.encode(payload, privatekey, algorithm='ES256', headers=header)
- return token