Use interface argument for IAP response instead of the library defined struct

This commit is contained in:
Jumpei Tsuji
2016-08-05 11:43:55 +09:00
parent e94030ddae
commit e9d5da1f8f
4 changed files with 17 additions and 11 deletions

View File

@@ -73,6 +73,10 @@ type (
}
// 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 {
Status int `json:"status"`
Environment string `json:"environment"`

View File

@@ -98,8 +98,7 @@ func NewWithConfig(config Config) Client {
}
// Verify sends receipts and gets validation result
func (c *Client) Verify(req IAPRequest) (IAPResponse, error) {
result := IAPResponse{}
func (c *Client) Verify(req IAPRequest, result interface{}) error {
_, body, errs := gorequest.New().
Post(c.URL).
Send(req).
@@ -107,10 +106,10 @@ func (c *Client) Verify(req IAPRequest) (IAPResponse, error) {
End()
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
}

View File

@@ -134,15 +134,16 @@ func TestNewWithConfigTimeout(t *testing.T) {
func TestVerify(t *testing.T) {
client := New()
expected := IAPResponse{
expected := &IAPResponse{
Status: 21002,
}
req := IAPRequest{
ReceiptData: "dummy data",
}
actual, _ := client.Verify(req)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
result := &IAPResponse{}
client.Verify(req, result)
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("")
_, actual := client.Verify(req)
result := &IAPResponse{}
actual := client.Verify(req, result)
if !reflect.DeepEqual(reflect.TypeOf(actual), reflect.TypeOf(expected)) {
t.Errorf("got %v\nwant %v", actual, expected)
}