@@ -46,6 +46,9 @@ type StoreConfig struct {
46
46
Sandbox bool // default is Production
47
47
TokenIssuedAtFunc func () int64 // The token’s creation time func. Default is current timestamp.
48
48
TokenExpiredAtFunc func () int64 // The token’s expiration time func. Default is one hour later.
49
+
50
+ // internal variables
51
+ HostDebug string // can be used to override the host for testing
49
52
}
50
53
51
54
type (
@@ -98,6 +101,7 @@ type StoreClient struct {
98
101
Token * Token
99
102
httpCli * http.Client
100
103
cert * Cert
104
+ host string
101
105
}
102
106
103
107
// NewStoreClient create a appstore server api client
@@ -111,6 +115,7 @@ func NewStoreClient(config *StoreConfig) *StoreClient {
111
115
httpCli : & http.Client {
112
116
Timeout : 30 * time .Second ,
113
117
},
118
+ host : getHost (config .Sandbox , config .HostDebug ),
114
119
}
115
120
return client
116
121
}
@@ -124,16 +129,24 @@ func NewStoreClientWithHTTPClient(config *StoreConfig, httpClient *http.Client)
124
129
Token : token ,
125
130
cert : & Cert {},
126
131
httpCli : httpClient ,
132
+ host : getHost (config .Sandbox , config .HostDebug ),
127
133
}
128
134
return client
129
135
}
130
136
137
+ func getHost (sandbox bool , debugHost string ) string {
138
+ if debugHost != "" {
139
+ return debugHost
140
+ }
141
+ if sandbox {
142
+ return HostSandBox
143
+ }
144
+ return HostProduction
145
+ }
146
+
131
147
// GetALLSubscriptionStatuses https://developer.apple.com/documentation/appstoreserverapi/get_all_subscription_statuses
132
148
func (a * StoreClient ) GetALLSubscriptionStatuses (ctx context.Context , originalTransactionId string , query * url.Values ) (rsp * StatusResponse , err error ) {
133
- URL := HostProduction + PathGetALLSubscriptionStatus
134
- if a .Token .Sandbox {
135
- URL = HostSandBox + PathGetALLSubscriptionStatus
136
- }
149
+ URL := a .host + PathGetALLSubscriptionStatus
137
150
URL = strings .Replace (URL , "{originalTransactionId}" , originalTransactionId , - 1 )
138
151
if query != nil {
139
152
URL = URL + "?" + query .Encode ()
@@ -157,10 +170,7 @@ func (a *StoreClient) GetALLSubscriptionStatuses(ctx context.Context, originalTr
157
170
158
171
// LookupOrderID https://developer.apple.com/documentation/appstoreserverapi/look_up_order_id
159
172
func (a * StoreClient ) LookupOrderID (ctx context.Context , orderId string ) (rsp * OrderLookupResponse , err error ) {
160
- URL := HostProduction + PathLookUp
161
- if a .Token .Sandbox {
162
- URL = HostSandBox + PathLookUp
163
- }
173
+ URL := a .host + PathLookUp
164
174
URL = strings .Replace (URL , "{orderId}" , orderId , - 1 )
165
175
statusCode , body , err := a .Do (ctx , http .MethodGet , URL , nil )
166
176
if err != nil {
@@ -181,10 +191,7 @@ func (a *StoreClient) LookupOrderID(ctx context.Context, orderId string) (rsp *O
181
191
182
192
// GetTransactionHistory https://developer.apple.com/documentation/appstoreserverapi/get_transaction_history
183
193
func (a * StoreClient ) GetTransactionHistory (ctx context.Context , originalTransactionId string , query * url.Values ) (responses []* HistoryResponse , err error ) {
184
- URL := HostProduction + PathTransactionHistory
185
- if a .Token .Sandbox {
186
- URL = HostSandBox + PathTransactionHistory
187
- }
194
+ URL := a .host + PathTransactionHistory
188
195
URL = strings .Replace (URL , "{originalTransactionId}" , originalTransactionId , - 1 )
189
196
190
197
if query == nil {
@@ -225,10 +232,7 @@ func (a *StoreClient) GetTransactionHistory(ctx context.Context, originalTransac
225
232
226
233
// GetTransactionInfo https://developer.apple.com/documentation/appstoreserverapi/get_transaction_info
227
234
func (a * StoreClient ) GetTransactionInfo (ctx context.Context , transactionId string ) (rsp * TransactionInfoResponse , err error ) {
228
- URL := HostProduction + PathTransactionInfo
229
- if a .Token .Sandbox {
230
- URL = HostSandBox + PathTransactionInfo
231
- }
235
+ URL := a .host + PathTransactionInfo
232
236
URL = strings .Replace (URL , "{transactionId}" , transactionId , - 1 )
233
237
234
238
statusCode , body , err := a .Do (ctx , http .MethodGet , URL , nil )
@@ -250,10 +254,7 @@ func (a *StoreClient) GetTransactionInfo(ctx context.Context, transactionId stri
250
254
251
255
// GetRefundHistory https://developer.apple.com/documentation/appstoreserverapi/get_refund_history
252
256
func (a * StoreClient ) GetRefundHistory (ctx context.Context , originalTransactionId string ) (responses []* RefundLookupResponse , err error ) {
253
- baseURL := HostProduction + PathRefundHistory
254
- if a .Token .Sandbox {
255
- baseURL = HostSandBox + PathRefundHistory
256
- }
257
+ baseURL := a .host + PathRefundHistory
257
258
baseURL = strings .Replace (baseURL , "{originalTransactionId}" , originalTransactionId , - 1 )
258
259
259
260
URL := baseURL
@@ -292,10 +293,7 @@ func (a *StoreClient) GetRefundHistory(ctx context.Context, originalTransactionI
292
293
293
294
// SendConsumptionInfo https://developer.apple.com/documentation/appstoreserverapi/send_consumption_information
294
295
func (a * StoreClient ) SendConsumptionInfo (ctx context.Context , originalTransactionId string , body ConsumptionRequestBody ) (statusCode int , err error ) {
295
- URL := HostProduction + PathConsumptionInfo
296
- if a .Token .Sandbox {
297
- URL = HostSandBox + PathConsumptionInfo
298
- }
296
+ URL := a .host + PathConsumptionInfo
299
297
URL = strings .Replace (URL , "{originalTransactionId}" , originalTransactionId , - 1 )
300
298
301
299
bodyBuf := new (bytes.Buffer )
@@ -313,10 +311,7 @@ func (a *StoreClient) SendConsumptionInfo(ctx context.Context, originalTransacti
313
311
314
312
// ExtendSubscriptionRenewalDate https://developer.apple.com/documentation/appstoreserverapi/extend_a_subscription_renewal_date
315
313
func (a * StoreClient ) ExtendSubscriptionRenewalDate (ctx context.Context , originalTransactionId string , body ExtendRenewalDateRequest ) (statusCode int , err error ) {
316
- URL := HostProduction + PathExtendSubscriptionRenewalDate
317
- if a .Token .Sandbox {
318
- URL = HostSandBox + PathExtendSubscriptionRenewalDate
319
- }
314
+ URL := a .host + PathExtendSubscriptionRenewalDate
320
315
URL = strings .Replace (URL , "{originalTransactionId}" , originalTransactionId , - 1 )
321
316
322
317
bodyBuf := new (bytes.Buffer )
@@ -334,10 +329,7 @@ func (a *StoreClient) ExtendSubscriptionRenewalDate(ctx context.Context, origina
334
329
335
330
// ExtendSubscriptionRenewalDateForAll https://developer.apple.com/documentation/appstoreserverapi/extend_subscription_renewal_dates_for_all_active_subscribers
336
331
func (a * StoreClient ) ExtendSubscriptionRenewalDateForAll (ctx context.Context , body MassExtendRenewalDateRequest ) (statusCode int , err error ) {
337
- URL := HostProduction + PathExtendSubscriptionRenewalDateForAll
338
- if a .Token .Sandbox {
339
- URL = HostSandBox + PathExtendSubscriptionRenewalDateForAll
340
- }
332
+ URL := a .host + PathExtendSubscriptionRenewalDateForAll
341
333
342
334
bodyBuf := new (bytes.Buffer )
343
335
err = json .NewEncoder (bodyBuf ).Encode (body )
@@ -354,10 +346,7 @@ func (a *StoreClient) ExtendSubscriptionRenewalDateForAll(ctx context.Context, b
354
346
355
347
// GetSubscriptionRenewalDataStatus https://developer.apple.com/documentation/appstoreserverapi/get_status_of_subscription_renewal_date_extensions
356
348
func (a * StoreClient ) GetSubscriptionRenewalDataStatus (ctx context.Context , productId , requestIdentifier string ) (statusCode int , rsp * MassExtendRenewalDateStatusResponse , err error ) {
357
- URL := HostProduction + PathGetStatusOfSubscriptionRenewalDate
358
- if a .Token .Sandbox {
359
- URL = HostSandBox + PathGetStatusOfSubscriptionRenewalDate
360
- }
349
+ URL := a .host + PathGetStatusOfSubscriptionRenewalDate
361
350
URL = strings .Replace (URL , "{productId}" , productId , - 1 )
362
351
URL = strings .Replace (URL , "{requestIdentifier}" , requestIdentifier , - 1 )
363
352
@@ -404,12 +393,7 @@ func (a *StoreClient) GetAllNotificationHistory(ctx context.Context, body Notifi
404
393
// GetNotificationHistory https://developer.apple.com/documentation/appstoreserverapi/get_notification_history
405
394
// Note: Notification history is available starting on June 6, 2022. Use a startDate of June 6, 2022 or later in your request.
406
395
func (a * StoreClient ) GetNotificationHistory (ctx context.Context , body NotificationHistoryRequest , paginationToken string ) (rsp * NotificationHistoryResponses , err error ) {
407
- baseURL := HostProduction + PathGetNotificationHistory
408
- if a .Token .Sandbox {
409
- baseURL = HostSandBox + PathGetNotificationHistory
410
- }
411
-
412
- URL := baseURL
396
+ URL := a .host + PathGetNotificationHistory
413
397
if paginationToken != "" {
414
398
query := url.Values {}
415
399
query .Set ("paginationToken" , paginationToken )
@@ -440,20 +424,14 @@ func (a *StoreClient) GetNotificationHistory(ctx context.Context, body Notificat
440
424
441
425
// SendRequestTestNotification https://developer.apple.com/documentation/appstoreserverapi/request_a_test_notification
442
426
func (a * StoreClient ) SendRequestTestNotification (ctx context.Context ) (int , []byte , error ) {
443
- URL := HostProduction + PathRequestTestNotification
444
- if a .Token .Sandbox {
445
- URL = HostSandBox + PathRequestTestNotification
446
- }
427
+ URL := a .host + PathRequestTestNotification
447
428
448
429
return a .Do (ctx , http .MethodPost , URL , nil )
449
430
}
450
431
451
432
// GetTestNotificationStatus https://developer.apple.com/documentation/appstoreserverapi/get_test_notification_status
452
433
func (a * StoreClient ) GetTestNotificationStatus (ctx context.Context , testNotificationToken string ) (int , []byte , error ) {
453
- URL := HostProduction + PathGetTestNotificationStatus
454
- if a .Token .Sandbox {
455
- URL = HostSandBox + PathGetTestNotificationStatus
456
- }
434
+ URL := a .host + PathGetTestNotificationStatus
457
435
URL = strings .Replace (URL , "{testNotificationToken}" , testNotificationToken , - 1 )
458
436
459
437
return a .Do (ctx , http .MethodGet , URL , nil )
0 commit comments