2017-09-28 00:15:52 +02:00
|
|
|
package paypalsdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// CreateBillingResp struct
|
|
|
|
CreateBillingResp struct {
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
State string `json:"state,omitempty"`
|
|
|
|
PaymentDefinitions []PaymentDefinition `json:"payment_definitions,omitempty"`
|
|
|
|
MerchantPreferences MerchantPreferences `json:"merchant_preferences,omitempty"`
|
|
|
|
CreateTime time.Time `json:"create_time,omitempty"`
|
|
|
|
UpdateTime time.Time `json:"update_time,omitempty"`
|
|
|
|
Links []Link `json:"links,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAgreementResp struct
|
|
|
|
CreateAgreementResp struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Plan BillingPlan `json:"plan,omitempty"`
|
|
|
|
Links []Link `json:"links,omitempty"`
|
|
|
|
StartTime time.Time `json:"start_time,omitempty"`
|
|
|
|
}
|
2019-01-14 06:06:05 +01:00
|
|
|
|
|
|
|
// BillingPlanListParams struct
|
|
|
|
BillingPlanListParams struct {
|
|
|
|
Page string `json:"page,omitempty"` //Default: 0.
|
|
|
|
Status string `json:"status,omitempty"` //Allowed values: CREATED, ACTIVE, INACTIVE, ALL.
|
|
|
|
PageSize string `json:"page_size,omitempty"` //Default: 10.
|
|
|
|
TotalRequired string `json:"total_required,omitempty"` //Default: no.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//BillingPlanListResp struct
|
|
|
|
BillingPlanListResp struct {
|
|
|
|
Plans []BillingPlan `json:"plans,omitempty"`
|
|
|
|
TotalItems string `json:"total_items,omitempty"`
|
|
|
|
TotalPages string `json:"total_pages,omitempty"`
|
|
|
|
Links []Link `json:"links,omitempty"`
|
|
|
|
}
|
2017-09-28 00:15:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateBillingPlan creates a billing plan in Paypal
|
2019-03-27 09:27:53 +01:00
|
|
|
// Endpoint: POST /v2/payments/billing-plans
|
2017-09-28 00:15:52 +02:00
|
|
|
func (c *Client) CreateBillingPlan(plan BillingPlan) (*CreateBillingResp, error) {
|
2019-03-27 09:27:53 +01:00
|
|
|
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/billing-plans"), plan)
|
2017-09-28 00:15:52 +02:00
|
|
|
response := &CreateBillingResp{}
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
err = c.SendWithAuth(req, response)
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActivatePlan activates a billing plan
|
|
|
|
// By default, a new plan is not activated
|
2019-03-27 09:27:53 +01:00
|
|
|
// Endpoint: PATCH /v2/payments/billing-plans/
|
2017-09-28 00:15:52 +02:00
|
|
|
func (c *Client) ActivatePlan(planID string) error {
|
2018-10-13 23:45:06 +02:00
|
|
|
buf := bytes.NewBuffer([]byte(`[{"op":"replace","path":"/","value":{"state":"ACTIVE"}}]`))
|
2019-03-27 09:27:53 +01:00
|
|
|
req, err := http.NewRequest("PATCH", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/billing-plans/"+planID), buf)
|
2017-09-28 00:15:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.SetBasicAuth(c.ClientID, c.Secret)
|
|
|
|
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
|
|
|
return c.SendWithAuth(req, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBillingAgreement creates an agreement for specified plan
|
2019-03-27 09:27:53 +01:00
|
|
|
// Endpoint: POST /v2/payments/billing-agreements
|
2017-09-28 00:15:52 +02:00
|
|
|
func (c *Client) CreateBillingAgreement(a BillingAgreement) (*CreateAgreementResp, error) {
|
|
|
|
// PayPal needs only ID, so we will remove all fields except Plan ID
|
|
|
|
a.Plan = BillingPlan{
|
|
|
|
ID: a.Plan.ID,
|
|
|
|
}
|
|
|
|
|
2019-03-27 09:27:53 +01:00
|
|
|
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/billing-agreements"), a)
|
2017-09-28 00:15:52 +02:00
|
|
|
response := &CreateAgreementResp{}
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
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.
|
2019-03-27 09:27:53 +01:00
|
|
|
// Endpoint: POST /v2/payments/billing-agreements/token/agreement-execute
|
2017-09-28 00:15:52 +02:00
|
|
|
func (c *Client) ExecuteApprovedAgreement(token string) (*ExecuteAgreementResponse, error) {
|
2019-03-27 09:27:53 +01:00
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/billing-agreements/"+token+"/agreement-execute"), nil)
|
2017-09-28 00:15:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return &ExecuteAgreementResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.SetBasicAuth(c.ClientID, c.Secret)
|
|
|
|
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
|
|
|
|
|
|
|
|
e := ExecuteAgreementResponse{}
|
2017-11-23 03:15:11 +01:00
|
|
|
|
|
|
|
if err = c.SendWithAuth(req, &e); err != nil {
|
2017-09-28 00:15:52 +02:00
|
|
|
return &e, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.ID == "" {
|
|
|
|
return &e, errors.New("Unable to execute agreement with token=" + token)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &e, err
|
|
|
|
}
|
2019-01-14 06:06:05 +01:00
|
|
|
|
|
|
|
// ListBillingPlans lists billing-plans
|
2019-03-27 09:27:53 +01:00
|
|
|
// Endpoint: GET /v2/payments/billing-plans
|
2019-01-14 06:06:05 +01:00
|
|
|
func (c *Client) ListBillingPlans(bplp BillingPlanListParams) (*BillingPlanListResp, error) {
|
2019-03-27 09:27:53 +01:00
|
|
|
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v2/payments/billing-plans"), nil)
|
2019-01-14 06:06:05 +01:00
|
|
|
q := req.URL.Query()
|
|
|
|
q.Add("page", bplp.Page)
|
|
|
|
q.Add("page_size", bplp.PageSize)
|
|
|
|
q.Add("status", bplp.Status)
|
|
|
|
q.Add("total_required", bplp.TotalRequired)
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
response := &BillingPlanListResp{}
|
|
|
|
if err != nil {
|
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
err = c.SendWithAuth(req, response)
|
|
|
|
return response, err
|
|
|
|
}
|