From ee955a582644b217d6d6ef1021d6390202c8933d Mon Sep 17 00:00:00 2001 From: imikod Date: Mon, 26 Mar 2018 18:44:29 +0300 Subject: [PATCH] fixes --- README.md | 2 ++ billing.go | 55 +++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index cc1554e..3fc4a0d 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,8 @@ * POST /v1/payments/billing-agreements * POST /v1/payments/billing-agreements/**TOKEN**/agreement-execute * POST /v1/payments/billing-agreements/**ID**/cancel + * POST /v1/payments/billing-agreements/**ID**/re-activate + * POST /v1/payments/billing-agreements/**ID**/suspend ### Missing endpoints It is possible that some endpoints are missing in this SDK Client, but you can use built-in **paypalsdk** functions to perform a request: **NewClient -> NewRequest -> SendWithAuth** diff --git a/billing.go b/billing.go index fec868e..0853f2b 100644 --- a/billing.go +++ b/billing.go @@ -16,20 +16,17 @@ func (c *Client) ActivatePlan(planID string) error { if err != nil { return err } - req.SetBasicAuth(c.ClientID, c.Secret) - req.Header.Set("Authorization", "Bearer "+c.Token.Token) return c.SendWithAuth(req, nil) } // CancelAgreement cancels a billing agreement. // Endpoint: POST /v1/payments/billing-agreements/{agreement_id}/cancel func (c *Client) CancelAgreement(agreementID string) error { - req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements/"+agreementID+"/cancel"), nil) + buf := bytes.NewBuffer([]byte(`{"note": "Canceling the profile."}`)) + req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements/"+agreementID+"/cancel"), buf) if err != nil { return err } - req.SetBasicAuth(c.ClientID, c.Secret) - req.Header.Set("Authorization", "Bearer "+c.Token.Token) err = c.SendWithAuth(req, nil) @@ -79,8 +76,6 @@ func (c *Client) DeletePlan(planID string) error { if err != nil { return err } - req.SetBasicAuth(c.ClientID, c.Secret) - req.Header.Set("Authorization", "Bearer "+c.Token.Token) return c.SendWithAuth(req, nil) } @@ -92,9 +87,6 @@ func (c *Client) ExecuteApprovedAgreement(token string) (*BillingAgreement, erro return &BillingAgreement{}, err } - req.SetBasicAuth(c.ClientID, c.Secret) - req.Header.Set("Authorization", "Bearer "+c.Token.Token) - e := BillingAgreement{} if err = c.SendWithAuth(req, &e); err != nil { @@ -123,9 +115,6 @@ func (c *Client) ListBillingPlans(status interface{}, page interface{}) (*ListBi return &ListBillingPlansResp{}, err } - req.SetBasicAuth(c.ClientID, c.Secret) - req.Header.Set("Authorization", "Bearer "+c.Token.Token) - l := ListBillingPlansResp{} err = c.SendWithAuth(req, &l) @@ -137,4 +126,44 @@ func (c *Client) ListBillingPlans(status interface{}, page interface{}) (*ListBi } return &l, err +} + +// ReactivateAgreement reactivates a suspended billing agreement. +// Endpoint: POST /v1/payments/billing-agreements/{agreement_id}/re-activate +func (c *Client) ReactivateAgreement(agreementID string) error { + buf := bytes.NewBuffer([]byte(`{"note": "Reactivating the profile."}`)) + req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements/"+agreementID+"/re-activate"), buf) + if err != nil { + return err + } + + err = c.SendWithAuth(req, nil) + + // A successful request returns the HTTP 204 No Content status code with no JSON response body. + // This raises error "EOF" + if err != nil && err.Error() == "EOF" { + return nil + } + + return err +} + +// SuspendAgreement suspends a billing agreement. +// Endpoint: POST /v1/payments/billing-agreements/{agreement_id}/suspend +func (c *Client) SuspendAgreement(agreementID string) error { + buf := bytes.NewBuffer([]byte(`{"note": "Suspending the profile."}`)) + req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements/"+agreementID+"/suspend"), buf) + if err != nil { + return err + } + + err = c.SendWithAuth(req, nil) + + // A successful request returns the HTTP 204 No Content status code with no JSON response body. + // This raises error "EOF" + if err != nil && err.Error() == "EOF" { + return nil + } + + return err } \ No newline at end of file