Merge pull request #12 from jun06t/cancel

Added cancellation method for subscription
This commit is contained in:
Junpei Tsuji
2015-06-16 13:40:59 +09:00
2 changed files with 73 additions and 5 deletions

View File

@@ -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
}

View File

@@ -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
}