#33 Send only Plan ID when create agreement

This commit is contained in:
Alex Pliutau 2017-09-27 17:15:52 -05:00
parent f48535a92e
commit b12ee60c90

View File

@ -1,93 +1,98 @@
package paypalsdk package paypalsdk
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
) )
type ( type (
// CreateBillingResp struct // CreateBillingResp struct
CreateBillingResp struct { CreateBillingResp struct {
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
State string `json:"state,omitempty"` State string `json:"state,omitempty"`
PaymentDefinitions []PaymentDefinition `json:"payment_definitions,omitempty"` PaymentDefinitions []PaymentDefinition `json:"payment_definitions,omitempty"`
MerchantPreferences MerchantPreferences `json:"merchant_preferences,omitempty"` MerchantPreferences MerchantPreferences `json:"merchant_preferences,omitempty"`
CreateTime time.Time `json:"create_time,omitempty"` CreateTime time.Time `json:"create_time,omitempty"`
UpdateTime time.Time `json:"update_time,omitempty"` UpdateTime time.Time `json:"update_time,omitempty"`
Links []Link `json:"links,omitempty"` Links []Link `json:"links,omitempty"`
} }
// CreateAgreementResp struct // CreateAgreementResp struct
CreateAgreementResp struct { CreateAgreementResp struct {
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Plan BillingPlan `json:"plan,omitempty"` Plan BillingPlan `json:"plan,omitempty"`
Links []Link `json:"links,omitempty"` Links []Link `json:"links,omitempty"`
StartTime time.Time `json:"start_time,omitempty"` StartTime time.Time `json:"start_time,omitempty"`
} }
) )
// CreateBillingPlan creates a billing plan in Paypal // CreateBillingPlan creates a billing plan in Paypal
// Endpoint: POST /v1/payments/billing-plans // Endpoint: POST /v1/payments/billing-plans
func (c *Client) CreateBillingPlan(plan BillingPlan) (*CreateBillingResp, error) { func (c *Client) CreateBillingPlan(plan BillingPlan) (*CreateBillingResp, error) {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans"), plan) req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans"), plan)
response := &CreateBillingResp{} response := &CreateBillingResp{}
if err != nil { if err != nil {
return response, err return response, err
} }
err = c.SendWithAuth(req, response) err = c.SendWithAuth(req, response)
return response, err return response, err
} }
// Activates a billing plan // ActivatePlan activates a billing plan
// By default, a new plan is not activated // By default, a new plan is not activated
// Endpoint: PATCH /v1/payments/billing-plans/ // Endpoint: PATCH /v1/payments/billing-plans/
func (c *Client) ActivatePlan(planID string) error { func (c *Client) ActivatePlan(planID string) error {
buf := bytes.NewBuffer([]byte("[{\"op\":\"replace\",\"path\":\"/\",\"value\":{\"state\":\"ACTIVE\"}}]")) buf := bytes.NewBuffer([]byte("[{\"op\":\"replace\",\"path\":\"/\",\"value\":{\"state\":\"ACTIVE\"}}]"))
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans/"+planID), buf) req, err := http.NewRequest("PATCH", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans/"+planID), buf)
if err != nil { if err != nil {
return err return err
} }
req.SetBasicAuth(c.ClientID, c.Secret) req.SetBasicAuth(c.ClientID, c.Secret)
req.Header.Set("Authorization", "Bearer "+c.Token.Token) req.Header.Set("Authorization", "Bearer "+c.Token.Token)
return c.SendWithAuth(req, nil) return c.SendWithAuth(req, nil)
} }
// Creates an agreement for specified plan // CreateBillingAgreement creates an agreement for specified plan
// Endpoint: POST /v1/payments/billing-agreements // Endpoint: POST /v1/payments/billing-agreements
func (c *Client) CreateBillingAgreement(a BillingAgreement) (*CreateAgreementResp, error) { func (c *Client) CreateBillingAgreement(a BillingAgreement) (*CreateAgreementResp, error) {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements"), a) // PayPal needs only ID, so we will remove all fields except Plan ID
response := &CreateAgreementResp{} a.Plan = BillingPlan{
if err != nil { ID: a.Plan.ID,
return response, err }
}
err = c.SendWithAuth(req, response) req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements"), a)
return response, err response := &CreateAgreementResp{}
} if err != nil {
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 err = c.SendWithAuth(req, response)
func (c *Client) ExecuteApprovedAgreement(token string) (*ExecuteAgreementResponse, error) { return response, err
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 // 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.SetBasicAuth(c.ClientID, c.Secret) req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements/"+token+"/agreement-execute"), nil)
req.Header.Set("Authorization", "Bearer "+c.Token.Token) if err != nil {
return &ExecuteAgreementResponse{}, err
e := ExecuteAgreementResponse{} }
err = c.SendWithAuth(req, &e)
if err != nil { req.SetBasicAuth(c.ClientID, c.Secret)
return &e, err req.Header.Set("Authorization", "Bearer "+c.Token.Token)
}
e := ExecuteAgreementResponse{}
if e.ID == "" { err = c.SendWithAuth(req, &e)
return &e, errors.New("Unable to execute agreement with token=" + token) if err != nil {
} return &e, err
}
return &e, err
} if e.ID == "" {
return &e, errors.New("Unable to execute agreement with token=" + token)
}
return &e, err
}