paypale/billing.go

149 lines
4.6 KiB
Go
Raw Permalink Normal View History

2019-08-21 15:50:20 +02:00
package paypal
import (
"bytes"
"encoding/json"
"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 {
ListParams
Status string `json:"status,omitempty"` //Allowed values: CREATED, ACTIVE, INACTIVE, ALL.
2019-01-14 06:06:05 +01:00
}
//BillingPlanListResp struct
BillingPlanListResp struct {
SharedListResponse
Plans []BillingPlan `json:"plans,omitempty"`
2019-01-14 06:06:05 +01:00
}
)
// CreateBillingPlan creates a billing plan in Paypal
2020-05-02 17:00:45 +02:00
// Endpoint: POST /v1/payments/billing-plans
func (c *Client) CreateBillingPlan(plan BillingPlan) (*CreateBillingResp, error) {
req, err := c.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans"), plan)
response := &CreateBillingResp{}
if err != nil {
return response, err
}
err = c.SendWithAuth(req, response)
return response, err
}
// UpdateBillingPlan updates values inside a billing plan
// Endpoint: PATCH /v1/payments/billing-plans
func (c *Client) UpdateBillingPlan(planId string, pathValues map[string]map[string]interface{}) error {
patchData := []Patch{}
for path, data := range pathValues {
patchData = append(patchData, Patch{
Operation: "replace",
Path: path,
Value: data,
})
}
jsonData, err := json.Marshal(patchData)
buf := bytes.NewBuffer(jsonData)
req, err := c.NewRequest(http.MethodPatch, fmt.Sprintf("%s%s%s", c.APIBase, "/v1/payments/billing-plans/", planId), buf)
if err != nil {
return err
}
err = c.SendWithAuth(req, nil)
return err
}
// ActivatePlan activates a billing plan
// By default, a new plan is not activated
2020-05-02 17:00:45 +02:00
// Endpoint: PATCH /v1/payments/billing-plans/
func (c *Client) ActivatePlan(planID string) error {
return c.UpdateBillingPlan(planID, map[string]map[string]interface{}{
"/": {"state": BillingPlanStatusActive},
})
}
// CreateBillingAgreement creates an agreement for specified plan
2020-05-02 17:00:45 +02:00
// Endpoint: POST /v1/payments/billing-agreements
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,
}
req, err := c.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements"), a)
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.
2020-05-02 17:00:45 +02:00
// Endpoint: POST /v1/payments/billing-agreements/token/agreement-execute
func (c *Client) ExecuteApprovedAgreement(token string) (*ExecuteAgreementResponse, error) {
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v1/payments/billing-agreements/%s/agreement-execute", c.APIBase, token), nil)
2019-06-16 04:39:08 +02:00
response := &ExecuteAgreementResponse{}
if err != nil {
2019-06-16 04:39:08 +02:00
return response, err
}
req.SetBasicAuth(c.ClientID, c.Secret)
req.Header.Set("Authorization", "Bearer "+c.Token.Token)
2019-06-16 04:39:08 +02:00
if err = c.SendWithAuth(req, response); err != nil {
return response, err
}
2019-06-16 04:39:08 +02:00
if response.ID == "" {
return response, errors.New("Unable to execute agreement with token=" + token)
}
2019-06-16 04:39:08 +02:00
return response, err
}
2019-01-14 06:06:05 +01:00
// ListBillingPlans lists billing-plans
2020-05-02 17:00:45 +02:00
// Endpoint: GET /v1/payments/billing-plans
2019-01-14 06:06:05 +01:00
func (c *Client) ListBillingPlans(bplp BillingPlanListParams) (*BillingPlanListResp, error) {
2020-05-02 17:00:45 +02:00
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans"), nil)
response := &BillingPlanListResp{}
if err != nil {
return response, err
}
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()
2019-01-14 06:06:05 +01:00
err = c.SendWithAuth(req, response)
return response, err
}