-
Notifications
You must be signed in to change notification settings - Fork 248
/
store.go
483 lines (400 loc) · 14.5 KB
/
store.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package api
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/golang-jwt/jwt/v4"
)
const (
HostSandBox = "https://api.storekit-sandbox.itunes.apple.com"
HostProduction = "https://api.storekit.itunes.apple.com"
PathLookUp = "/inApps/v1/lookup/{orderId}"
PathTransactionHistory = "/inApps/v1/history/{originalTransactionId}"
PathTransactionInfo = "/inApps/v1/transactions/{transactionId}"
PathRefundHistory = "/inApps/v2/refund/lookup/{originalTransactionId}"
PathGetALLSubscriptionStatus = "/inApps/v1/subscriptions/{originalTransactionId}"
PathConsumptionInfo = "/inApps/v1/transactions/consumption/{originalTransactionId}"
PathExtendSubscriptionRenewalDate = "/inApps/v1/subscriptions/extend/{originalTransactionId}"
PathExtendSubscriptionRenewalDateForAll = "/inApps/v1/subscriptions/extend/mass/"
PathGetStatusOfSubscriptionRenewalDate = "/inApps/v1/subscriptions/extend/mass/{productId}/{requestIdentifier}"
PathGetNotificationHistory = "/inApps/v1/notifications/history"
PathRequestTestNotification = "/inApps/v1/notifications/test"
PathGetTestNotificationStatus = "/inApps/v1/notifications/test/{testNotificationToken}"
)
type StoreConfig struct {
KeyContent []byte // Loads a .p8 certificate
KeyID string // Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
BundleID string // Your app’s bundle ID
Issuer string // Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
Sandbox bool // default is Production
}
type StoreClient struct {
Token *Token
httpCli *http.Client
cert *Cert
}
// NewStoreClient create a appstore server api client
func NewStoreClient(config *StoreConfig) *StoreClient {
token := &Token{}
token.WithConfig(config)
client := &StoreClient{
Token: token,
cert: &Cert{},
httpCli: &http.Client{
Timeout: 30 * time.Second,
},
}
return client
}
// NewWithClient creates a appstore server api client with a custom http client.
func NewStoreClientWithHTTPClient(config *StoreConfig, httpClient *http.Client) *StoreClient {
token := &Token{}
token.WithConfig(config)
client := &StoreClient{
Token: token,
cert: &Cert{},
httpCli: httpClient,
}
return client
}
// GetALLSubscriptionStatuses https://developer.apple.com/documentation/appstoreserverapi/get_all_subscription_statuses
func (a *StoreClient) GetALLSubscriptionStatuses(ctx context.Context, originalTransactionId string) (rsp *StatusResponse, err error) {
URL := HostProduction + PathGetALLSubscriptionStatus
if a.Token.Sandbox {
URL = HostSandBox + PathGetALLSubscriptionStatus
}
URL = strings.Replace(URL, "{originalTransactionId}", originalTransactionId, -1)
statusCode, body, err := a.Do(ctx, http.MethodGet, URL, nil)
if err != nil {
return
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
}
err = json.Unmarshal(body, &rsp)
if err != nil {
return nil, err
}
return
}
// LookupOrderID https://developer.apple.com/documentation/appstoreserverapi/look_up_order_id
func (a *StoreClient) LookupOrderID(ctx context.Context, orderId string) (rsp *OrderLookupResponse, err error) {
URL := HostProduction + PathLookUp
if a.Token.Sandbox {
URL = HostSandBox + PathLookUp
}
URL = strings.Replace(URL, "{orderId}", orderId, -1)
statusCode, body, err := a.Do(ctx, http.MethodGet, URL, nil)
if err != nil {
return
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
}
err = json.Unmarshal(body, &rsp)
if err != nil {
return nil, err
}
return
}
// GetTransactionHistory https://developer.apple.com/documentation/appstoreserverapi/get_transaction_history
func (a *StoreClient) GetTransactionHistory(ctx context.Context, originalTransactionId string, query *url.Values) (responses []*HistoryResponse, err error) {
URL := HostProduction + PathTransactionHistory
if a.Token.Sandbox {
URL = HostSandBox + PathTransactionHistory
}
URL = strings.Replace(URL, "{originalTransactionId}", originalTransactionId, -1)
if query == nil {
query = &url.Values{}
}
for {
rsp := HistoryResponse{}
statusCode, body, errOmit := a.Do(ctx, http.MethodGet, URL+"?"+query.Encode(), nil)
if errOmit != nil {
return nil, errOmit
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
}
err = json.Unmarshal(body, &rsp)
if err != nil {
return nil, err
}
responses = append(responses, &rsp)
if !rsp.HasMore {
break
}
if rsp.HasMore && rsp.Revision != "" {
query.Set("revision", rsp.Revision)
}
time.Sleep(10 * time.Millisecond)
}
return
}
// GetTransactionInfo https://developer.apple.com/documentation/appstoreserverapi/get_transaction_info
func (a *StoreClient) GetTransactionInfo(ctx context.Context, transactionId string) (rsp *TransactionInfoResponse, err error) {
URL := HostProduction + PathTransactionInfo
if a.Token.Sandbox {
URL = HostSandBox + PathTransactionInfo
}
URL = strings.Replace(URL, "{transactionId}", transactionId, -1)
statusCode, body, err := a.Do(ctx, http.MethodGet, URL, nil)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
}
err = json.Unmarshal(body, &rsp)
if err != nil {
return nil, err
}
return
}
// GetRefundHistory https://developer.apple.com/documentation/appstoreserverapi/get_refund_history
func (a *StoreClient) GetRefundHistory(ctx context.Context, originalTransactionId string) (responses []*RefundLookupResponse, err error) {
baseURL := HostProduction + PathRefundHistory
if a.Token.Sandbox {
baseURL = HostSandBox + PathRefundHistory
}
baseURL = strings.Replace(baseURL, "{originalTransactionId}", originalTransactionId, -1)
URL := baseURL
for {
rsp := RefundLookupResponse{}
statusCode, body, errOmit := a.Do(ctx, http.MethodGet, URL, nil)
if errOmit != nil {
return nil, errOmit
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
}
err = json.Unmarshal(body, &rsp)
if err != nil {
return nil, err
}
responses = append(responses, &rsp)
if !rsp.HasMore {
break
}
data := url.Values{}
if rsp.HasMore && rsp.Revision != "" {
data.Set("revision", rsp.Revision)
URL = baseURL + "?" + data.Encode()
}
time.Sleep(10 * time.Millisecond)
}
return
}
// SendConsumptionInfo https://developer.apple.com/documentation/appstoreserverapi/send_consumption_information
func (a *StoreClient) SendConsumptionInfo(ctx context.Context, originalTransactionId string, body ConsumptionRequestBody) (statusCode int, err error) {
URL := HostProduction + PathConsumptionInfo
if a.Token.Sandbox {
URL = HostSandBox + PathConsumptionInfo
}
URL = strings.Replace(URL, "{originalTransactionId}", originalTransactionId, -1)
bodyBuf := new(bytes.Buffer)
err = json.NewEncoder(bodyBuf).Encode(body)
if err != nil {
return 0, err
}
statusCode, _, err = a.Do(ctx, http.MethodPut, URL, bodyBuf)
if err != nil {
return statusCode, err
}
return statusCode, nil
}
// ExtendSubscriptionRenewalDate https://developer.apple.com/documentation/appstoreserverapi/extend_a_subscription_renewal_date
func (a *StoreClient) ExtendSubscriptionRenewalDate(ctx context.Context, originalTransactionId string, body ExtendRenewalDateRequest) (statusCode int, err error) {
URL := HostProduction + PathExtendSubscriptionRenewalDate
if a.Token.Sandbox {
URL = HostSandBox + PathExtendSubscriptionRenewalDate
}
URL = strings.Replace(URL, "{originalTransactionId}", originalTransactionId, -1)
bodyBuf := new(bytes.Buffer)
err = json.NewEncoder(bodyBuf).Encode(body)
if err != nil {
return 0, err
}
statusCode, _, err = a.Do(ctx, http.MethodPut, URL, bodyBuf)
if err != nil {
return statusCode, err
}
return statusCode, nil
}
// ExtendSubscriptionRenewalDateForAll https://developer.apple.com/documentation/appstoreserverapi/extend_subscription_renewal_dates_for_all_active_subscribers
func (a *StoreClient) ExtendSubscriptionRenewalDateForAll(ctx context.Context, body MassExtendRenewalDateRequest) (statusCode int, err error) {
URL := HostProduction + PathExtendSubscriptionRenewalDateForAll
if a.Token.Sandbox {
URL = HostSandBox + PathExtendSubscriptionRenewalDateForAll
}
bodyBuf := new(bytes.Buffer)
err = json.NewEncoder(bodyBuf).Encode(body)
if err != nil {
return 0, err
}
statusCode, _, err = a.Do(ctx, http.MethodPost, URL, bodyBuf)
if err != nil {
return statusCode, err
}
return statusCode, nil
}
// GetSubscriptionRenewalDataStatus https://developer.apple.com/documentation/appstoreserverapi/get_status_of_subscription_renewal_date_extensions
func (a *StoreClient) GetSubscriptionRenewalDataStatus(ctx context.Context, productId, requestIdentifier string) (statusCode int, err error) {
URL := HostProduction + PathGetStatusOfSubscriptionRenewalDate
if a.Token.Sandbox {
URL = HostSandBox + PathGetStatusOfSubscriptionRenewalDate
}
URL = strings.Replace(URL, "{productId}", productId, -1)
URL = strings.Replace(URL, "{requestIdentifier}", requestIdentifier, -1)
statusCode, _, err = a.Do(ctx, http.MethodGet, URL, nil)
return statusCode, nil
}
// GetNotificationHistory https://developer.apple.com/documentation/appstoreserverapi/get_notification_history
// Note: Notification history is available starting on June 6, 2022. Use a startDate of June 6, 2022 or later in your request.
func (a *StoreClient) GetNotificationHistory(ctx context.Context, body NotificationHistoryRequest) (responses []NotificationHistoryResponseItem, err error) {
baseURL := HostProduction + PathGetNotificationHistory
if a.Token.Sandbox {
baseURL = HostSandBox + PathGetNotificationHistory
}
bodyBuf := new(bytes.Buffer)
err = json.NewEncoder(bodyBuf).Encode(body)
if err != nil {
return nil, err
}
URL := baseURL
for {
rsp := NotificationHistoryResponses{}
rsp.NotificationHistory = make([]NotificationHistoryResponseItem, 0)
statusCode, rspBody, errOmit := a.Do(ctx, http.MethodPost, URL, bodyBuf)
if errOmit != nil {
return nil, errOmit
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("appstore api: %v return status code %v", URL, statusCode)
}
err = json.Unmarshal(rspBody, &rsp)
if err != nil {
return nil, err
}
responses = append(responses, rsp.NotificationHistory...)
if !rsp.HasMore {
break
}
data := url.Values{}
if rsp.HasMore && rsp.PaginationToken != "" {
data.Set("paginationToken", rsp.PaginationToken)
URL = baseURL + "?" + data.Encode()
}
time.Sleep(10 * time.Millisecond)
}
return responses, nil
}
// SendRequestTestNotification https://developer.apple.com/documentation/appstoreserverapi/request_a_test_notification
func (a *StoreClient) SendRequestTestNotification(ctx context.Context) (int, []byte, error) {
URL := HostProduction + PathRequestTestNotification
if a.Token.Sandbox {
URL = HostSandBox + PathRequestTestNotification
}
return a.Do(ctx, http.MethodPost, URL, nil)
}
// GetTestNotificationStatus https://developer.apple.com/documentation/appstoreserverapi/get_test_notification_status
func (a *StoreClient) GetTestNotificationStatus(ctx context.Context, testNotificationToken string) (int, []byte, error) {
URL := HostProduction + PathGetTestNotificationStatus
if a.Token.Sandbox {
URL = HostSandBox + PathGetTestNotificationStatus
}
URL = strings.Replace(URL, "{testNotificationToken}", testNotificationToken, -1)
return a.Do(ctx, http.MethodGet, URL, nil)
}
// ParseSignedTransactions parse the jws singed transactions
// Per doc: https://datatracker.ietf.org/doc/html/rfc7515#section-4.1.6
func (a *StoreClient) ParseSignedTransactions(transactions []string) ([]*JWSTransaction, error) {
result := make([]*JWSTransaction, 0)
for _, v := range transactions {
trans, err := a.parseSignedTransaction(v)
if err == nil && trans != nil {
result = append(result, trans)
}
}
return result, nil
}
func (a *StoreClient) parseSignedTransaction(transaction string) (*JWSTransaction, error) {
tran := &JWSTransaction{}
rootCertBytes, err := a.cert.extractCertByIndex(transaction, 2)
if err != nil {
return nil, err
}
rootCert, err := x509.ParseCertificate(rootCertBytes)
if err != nil {
return nil, fmt.Errorf("appstore failed to parse root certificate")
}
intermediaCertBytes, err := a.cert.extractCertByIndex(transaction, 1)
if err != nil {
return nil, err
}
intermediaCert, err := x509.ParseCertificate(intermediaCertBytes)
if err != nil {
return nil, fmt.Errorf("appstore failed to parse intermediate certificate")
}
leafCertBytes, err := a.cert.extractCertByIndex(transaction, 0)
if err != nil {
return nil, err
}
leafCert, err := x509.ParseCertificate(leafCertBytes)
if err != nil {
return nil, fmt.Errorf("appstore failed to parse leaf certificate")
}
if err = a.cert.verifyCert(rootCert, intermediaCert, leafCert); err != nil {
return nil, err
}
pk, ok := leafCert.PublicKey.(*ecdsa.PublicKey)
if !ok {
return nil, fmt.Errorf("appstore public key must be of type ecdsa.PublicKey")
}
_, err = jwt.ParseWithClaims(transaction, tran, func(token *jwt.Token) (interface{}, error) {
return pk, nil
})
if err != nil {
return nil, err
}
return tran, nil
}
// Per doc: https://developer.apple.com/documentation/appstoreserverapi#topics
func (a *StoreClient) Do(ctx context.Context, method string, url string, body io.Reader) (int, []byte, error) {
authToken, err := a.Token.GenerateIfExpired()
if err != nil {
return 0, nil, fmt.Errorf("appstore generate token err %w", err)
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return 0, nil, fmt.Errorf("appstore new http request err %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+authToken)
req.Header.Set("User-Agent", "App Store Client")
req = req.WithContext(ctx)
resp, err := a.httpCli.Do(req)
if err != nil {
return 0, nil, fmt.Errorf("appstore http client do err %w", err)
}
defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return resp.StatusCode, nil, fmt.Errorf("appstore read http body err %w", err)
}
if resp.StatusCode != http.StatusOK {
// try to extract detailed error.
if rErr, ok := newErrorFromJSON(bytes); ok {
return resp.StatusCode, bytes, rErr
}
}
return resp.StatusCode, bytes, err
}