Skip to content

Commit

Permalink
Added cancellation method for subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
jun06t committed Jun 16, 2015
1 parent 51163ff commit 764aa49
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
34 changes: 29 additions & 5 deletions playstore/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
)

const (
scope = "https://www.googleapis.com/auth/androidpublisher"

defaultTimeout = time.Second * 5
)

Expand Down Expand Up @@ -42,12 +40,12 @@ func New(jsonKey []byte) (Client, error) {
Timeout: timeout,
})

conf, err := google.JWTConfigFromJSON(jsonKey, scope)
conf, err := google.JWTConfigFromJSON(jsonKey, androidpublisher.AndroidpublisherScope)

return Client{conf.Client(ctx)}, err
}

// VerifySubscription Verifies subscription status
// VerifySubscription verifies subscription status
func (c *Client) VerifySubscription(
packageName string,
subscriptionID string,
Expand All @@ -64,7 +62,7 @@ func (c *Client) VerifySubscription(
return result, err
}

// VerifyProduct Verifies product status
// VerifyProduct verifies product status
func (c *Client) VerifyProduct(
packageName string,
productID string,
Expand All @@ -80,3 +78,29 @@ func (c *Client) VerifyProduct(

return result, err
}

// CancelSubscription cancels a user's subscription purchase.
func (c *Client) CancelSubscription(packageName string, subscriptionID string, token string) error {
service, err := androidpublisher.New(c.httpClient)
if err != nil {
return err
}

ps := androidpublisher.NewPurchasesSubscriptionsService(service)
err = ps.Cancel(packageName, subscriptionID, token).Do()

return err
}

// RefundSubscription refunds a user's subscription purchase.
func (c *Client) RefundSubscription(packageName string, subscriptionID string, token string) error {
service, err := androidpublisher.New(c.httpClient)
if err != nil {
return err
}

ps := androidpublisher.NewPurchasesSubscriptionsService(service)
err = ps.Refund(packageName, subscriptionID, token).Do()

return err
}
44 changes: 44 additions & 0 deletions playstore/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,47 @@ func TestVerifyProductAndroidPublisherError(t *testing.T) {
t.Errorf("got %v\nwant %v", actual, expected)
}
}

func TestCancelSubscription(t *testing.T) {
// Exception scenario
client := Client{nil}
expected := errors.New("client is nil")
actual := client.CancelSubscription("package", "productID", "purchaseToken")

if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
}

jsonKey, _ := json.Marshal(testJSON)
client, _ = New(jsonKey)
expectedStr := "Post https://www.googleapis.com/androidpublisher/v2/applications/package/purchases/subscriptions/productID/tokens/purchaseToken:cancel?alt=json: oauth2: cannot fetch token: 400 Bad Request\nResponse: {\n \"error\" : \"invalid_grant\"\n}"
actual = client.CancelSubscription("package", "productID", "purchaseToken")

if actual.Error() != expectedStr {
t.Errorf("got %v\nwant %v", actual, expectedStr)
}

// TODO Normal scenario
}

func TestRefundSubscription(t *testing.T) {
// Exception scenario
client := Client{nil}
expected := errors.New("client is nil")
actual := client.RefundSubscription("package", "productID", "purchaseToken")

if !reflect.DeepEqual(actual, expected) {
t.Errorf("got %v\nwant %v", actual, expected)
}

jsonKey, _ := json.Marshal(testJSON)
client, _ = New(jsonKey)
expectedStr := "Post https://www.googleapis.com/androidpublisher/v2/applications/package/purchases/subscriptions/productID/tokens/purchaseToken:refund?alt=json: oauth2: cannot fetch token: 400 Bad Request\nResponse: {\n \"error\" : \"invalid_grant\"\n}"
actual = client.RefundSubscription("package", "productID", "purchaseToken")

if actual.Error() != expectedStr {
t.Errorf("got %v\nwant %v", actual, expectedStr)
}

// TODO Normal scenario
}

0 comments on commit 764aa49

Please sign in to comment.