Use interface argument for IAP response instead of the library defined struct
This commit is contained in:
@@ -43,7 +43,8 @@ func main() {
|
|||||||
req := appstore.IAPRequest{
|
req := appstore.IAPRequest{
|
||||||
ReceiptData: "your receipt data encoded by base64",
|
ReceiptData: "your receipt data encoded by base64",
|
||||||
}
|
}
|
||||||
resp, err := client.Verify(req)
|
resp := &IAPResponse{}
|
||||||
|
err := client.Verify(req, resp)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -73,6 +73,10 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The IAPResponse type has the response properties
|
// The IAPResponse type has the response properties
|
||||||
|
// We defined each field by the current IAP response, but some fields are not mentioned
|
||||||
|
// in the following Apple's document;
|
||||||
|
// https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html
|
||||||
|
// If you get other types or fileds from the IAP response, you should use the struct you defined.
|
||||||
IAPResponse struct {
|
IAPResponse struct {
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
Environment string `json:"environment"`
|
Environment string `json:"environment"`
|
||||||
|
|||||||
@@ -98,8 +98,7 @@ func NewWithConfig(config Config) Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify sends receipts and gets validation result
|
// Verify sends receipts and gets validation result
|
||||||
func (c *Client) Verify(req IAPRequest) (IAPResponse, error) {
|
func (c *Client) Verify(req IAPRequest, result interface{}) error {
|
||||||
result := IAPResponse{}
|
|
||||||
_, body, errs := gorequest.New().
|
_, body, errs := gorequest.New().
|
||||||
Post(c.URL).
|
Post(c.URL).
|
||||||
Send(req).
|
Send(req).
|
||||||
@@ -107,10 +106,10 @@ func (c *Client) Verify(req IAPRequest) (IAPResponse, error) {
|
|||||||
End()
|
End()
|
||||||
|
|
||||||
if errs != nil {
|
if errs != nil {
|
||||||
return result, fmt.Errorf("%v", errs)
|
return fmt.Errorf("%v", errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := json.NewDecoder(strings.NewReader(body)).Decode(&result)
|
err := json.NewDecoder(strings.NewReader(body)).Decode(result)
|
||||||
|
|
||||||
return result, err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,15 +134,16 @@ func TestNewWithConfigTimeout(t *testing.T) {
|
|||||||
func TestVerify(t *testing.T) {
|
func TestVerify(t *testing.T) {
|
||||||
client := New()
|
client := New()
|
||||||
|
|
||||||
expected := IAPResponse{
|
expected := &IAPResponse{
|
||||||
Status: 21002,
|
Status: 21002,
|
||||||
}
|
}
|
||||||
req := IAPRequest{
|
req := IAPRequest{
|
||||||
ReceiptData: "dummy data",
|
ReceiptData: "dummy data",
|
||||||
}
|
}
|
||||||
actual, _ := client.Verify(req)
|
result := &IAPResponse{}
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
client.Verify(req, result)
|
||||||
t.Errorf("got %v\nwant %v", actual, expected)
|
if !reflect.DeepEqual(result, expected) {
|
||||||
|
t.Errorf("got %v\nwant %v", result, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +157,8 @@ func TestVerifyTimeout(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expected := errors.New("")
|
expected := errors.New("")
|
||||||
_, actual := client.Verify(req)
|
result := &IAPResponse{}
|
||||||
|
actual := client.Verify(req, result)
|
||||||
if !reflect.DeepEqual(reflect.TypeOf(actual), reflect.TypeOf(expected)) {
|
if !reflect.DeepEqual(reflect.TypeOf(actual), reflect.TypeOf(expected)) {
|
||||||
t.Errorf("got %v\nwant %v", actual, expected)
|
t.Errorf("got %v\nwant %v", actual, expected)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user