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

     1  package paypal
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  )
     7  
     8  // CreatePaypalBillingAgreementToken - Use this call to create a billing agreement token
     9  // Endpoint: POST /v1/billing-agreements/agreement-tokens
    10  // Deprecated: use CreateBillingAgreementToken instead
    11  func (c *Client) CreatePaypalBillingAgreementToken(
    12  	ctx context.Context,
    13  	description *string,
    14  	shippingAddress *ShippingAddress,
    15  	payer *Payer,
    16  	plan *BillingPlan,
    17  ) (*BillingAgreementToken, error) {
    18  	return c.CreateBillingAgreementToken(ctx, description, shippingAddress, payer, plan)
    19  }
    20  
    21  // CreateBillingAgreementToken - Use this call to create a billing agreement token
    22  // Endpoint: POST /v1/billing-agreements/agreement-tokens
    23  func (c *Client) CreateBillingAgreementToken(
    24  	ctx context.Context,
    25  	description *string,
    26  	shippingAddress *ShippingAddress,
    27  	payer *Payer,
    28  	plan *BillingPlan,
    29  ) (*BillingAgreementToken, error) {
    30  	type createBARequest struct {
    31  		Description     *string          `json:"description,omitempty"`
    32  		ShippingAddress *ShippingAddress `json:"shipping_address,omitempty"`
    33  		Payer           *Payer           `json:"payer"`
    34  		Plan            *BillingPlan     `json:"plan"`
    35  	}
    36  
    37  	billingAgreementToken := &BillingAgreementToken{}
    38  
    39  	req, err := c.NewRequest(
    40  		ctx,
    41  		"POST",
    42  		fmt.Sprintf("%s%s", c.APIBase, "/v1/billing-agreements/agreement-tokens"),
    43  		createBARequest{Description: description, ShippingAddress: shippingAddress, Payer: payer, Plan: plan})
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	if err = c.SendWithAuth(req, billingAgreementToken); err != nil {
    49  		return billingAgreementToken, err
    50  	}
    51  
    52  	return billingAgreementToken, nil
    53  }
    54  
    55  // CreatePaypalBillingAgreementFromToken - Use this call to create a billing agreement
    56  // Endpoint: POST /v1/billing-agreements/agreements
    57  // Deprecated: use CreateBillingAgreementFromToken instead
    58  func (c *Client) CreatePaypalBillingAgreementFromToken(
    59  	ctx context.Context,
    60  	tokenID string,
    61  ) (*BillingAgreementFromToken, error) {
    62  	return c.CreateBillingAgreementFromToken(ctx, tokenID)
    63  }
    64  
    65  // CreateBillingAgreementFromToken - Use this call to create a billing agreement
    66  // Endpoint: POST /v1/billing-agreements/agreements
    67  func (c *Client) CreateBillingAgreementFromToken(
    68  	ctx context.Context,
    69  	tokenID string,
    70  ) (*BillingAgreementFromToken, error) {
    71  	type createBARequest struct {
    72  		TokenID string `json:"token_id"`
    73  	}
    74  
    75  	billingAgreement := &BillingAgreementFromToken{}
    76  
    77  	req, err := c.NewRequest(
    78  		ctx,
    79  		"POST",
    80  		fmt.Sprintf("%s%s", c.APIBase, "/v1/billing-agreements/agreements"),
    81  		createBARequest{TokenID: tokenID})
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	if err = c.SendWithAuth(req, billingAgreement); err != nil {
    87  		return billingAgreement, err
    88  	}
    89  
    90  	return billingAgreement, nil
    91  }
    92  
    93  // CancelBillingAgreement - Use this call to cancel a billing agreement
    94  // Endpoint: POST /v1/billing-agreements/agreements/{agreement_id}/cancel
    95  func (c *Client) CancelBillingAgreement(
    96  	ctx context.Context,
    97  	billingAgreementID string,
    98  ) error {
    99  	type cancelBARequest struct{}
   100  
   101  	req, err := c.NewRequest(
   102  		ctx,
   103  		"POST",
   104  		fmt.Sprintf("%s%s%s%s", c.APIBase, "/v1/billing-agreements/agreements/", billingAgreementID, "/cancel"),
   105  		cancelBARequest{})
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	if err = c.SendWithAuth(req, nil); err != nil {
   111  		return err
   112  	}
   113  
   114  	return nil
   115  }