Skip to content

Commit 9b85132

Browse files
authored
Merge pull request #195 from richzw/master
feat(ios): mark ios test receipt through ErrReceiptIsForTest
2 parents c4aaa88 + 01244fa commit 9b85132

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

appstore/validator.go

+25-14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
// IAPClient is an interface to call validation API in App Store
2626
type IAPClient interface {
2727
Verify(ctx context.Context, reqBody IAPRequest, resp interface{}) error
28+
VerifyWithStatus(ctx context.Context, reqBody IAPRequest, resp interface{}) (int, error)
2829
}
2930

3031
// Client implements IAPClient
@@ -109,69 +110,79 @@ func NewWithClient(client *http.Client) *Client {
109110

110111
// Verify sends receipts and gets validation result
111112
func (c *Client) Verify(ctx context.Context, reqBody IAPRequest, result interface{}) error {
113+
_, err := c.verify(ctx, reqBody, result)
114+
return err
115+
}
116+
117+
// VerifyWithStatus sends receipts and gets validation result with status code
118+
func (c *Client) VerifyWithStatus(ctx context.Context, reqBody IAPRequest, result interface{}) (int, error) {
119+
return c.verify(ctx, reqBody, result)
120+
}
121+
122+
func (c *Client) verify(ctx context.Context, reqBody IAPRequest, result interface{}) (int, error) {
112123
b := new(bytes.Buffer)
113124
if err := json.NewEncoder(b).Encode(reqBody); err != nil {
114-
return err
125+
return 0, err
115126
}
116127

117128
req, err := http.NewRequest("POST", c.ProductionURL, b)
118129
if err != nil {
119-
return err
130+
return 0, err
120131
}
121132
req.Header.Set("Content-Type", ContentType)
122133
req = req.WithContext(ctx)
123134
resp, err := c.httpCli.Do(req)
124135
if err != nil {
125-
return err
136+
return 0, err
126137
}
127138
defer resp.Body.Close()
128139
if resp.StatusCode >= 500 {
129-
return fmt.Errorf("Received http status code %d from the App Store: %w", resp.StatusCode, ErrAppStoreServer)
140+
return 0, fmt.Errorf("Received http status code %d from the App Store: %w", resp.StatusCode, ErrAppStoreServer)
130141
}
131142
return c.parseResponse(resp, result, ctx, reqBody)
132143
}
133144

134-
func (c *Client) parseResponse(resp *http.Response, result interface{}, ctx context.Context, reqBody IAPRequest) error {
145+
func (c *Client) parseResponse(resp *http.Response, result interface{}, ctx context.Context, reqBody IAPRequest) (int, error) {
135146
// Read the body now so that we can unmarshal it twice
136147
buf, err := ioutil.ReadAll(resp.Body)
137148
if err != nil {
138-
return err
149+
return 0, err
139150
}
140151

141152
err = json.Unmarshal(buf, &result)
142153
if err != nil {
143-
return err
154+
return 0, err
144155
}
145156

146157
// https://developer.apple.com/library/content/technotes/tn2413/_index.html#//apple_ref/doc/uid/DTS40016228-CH1-RECEIPTURL
147158
var r StatusResponse
148159
err = json.Unmarshal(buf, &r)
149160
if err != nil {
150-
return err
161+
return 0, err
151162
}
152163
if r.Status == 21007 {
153164
b := new(bytes.Buffer)
154165
if err := json.NewEncoder(b).Encode(reqBody); err != nil {
155-
return err
166+
return 0, err
156167
}
157168

158169
req, err := http.NewRequest("POST", c.SandboxURL, b)
159170
if err != nil {
160-
return err
171+
return 0, err
161172
}
162173
req.Header.Set("Content-Type", ContentType)
163174
req = req.WithContext(ctx)
164175
resp, err := c.httpCli.Do(req)
165176
if err != nil {
166-
return err
177+
return 0, err
167178
}
168179
defer resp.Body.Close()
169180
if resp.StatusCode >= 500 {
170-
return fmt.Errorf("Received http status code %d from the App Store Sandbox: %w", resp.StatusCode, ErrAppStoreServer)
181+
return resp.StatusCode, fmt.Errorf("Received http status code %d from the App Store Sandbox: %w", resp.StatusCode, ErrAppStoreServer)
171182
}
172183

173-
return json.NewDecoder(resp.Body).Decode(result)
184+
return r.Status, json.NewDecoder(resp.Body).Decode(result)
174185
}
175186

176-
return nil
187+
return r.Status, nil
177188
}

appstore/validator_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func TestCannotReadBody(t *testing.T) {
286286
testResponse := http.Response{Body: ioutil.NopCloser(errReader(0))}
287287

288288
ctx := context.Background()
289-
if client.parseResponse(&testResponse, IAPResponse{}, ctx, IAPRequest{}) == nil {
289+
if _, err := client.parseResponse(&testResponse, IAPResponse{}, ctx, IAPRequest{}); err == nil {
290290
t.Errorf("expected redirectToSandbox to fail to read the body")
291291
}
292292
}
@@ -296,7 +296,7 @@ func TestCannotUnmarshalBody(t *testing.T) {
296296
testResponse := http.Response{Body: ioutil.NopCloser(strings.NewReader(`{"status": true}`))}
297297

298298
ctx := context.Background()
299-
if client.parseResponse(&testResponse, StatusResponse{}, ctx, IAPRequest{}) == nil {
299+
if _, err := client.parseResponse(&testResponse, StatusResponse{}, ctx, IAPRequest{}); err == nil {
300300
t.Errorf("expected redirectToSandbox to fail to unmarshal the data")
301301
}
302302
}

0 commit comments

Comments
 (0)