diff --git a/billing_agreements.go b/billing_agreements.go index 2fa1b3b..a2afc55 100644 --- a/billing_agreements.go +++ b/billing_agreements.go @@ -7,12 +7,25 @@ import ( // CreatePaypalBillingAgreementToken - Use this call to create a billing agreement token // Endpoint: POST /v1/billing-agreements/agreement-tokens +// Deprecated: use CreateBillingAgreementToken instead func (c *Client) CreatePaypalBillingAgreementToken( ctx context.Context, description *string, shippingAddress *ShippingAddress, payer *Payer, 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) { type createBARequest struct { Description *string `json:"description,omitempty"` @@ -41,9 +54,19 @@ func (c *Client) CreatePaypalBillingAgreementToken( // CreatePaypalBillingAgreementFromToken - Use this call to create a billing agreement // Endpoint: POST /v1/billing-agreements/agreements +// Deprecated: use CreateBillingAgreementFromToken instead func (c *Client) CreatePaypalBillingAgreementFromToken( ctx context.Context, 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) { type createBARequest struct { TokenID string `json:"token_id"` @@ -66,3 +89,27 @@ func (c *Client) CreatePaypalBillingAgreementFromToken( 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 +} diff --git a/unit_test.go b/unit_test.go index deaa067..589dfcf 100644 --- a/unit_test.go +++ b/unit_test.go @@ -3,12 +3,15 @@ package paypal import ( "context" "encoding/json" + "fmt" "io/ioutil" "net/http" "net/http/httptest" "testing" ) +var testBillingAgreementID = "BillingAgreementID" + type webprofileTestServer struct { t *testing.T } @@ -494,6 +497,11 @@ func (ts *webprofileTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request 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) { @@ -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}) defer ts.Close() @@ -883,7 +891,7 @@ func TestCreatePaypalBillingAgreementToken(t *testing.T) { c, _ := NewClient("foo", "bar", ts.URL) description := "name A" - _, err := c.CreatePaypalBillingAgreementToken( + _, err := c.CreateBillingAgreementToken( context.Background(), &description, &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}) defer ts.Close() 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 { t.Fatal(err)