diff --git a/playstore/validator.go b/playstore/validator.go index d56490d..5f0d6ae 100644 --- a/playstore/validator.go +++ b/playstore/validator.go @@ -11,8 +11,6 @@ import ( ) const ( - scope = "https://www.googleapis.com/auth/androidpublisher" - defaultTimeout = time.Second * 5 ) @@ -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, @@ -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, @@ -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 +} diff --git a/playstore/validator_test.go b/playstore/validator_test.go index f33dba9..86afee0 100644 --- a/playstore/validator_test.go +++ b/playstore/validator_test.go @@ -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 +}