-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
251 lines (229 loc) · 6.92 KB
/
client.go
File metadata and controls
251 lines (229 loc) · 6.92 KB
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
package appstoreserverapi
import (
"encoding/json"
"errors"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/tidwall/gjson"
"io"
"log"
"net/http"
"sync"
"time"
)
var (
ErrConfigIsNil = errors.New("config is nil")
ErrConfigInvalid = errors.New("config invalid")
ErrRequestFailed = errors.New("request failed")
)
type env string
const (
Production env = "production"
Development env = "development"
)
type Config struct {
// 发行人: 您在 App Store Connect 中的密钥页面中的发行者 ID(例如:" 57246542-96fe-1a63-e053-0824d011072a")
// Issuer: Your issuer ID from the Keys page in App Store Connect (Ex: "57246542-96fe-1a63-e053-0824d011072a")
Iss string
// 秘钥:您在 App Store Connect 中的私钥 ID(例如2X9R4HXF34:)
// Key ID: Your private key ID from App Store Connect (Ex: 2X9R4HXF34)
Kid string
// 应用的BundleID(例如:“com.example.testbundleid2021”)
// Bundle ID: Your app’s bundle ID (Ex: “com.example.testbundleid2021”)
Bid string
// 签名的秘钥
// sign private key, eg:
/*
-----BEGIN PRIVATE KEY-----
MIGTAg23kjjh2h3uhuhfduhJHAKJ23JASjhaskjj234hjHKJHS31hkjj
-----END PRIVATE KEY-----`
*/
Pk string
// 受众:appstoreconnect-v1
// Audience: appstoreconnect-v1
Aud string
// 有效期:默认是10分钟
ExpiryIn time.Duration
// 环境:默认正式环境
Evn env
// 重试次数:默认10次
TryCount uint
exp time.Time
}
type Client interface {
// ApiGetAllSubscriptionStatuses 获取所有的订阅状态
// Get All Subscription Statuses
// doc: https://developer.apple.com/documentation/appstoreserverapi/get_all_subscription_statuses
ApiGetAllSubscriptionStatuses(transactionId string) (*StatusResponse, error)
// ApiLookUpOrderId 查找订单 ID
// Look Up Order ID
// doc: https://developer.apple.com/documentation/appstoreserverapi/look_up_order_id
ApiLookUpOrderId(orderId string) (*OrderLookupResponse, error)
// ApiGetTransactionHistory 获取历史交易记录
// Get Transaction History
// doc: https://developer.apple.com/documentation/appstoreserverapi/get_transaction_history
// desc: true then signedTransactions order by webOrderLineItemId desc
ApiGetTransactionHistory(transactionId string, desc bool) (*HistoryResponse, error)
// ApiGetRefundHistory 获取退款历史
// Get Refund History
// doc: https://developer.apple.com/documentation/appstoreserverapi/get_refund_history
// desc: true then signedTransactions order by webOrderLineItemId desc
ApiGetRefundHistory(transactionId string, desc bool) (*RefundLookupResponse, error)
// ApiExtendAsubscriptionRenewalDate 延长订阅续订日期
// Extend a Subscription Renewal Date
// doc: https://developer.apple.com/documentation/appstoreserverapi/extend_a_subscription_renewal_date
ApiExtendAsubscriptionRenewalDate(transactionId string, req ExtendRenewalDateRequest) (*ExtendRenewalDateResponse, error)
// ApiSendConsumptionInformation 发送消费信息
// Send Consumption Information
// doc: https://developer.apple.com/documentation/appstoreserverapi/send_consumption_information
ApiSendConsumptionInformation(transactionId string, req ConsumptionRequest) error
}
type apiUrl struct {
apiGetAllSubscriptionStatusesUrl string
apiGetTransactionHistoryUrl string
apiLookupOrderIdUrl string
apiGetRefundHistoryUrl string
apiExtendASubscriptionRenewalDateUrl string
apiSendConsumptionInformationUrl string
}
type client struct {
apiUrl
bearer string
lock sync.Mutex
cfg *Config
}
func NewClient(cfg *Config) (Client, error) {
if cfg == nil {
return nil, ErrConfigIsNil
}
if cfg.Iss == "" {
return nil, ErrConfigInvalid
}
if cfg.Kid == "" {
return nil, ErrConfigInvalid
}
if cfg.Bid == "" {
return nil, ErrConfigInvalid
}
if cfg.Pk == "" {
return nil, ErrConfigInvalid
}
if cfg.ExpiryIn == 0 {
cfg.ExpiryIn = time.Minute * 10
}
if cfg.TryCount == 0 {
cfg.TryCount = 10
}
if cfg.Evn == "" {
cfg.Evn = Production
}
if cfg.Aud == "" {
cfg.Aud = "appstoreconnect-v1"
}
c := &client{
cfg: cfg,
lock: sync.Mutex{},
apiUrl: apiUrl{
apiGetAllSubscriptionStatusesUrl: productionBaseUrl + apiGetAllSubscriptionStatusesUri,
apiGetTransactionHistoryUrl: productionBaseUrl + apiGetTransactionHistoryUri,
apiLookupOrderIdUrl: productionBaseUrl + apiLookupOrderIdUri,
apiGetRefundHistoryUrl: productionBaseUrl + apiGetRefundHistoryUri,
apiExtendASubscriptionRenewalDateUrl: productionBaseUrl + apiExtendASubscriptionRenewalDateUri,
apiSendConsumptionInformationUrl: productionBaseUrl + apiSendConsumptionInformationUri,
},
}
if cfg.Evn == Development {
c.apiUrl = apiUrl{
apiGetAllSubscriptionStatusesUrl: developmentBaseUrl + apiGetAllSubscriptionStatusesUri,
apiGetTransactionHistoryUrl: developmentBaseUrl + apiGetTransactionHistoryUri,
apiLookupOrderIdUrl: developmentBaseUrl + apiLookupOrderIdUri,
apiGetRefundHistoryUrl: developmentBaseUrl + apiGetRefundHistoryUri,
apiExtendASubscriptionRenewalDateUrl: developmentBaseUrl + apiExtendASubscriptionRenewalDateUri,
apiSendConsumptionInformationUrl: developmentBaseUrl + apiSendConsumptionInformationUri,
}
}
return c, nil
}
func (c *client) GetBearer() (string, error) {
if err := c.newIfExpired(); err != nil {
return "", err
}
return c.bearer, nil
}
func (c *client) newIfExpired() error {
defer c.lock.Unlock()
c.lock.Lock()
if c.bearer == "" {
token, err := SignJwt(c.cfg)
if err != nil {
return err
}
c.bearer = token
return nil
}
// 过期的时
if time.Now().Unix() >= c.cfg.exp.Unix() {
token, err := SignJwt(c.cfg)
if err != nil {
return err
}
c.bearer = token
return nil
}
return nil
}
func (c *client) doRequest(method, url string, body io.Reader) (*gjson.Result, error) {
var err error
bearer, err := c.GetBearer()
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+bearer)
var resp *http.Response
for i := int(c.cfg.TryCount); i > 0; i-- {
resp, err = http.DefaultClient.Do(req)
if err != nil {
continue
}
b, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
appErr, ok := newAppErrorFromJson(b)
if ok {
err = appErr
if appErr.IsRetryable() {
continue
}
return nil, err
}
err = errors.New(resp.Status)
continue
}
r := gjson.ParseBytes(b)
return &r, nil
}
if err != nil {
return nil, err
}
return nil, ErrRequestFailed
}
func Parse(payload string, v interface{}) error {
token, err := jwt.ParseString(payload, jwt.WithVerify(false), jwt.WithValidate(false))
if err != nil {
log.Println(err)
return err
}
b, err := json.Marshal(token.PrivateClaims())
if err != nil {
log.Println(err)
return err
}
if v == nil {
log.Println(string(b))
return nil
}
return json.Unmarshal(b, v)
}