diff --git a/playstore/validator.go b/playstore/validator.go index 5f0d6ae..05983a9 100644 --- a/playstore/validator.go +++ b/playstore/validator.go @@ -92,7 +92,8 @@ func (c *Client) CancelSubscription(packageName string, subscriptionID string, t return err } -// RefundSubscription refunds a user's subscription purchase. +// RefundSubscription refunds a user's subscription purchase, but the subscription remains valid +// until its expiration time and it will continue to recur. func (c *Client) RefundSubscription(packageName string, subscriptionID string, token string) error { service, err := androidpublisher.New(c.httpClient) if err != nil { @@ -104,3 +105,17 @@ func (c *Client) RefundSubscription(packageName string, subscriptionID string, t return err } + +// RevokeSubscription refunds and immediately revokes a user's subscription purchase. +// Access to the subscription will be terminated immediately and it will stop recurring. +func (c *Client) RevokeSubscription(packageName string, subscriptionID string, token string) error { + service, err := androidpublisher.New(c.httpClient) + if err != nil { + return err + } + + ps := androidpublisher.NewPurchasesSubscriptionsService(service) + err = ps.Revoke(packageName, subscriptionID, token).Do() + + return err +} diff --git a/playstore/validator_test.go b/playstore/validator_test.go index 86afee0..e8e7453 100644 --- a/playstore/validator_test.go +++ b/playstore/validator_test.go @@ -148,3 +148,25 @@ func TestRefundSubscription(t *testing.T) { // TODO Normal scenario } + +func TestRevokeSubscription(t *testing.T) { + // Exception scenario + client := Client{nil} + expected := errors.New("client is nil") + actual := client.RevokeSubscription("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:revoke?alt=json: oauth2: cannot fetch token: 400 Bad Request\nResponse: {\n \"error\" : \"invalid_grant\"\n}" + actual = client.RevokeSubscription("package", "productID", "purchaseToken") + + if actual.Error() != expectedStr { + t.Errorf("got %v\nwant %v", actual, expectedStr) + } + + // TODO Normal scenario +}