From ce8c1014ec493153e579e33038b3c7749f235b61 Mon Sep 17 00:00:00 2001 From: envy124 Date: Thu, 24 Aug 2017 13:22:46 +0300 Subject: [PATCH] ExecuteApprovedAgreement added --- billing.go | 25 +++++++++++++++++++++++++ types.go | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/billing.go b/billing.go index 052050c..18a18cb 100644 --- a/billing.go +++ b/billing.go @@ -2,6 +2,7 @@ package paypalsdk import ( "bytes" + "errors" "fmt" "net/http" "time" @@ -66,3 +67,27 @@ func (c *Client) CreateBillingAgreement(a BillingAgreement) (*CreateAgreementRes err = c.SendWithAuth(req, response) return response, err } + +// ExecuteApprovedAgreement - Use this call to execute (complete) a PayPal agreement that has been approved by the payer. +// Endpoint: POST /v1/payments/billing-agreements/token/agreement-execute +func (c *Client) ExecuteApprovedAgreement(token string) (*ExecuteAgreementResponse, error) { + req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements/"+token+"/agreement-execute"), nil) + if err != nil { + return &ExecuteAgreementResponse{}, err + } + + req.SetBasicAuth(c.ClientID, c.Secret) + req.Header.Set("Authorization", "Bearer "+c.Token.Token) + + e := ExecuteAgreementResponse{} + err = c.SendWithAuth(req, &e) + if err != nil { + return &e, err + } + + if e.ID == "" { + return &e, errors.New("Unable to execute agreement with token=" + token) + } + + return &e, err +} diff --git a/types.go b/types.go index 16cff3d..9a76801 100644 --- a/types.go +++ b/types.go @@ -58,6 +58,18 @@ type ( Phone string `json:"phone,omitempty"` } + // AgreementDetails struct + AgreementDetails struct { + OutstandingBalance AmountPayout `json:"outstanding_balance"` + CyclesRemaining int `json:"cycles_remaining"` + CyclesCompleted int `json:"cycles_completed"` + NextBillingDate time.Time `json:"next_billing_date"` + LastPaymentDate time.Time `json:"last_payment_date"` + LastPaymentAmount AmountPayout `json:"last_payment_amount"` + FinalPaymentDate time.Time `json:"final_payment_date"` + FailedPaymentCount int `json:"failed_payment_count"` + } + // Amount struct Amount struct { Currency string `json:"currency"` @@ -208,6 +220,19 @@ type ( Details string `json:"details"` } + // ExecuteAgreementResponse struct + ExecuteAgreementResponse struct { + ID string `json:"id"` + State string `json:"state"` + Description string `json:"description,omitempty"` + Payer Payer `json:"payer"` + Plan BillingPlan `json:"plan"` + StartDate time.Time `json:"start_date"` + ShippingAddress ShippingAddress `json:"shipping_address"` + AgreementDetails AgreementDetails `json:"agreement_details"` + Links []Link `json:"links"` + } + // ExecuteResponse struct ExecuteResponse struct { ID string `json:"id"`