Add cancel billing agreement function (#222)

This commit is contained in:
rohanr-bolt 2021-08-19 13:29:00 -04:00 committed by GitHub
parent d5cba40cd6
commit 06067823c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 4 deletions

View File

@ -7,12 +7,25 @@ import (
// CreatePaypalBillingAgreementToken - Use this call to create a billing agreement token // CreatePaypalBillingAgreementToken - Use this call to create a billing agreement token
// Endpoint: POST /v1/billing-agreements/agreement-tokens // Endpoint: POST /v1/billing-agreements/agreement-tokens
// Deprecated: use CreateBillingAgreementToken instead
func (c *Client) CreatePaypalBillingAgreementToken( func (c *Client) CreatePaypalBillingAgreementToken(
ctx context.Context, ctx context.Context,
description *string, description *string,
shippingAddress *ShippingAddress, shippingAddress *ShippingAddress,
payer *Payer, payer *Payer,
plan *BillingPlan, plan *BillingPlan,
) (*BillingAgreementToken, error) {
return c.CreateBillingAgreementToken(ctx, description, shippingAddress, payer, plan)
}
// CreateBillingAgreementToken - Use this call to create a billing agreement token
// Endpoint: POST /v1/billing-agreements/agreement-tokens
func (c *Client) CreateBillingAgreementToken(
ctx context.Context,
description *string,
shippingAddress *ShippingAddress,
payer *Payer,
plan *BillingPlan,
) (*BillingAgreementToken, error) { ) (*BillingAgreementToken, error) {
type createBARequest struct { type createBARequest struct {
Description *string `json:"description,omitempty"` Description *string `json:"description,omitempty"`
@ -41,9 +54,19 @@ func (c *Client) CreatePaypalBillingAgreementToken(
// CreatePaypalBillingAgreementFromToken - Use this call to create a billing agreement // CreatePaypalBillingAgreementFromToken - Use this call to create a billing agreement
// Endpoint: POST /v1/billing-agreements/agreements // Endpoint: POST /v1/billing-agreements/agreements
// Deprecated: use CreateBillingAgreementFromToken instead
func (c *Client) CreatePaypalBillingAgreementFromToken( func (c *Client) CreatePaypalBillingAgreementFromToken(
ctx context.Context, ctx context.Context,
tokenID string, tokenID string,
) (*BillingAgreementFromToken, error) {
return c.CreateBillingAgreementFromToken(ctx, tokenID)
}
// CreateBillingAgreementFromToken - Use this call to create a billing agreement
// Endpoint: POST /v1/billing-agreements/agreements
func (c *Client) CreateBillingAgreementFromToken(
ctx context.Context,
tokenID string,
) (*BillingAgreementFromToken, error) { ) (*BillingAgreementFromToken, error) {
type createBARequest struct { type createBARequest struct {
TokenID string `json:"token_id"` TokenID string `json:"token_id"`
@ -66,3 +89,27 @@ func (c *Client) CreatePaypalBillingAgreementFromToken(
return billingAgreement, nil return billingAgreement, nil
} }
// CancelBillingAgreement - Use this call to cancel a billing agreement
// Endpoint: POST /v1/billing-agreements/agreements/{agreement_id}/cancel
func (c *Client) CancelBillingAgreement(
ctx context.Context,
billingAgreementID string,
) error {
type cancelBARequest struct{}
req, err := c.NewRequest(
ctx,
"POST",
fmt.Sprintf("%s%s%s%s", c.APIBase, "/v1/billing-agreements/agreements/", billingAgreementID, "/cancel"),
cancelBARequest{})
if err != nil {
return err
}
if err = c.SendWithAuth(req, nil); err != nil {
return err
}
return nil
}

View File

@ -3,12 +3,15 @@ package paypal
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
) )
var testBillingAgreementID = "BillingAgreementID"
type webprofileTestServer struct { type webprofileTestServer struct {
t *testing.T t *testing.T
} }
@ -494,6 +497,11 @@ func (ts *webprofileTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request
ts.createWithoutName(w, r) ts.createWithoutName(w, r)
} }
} }
if r.RequestURI == fmt.Sprintf("/v1/billing-agreements/agreements/%s/cancel", testBillingAgreementID) {
if r.Method == "POST" {
ts.deletevalid(w, r)
}
}
} }
func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) { func (ts *webprofileTestServer) create(w http.ResponseWriter, r *http.Request) {
@ -875,7 +883,7 @@ func TestDeleteWebProfile_invalid(t *testing.T) {
} }
func TestCreatePaypalBillingAgreementToken(t *testing.T) { func TestCreateBillingAgreementToken(t *testing.T) {
ts := httptest.NewServer(&webprofileTestServer{t: t}) ts := httptest.NewServer(&webprofileTestServer{t: t})
defer ts.Close() defer ts.Close()
@ -883,7 +891,7 @@ func TestCreatePaypalBillingAgreementToken(t *testing.T) {
c, _ := NewClient("foo", "bar", ts.URL) c, _ := NewClient("foo", "bar", ts.URL)
description := "name A" description := "name A"
_, err := c.CreatePaypalBillingAgreementToken( _, err := c.CreateBillingAgreementToken(
context.Background(), context.Background(),
&description, &description,
&ShippingAddress{RecipientName: "Name", Type: "Type", Line1: "Line1", Line2: "Line2"}, &ShippingAddress{RecipientName: "Name", Type: "Type", Line1: "Line1", Line2: "Line2"},
@ -896,14 +904,28 @@ func TestCreatePaypalBillingAgreementToken(t *testing.T) {
} }
func TestCreatePaypalBillingAgreementFromToken(t *testing.T) { func TestCreateBillingAgreementFromToken(t *testing.T) {
ts := httptest.NewServer(&webprofileTestServer{t: t}) ts := httptest.NewServer(&webprofileTestServer{t: t})
defer ts.Close() defer ts.Close()
c, _ := NewClient("foo", "bar", ts.URL) c, _ := NewClient("foo", "bar", ts.URL)
_, err := c.CreatePaypalBillingAgreementFromToken(context.Background(), "BillingAgreementToken") _, err := c.CreateBillingAgreementFromToken(context.Background(), "BillingAgreementToken")
if err != nil {
t.Fatal(err)
}
}
func TestCancelBillingAgreement(t *testing.T) {
ts := httptest.NewServer(&webprofileTestServer{t: t})
defer ts.Close()
c, _ := NewClient("foo", "bar", ts.URL)
err := c.CancelBillingAgreement(context.Background(), testBillingAgreementID)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)