forked from plutov/paypal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriptions.go
141 lines (112 loc) · 4.97 KB
/
subscriptions.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package paypal
import "fmt"
// CreateSubscription - Use this call to create a subscription
// Endpoint: POST /v1/billing/subscriptions
func (c *Client) CreateSubscription(subscription *CreateSubscriptionRequest) (*Subscription, error) {
resp := &Subscription{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/subscriptions"), subscription)
if err != nil {
return nil, err
}
if err = c.SendWithBasicAuth(req, resp); err != nil {
return nil, err
}
return resp, nil
}
// ShowSubscription shows details for a subscription by ID
// Endpoint: GET /v1/billing/subscriptions/{subscription_id}
func (c *Client) ShowSubscription(subscriptionID string, params *ShowSubscriptionRequest) (*Subscription, error) {
resp := &Subscription{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/subscriptions/"+subscriptionID), nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("fields", params.Fields)
if err = c.SendWithBasicAuth(req, resp); err != nil {
return nil, err
}
return resp, nil
}
// ActivatePlan activates subscription by ID
// Endpoint: POST /v1/billing/subscriptions/{subscription_id}/activate
func (c *Client) ActivateSubscription(subscriptionID string, body UpdateSubscriptionStatusRequest) error {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/subscriptions/"+subscriptionID+"/activate"), body)
if err != nil {
return err
}
return c.SendWithBasicAuth(req, nil)
}
// CancelSubscription cancels subscription by ID
// Endpoint: POST /v1/billing/subscriptions/{subscription_id}/cancel
func (c *Client) CancelSubscription(subscriptionID string, body *UpdateSubscriptionStatusRequest) error {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/subscriptions/"+subscriptionID+"/cancel"), body)
if err != nil {
return err
}
return c.SendWithBasicAuth(req, nil)
}
// SuspendSubscription suspends subscription by ID
// Endpoint: POST /v1/billing/subscriptions/{subscription_id}/suspend
func (c *Client) SuspendSubscription(subscriptionID string, body *UpdateSubscriptionStatusRequest) error {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/subscriptions/"+subscriptionID+"/suspend"), body)
if err != nil {
return err
}
return c.SendWithBasicAuth(req, nil)
}
// UpdateSubscription updates subscription by ID
// You can update the following attributes and objects
//-----------------------------------------------
// | Attribute or Object | Operation |
// ----------------------------------------------
// | path to the object | add, replace, remove |
// | (delimiter: /) | |
// ----------------------------------------------
// Endpoint: PATCH /v1/billing/subscriptions/{subscription_id}
func (c *Client) UpdateSubscription(subscriptionID string, body []*PatchObject) error {
req, err := c.NewRequest("PATCH", fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/subscriptions/"+subscriptionID), body)
if err != nil {
return err
}
return c.SendWithBasicAuth(req, nil)
}
// CaptureAuthorizedPaymentOnSubscription captures an authorized payment from the subscriber on the subscription
// Endpoint: POST /v1/billing/subscriptions/{subscription_id}/capture
func (c *Client) CaptureAuthorizedPaymentOnSubscription(subscriptionID string, body *CaptureAuthorizedPaymentOnSubscriptionRequest) error {
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/subscriptions/"+subscriptionID+"/capture"), body)
if err != nil {
return err
}
return c.SendWithBasicAuth(req, nil)
}
// ListTransactionsForSubscription lists transactions for subscription
// Endpoint: GET /v1/billing/subscriptions/{subscription_id}/transactions
func (c *Client) ListTransactionsForSubscription(subscriptionID string, params *ListTransactionsForSubscriptionRequest) (*TransactionsList, error) {
resp := &TransactionsList{}
req, err := c.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/subscriptions/"+subscriptionID+"/transactions"), nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("start_time", params.StartTime)
q.Add("end_time", params.EndTime)
if err = c.SendWithBasicAuth(req, resp); err != nil {
return nil, err
}
return resp, nil
}
// ReviseSubscription updates the quantity of the product or service in a subscription. You can also use this method to switch the plan
// and update the shipping_amount, shipping_address values for the subscription. This type of update requires the buyer's consent.
// Endpoint: POST /v1/billing/subscriptions/{subscription_id}/revise
func (c *Client) ReviseSubscription(subscriptionID string, body *ReviseSubscriptionRequest) (*ReviseSubscriptionResponse, error) {
resp := &ReviseSubscriptionResponse{}
req, err := c.NewRequest("POST", fmt.Sprintf("%s%s", c.APIBase, "/v1/billing/subscriptions/"+subscriptionID+"/revise"), body)
if err != nil {
return nil, err
}
if err = c.SendWithBasicAuth(req, resp); err != nil {
return nil, err
}
return resp, nil
}