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