github.com/plutov/paypal/v4@v4.7.1/billing.go (about)

     1  package paypal
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  	"time"
     9  )
    10  
    11  type (
    12  	// CreateBillingResponse struct
    13  	CreateBillingResponse struct {
    14  		ID                  string              `json:"id,omitempty"`
    15  		State               string              `json:"state,omitempty"`
    16  		PaymentDefinitions  []PaymentDefinition `json:"payment_definitions,omitempty"`
    17  		MerchantPreferences MerchantPreferences `json:"merchant_preferences,omitempty"`
    18  		CreateTime          time.Time           `json:"create_time,omitempty"`
    19  		UpdateTime          time.Time           `json:"update_time,omitempty"`
    20  		Links               []Link              `json:"links,omitempty"`
    21  	}
    22  
    23  	// CreateBillingResp.
    24  	//
    25  	// Deprecated: use CreateBillingResponse instead.
    26  	CreateBillingResp = CreateBillingResponse
    27  
    28  	// CreateAgreementResponse struct
    29  	CreateAgreementResponse struct {
    30  		Name        string      `json:"name,omitempty"`
    31  		Description string      `json:"description,omitempty"`
    32  		Plan        BillingPlan `json:"plan,omitempty"`
    33  		Links       []Link      `json:"links,omitempty"`
    34  		StartTime   time.Time   `json:"start_time,omitempty"`
    35  	}
    36  
    37  	// CreateAgreementResp.
    38  	//
    39  	// Deprecated: use CreateAgreementResponse instead.
    40  	CreateAgreementResp = CreateAgreementResponse
    41  
    42  	// BillingPlanListParams
    43  	BillingPlanListParams struct {
    44  		ListParams
    45  		Status string `json:"status,omitempty"` //Allowed values: CREATED, ACTIVE, INACTIVE, ALL.
    46  	}
    47  
    48  	// BillingPlanListResponse
    49  	BillingPlanListResponse struct {
    50  		SharedListResponse
    51  		Plans []BillingPlan `json:"plans,omitempty"`
    52  	}
    53  
    54  	// BillingPlanListResp.
    55  	//
    56  	// Deprecated: use BillingPlanListResponse instead.
    57  	BillingPlanListResp = BillingPlanListResponse
    58  )
    59  
    60  // CreateBillingPlan creates a billing plan in Paypal
    61  // Endpoint: POST /v1/payments/billing-plans
    62  func (c *Client) CreateBillingPlan(ctx context.Context, plan BillingPlan) (*CreateBillingResponse, error) {
    63  	req, err := c.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans"), plan)
    64  	response := &CreateBillingResponse{}
    65  	if err != nil {
    66  		return response, err
    67  	}
    68  	err = c.SendWithAuth(req, response)
    69  	return response, err
    70  }
    71  
    72  // UpdateBillingPlan updates values inside a billing plan
    73  // Endpoint: PATCH /v1/payments/billing-plans
    74  func (c *Client) UpdateBillingPlan(ctx context.Context, planId string, pathValues map[string]map[string]interface{}) error {
    75  	var patchData []Patch
    76  	for path, data := range pathValues {
    77  		patchData = append(patchData, Patch{
    78  			Operation: "replace",
    79  			Path:      path,
    80  			Value:     data,
    81  		})
    82  	}
    83  
    84  	req, err := c.NewRequest(ctx, http.MethodPatch, fmt.Sprintf("%s%s%s", c.APIBase, "/v1/payments/billing-plans/", planId), patchData)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	err = c.SendWithAuth(req, nil)
    89  	return err
    90  }
    91  
    92  // ActivatePlan activates a billing plan
    93  // By default, a new plan is not activated
    94  // Endpoint: PATCH /v1/payments/billing-plans/
    95  func (c *Client) ActivatePlan(ctx context.Context, planID string) error {
    96  	return c.UpdateBillingPlan(ctx, planID, map[string]map[string]interface{}{
    97  		"/": {"state": BillingPlanStatusActive},
    98  	})
    99  }
   100  
   101  // CreateBillingAgreement creates an agreement for specified plan
   102  // Endpoint: POST /v1/payments/billing-agreements
   103  // Deprecated: Use POST /v1/billing-agreements/agreements
   104  func (c *Client) CreateBillingAgreement(ctx context.Context, a BillingAgreement) (*CreateAgreementResponse, error) {
   105  	// PayPal needs only ID, so we will remove all fields except Plan ID
   106  	a.Plan = BillingPlan{
   107  		ID: a.Plan.ID,
   108  	}
   109  
   110  	req, err := c.NewRequest(ctx, http.MethodPost, fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-agreements"), a)
   111  	response := &CreateAgreementResponse{}
   112  	if err != nil {
   113  		return response, err
   114  	}
   115  	err = c.SendWithAuth(req, response)
   116  	return response, err
   117  }
   118  
   119  // ExecuteApprovedAgreement - Use this call to execute (complete) a PayPal agreement that has been approved by the payer.
   120  // Endpoint: POST /v1/payments/billing-agreements/token/agreement-execute
   121  func (c *Client) ExecuteApprovedAgreement(ctx context.Context, token string) (*ExecuteAgreementResponse, error) {
   122  	req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("%s/v1/payments/billing-agreements/%s/agreement-execute", c.APIBase, token), nil)
   123  	response := &ExecuteAgreementResponse{}
   124  
   125  	if err != nil {
   126  		return response, err
   127  	}
   128  
   129  	req.SetBasicAuth(c.ClientID, c.Secret)
   130  	req.Header.Set("Authorization", "Bearer "+c.Token.Token)
   131  
   132  	if err = c.SendWithAuth(req, response); err != nil {
   133  		return response, err
   134  	}
   135  
   136  	if response.ID == "" {
   137  		return response, errors.New("Unable to execute agreement with token=" + token)
   138  	}
   139  
   140  	return response, err
   141  }
   142  
   143  // ListBillingPlans lists billing-plans
   144  // Endpoint: GET /v1/payments/billing-plans
   145  func (c *Client) ListBillingPlans(ctx context.Context, bplp BillingPlanListParams) (*BillingPlanListResponse, error) {
   146  	req, err := c.NewRequest(ctx, "GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/billing-plans"), nil)
   147  	response := &BillingPlanListResponse{}
   148  	if err != nil {
   149  		return response, err
   150  	}
   151  
   152  	q := req.URL.Query()
   153  	q.Add("page", bplp.Page)
   154  	q.Add("page_size", bplp.PageSize)
   155  	q.Add("status", bplp.Status)
   156  	q.Add("total_required", bplp.TotalRequired)
   157  	req.URL.RawQuery = q.Encode()
   158  
   159  	err = c.SendWithAuth(req, response)
   160  	return response, err
   161  }