forked from Gurpartap/storekit-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
212 lines (184 loc) · 5.51 KB
/
client.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
package storekit
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"unicode"
"github.com/pkg/errors"
)
const (
sandboxReceiptVerificationURL = "https://sandbox.itunes.apple.com/verifyReceipt"
productionReceiptVerificationURL = "https://buy.itunes.apple.com/verifyReceipt"
)
type client struct {
verificationURL string
autofixEnvironment bool
}
// NewVerificationClient defaults to production verification URL with auto fix
// enabled.
//
// Auto fix automatically handles the incompatible receipt environment error. It
// subsequently gets disabled after the first attempt to avoid unexpected
// looping.
func NewVerificationClient() *client {
return &client{
verificationURL: productionReceiptVerificationURL,
autofixEnvironment: true,
}
}
// OnProductionEnv sets the client to use sandbox URL for verification.
func (c *client) OnSandboxEnv() *client {
c.verificationURL = sandboxReceiptVerificationURL
return c
}
// OnProductionEnv sets the client to use production URL for verification.
func (c *client) OnProductionEnv() *client {
c.verificationURL = productionReceiptVerificationURL
return c
}
// WithoutEnvAutoFix disables automatic handling of incompatible receipt
// environment error.
func (c *client) WithoutEnvAutoFix() *client {
c.autofixEnvironment = false
return c
}
func (c *client) Verify(ctx context.Context, req *ReceiptRequest) ([]byte, *ReceiptResponse, error) {
post:
body, err := c.post(ctx, req)
if err != nil {
return nil, nil, err
}
resp := &ReceiptResponse{}
err = json.Unmarshal(
bytes.Map(func(r rune) rune {
if unicode.IsControl(r) {
return -1
}
return r
}, body),
resp,
)
if err != nil {
return nil, nil, errors.Wrap(err, "could not unmarshal app store response")
}
if c.autofixEnvironment {
// Auto fix but only once.
c.autofixEnvironment = false
switch resp.Status {
case ReceiptResponseStatusSandboxReceiptSentToProduction:
// On a 21007 status, retry the request in the sandbox environment (only if the
// current environment is production – to avoid unexpected loop).
//
// These are receipts from Apple review team.
if c.isProduction() {
c.verificationURL = sandboxReceiptVerificationURL
goto post
}
case ReceiptResponseStatusProductionReceiptSentToSandbox:
// On a 21008 status, retry the request in the production environment (only if
// the current environment is sandbox – to avoid unexpected loop).
if c.isSandbox() {
c.verificationURL = productionReceiptVerificationURL
goto post
}
default:
// TODO: Retry at least once when an App Store internal error occurs here:
// if resp.Status >= 21100 && resp.Status <= 21199 {
// if resp.IsRetryable {
// goto post
// }
// }
break
}
}
return body, resp, nil
}
func (c *client) VerifyRaw(ctx context.Context, req *ReceiptRequestRaw) ([]byte, *ReceiptResponse, error) {
post:
body, err := c.post(ctx, req)
if err != nil {
return nil, nil, err
}
resp := &ReceiptResponse{}
err = json.Unmarshal(
bytes.Map(func(r rune) rune {
if unicode.IsControl(r) {
return -1
}
return r
}, body),
resp,
)
if err != nil {
return nil, nil, errors.Wrap(err, "could not unmarshal app store response")
}
if c.autofixEnvironment {
// Auto fix but only once.
c.autofixEnvironment = false
switch resp.Status {
case ReceiptResponseStatusSandboxReceiptSentToProduction:
// On a 21007 status, retry the request in the sandbox environment (only if the
// current environment is production – to avoid unexpected loop).
//
// These are receipts from Apple review team.
if c.isProduction() {
c.verificationURL = sandboxReceiptVerificationURL
goto post
}
case ReceiptResponseStatusProductionReceiptSentToSandbox:
// On a 21008 status, retry the request in the production environment (only if
// the current environment is sandbox – to avoid unexpected loop).
if c.isSandbox() {
c.verificationURL = productionReceiptVerificationURL
goto post
}
default:
// TODO: Retry at least once when an App Store internal error occurs here:
// if resp.Status >= 21100 && resp.Status <= 21199 {
// if resp.IsRetryable {
// goto post
// }
// }
break
}
}
return body, resp, nil
}
func (c *client) post(ctx context.Context, receiptRequest interface{}) ([]byte, error) {
// Prepare request:
reqJSON, err := json.Marshal(receiptRequest)
if err != nil {
return nil, errors.Wrap(err, "could not marshal receipt request")
}
// Dial the App Store server:
buf := bytes.NewReader(reqJSON)
req, err := http.NewRequest("POST", c.verificationURL, buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req = req.WithContext(ctx)
r, err := http.DefaultClient.Do(req)
if err != nil {
// TODO: Handle this error (and probably retry at least once):
// Post https://sandbox.itunes.apple.com/verifyReceipt: read tcp 10.1.11.101:36372->17.154.66.159:443: read: connection reset by peer
return nil, errors.Wrap(err, "could not connect to app store server")
}
if r.StatusCode != http.StatusOK {
return nil, errors.New("app store http error (" + r.Status + ")")
}
// Parse response:
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, errors.Wrap(err, "could not read app store response")
}
return body, nil
}
func (c *client) isSandbox() bool {
return c.verificationURL == sandboxReceiptVerificationURL
}
func (c *client) isProduction() bool {
return c.verificationURL == productionReceiptVerificationURL
}