1
|
|
|
package lemonsqueezy |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"net/http" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// SubscriptionsService is the API client for the `/subscriptions` endpoint |
10
|
|
|
type SubscriptionsService service |
11
|
|
|
|
12
|
|
|
// Update an existing subscription to specific parameter |
13
|
|
|
// |
14
|
|
|
// https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription |
15
|
|
|
func (service *SubscriptionsService) Update(ctx context.Context, params *SubscriptionUpdateParams) (*SubscriptionApiResponse, *Response, error) { |
16
|
|
|
payload := map[string]any{ |
17
|
|
|
"data": map[string]any{ |
18
|
|
|
"id": params.ID, |
19
|
|
|
"type": "subscriptions", |
20
|
|
|
"attributes": params.Attributes, |
21
|
|
|
}, |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
response, err := service.client.do(ctx, http.MethodPatch, "/v1/subscriptions/"+params.ID, payload) |
25
|
|
|
if err != nil { |
26
|
|
|
return nil, response, err |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
subscription := new(SubscriptionApiResponse) |
30
|
|
|
if err = json.Unmarshal(*response.Body, subscription); err != nil { |
31
|
|
|
return nil, response, err |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return subscription, response, nil |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// List returns a paginated list of subscriptions ordered by created_at (descending) |
38
|
|
|
// |
39
|
|
|
// https://docs.lemonsqueezy.com/api/subscriptions#list-all-subscriptions |
40
|
|
|
func (service *SubscriptionsService) List(ctx context.Context) (*SubscriptionsApiResponse, *Response, error) { |
41
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscriptions") |
42
|
|
|
if err != nil { |
43
|
|
|
return nil, response, err |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
subscriptions := new(SubscriptionsApiResponse) |
47
|
|
|
if err = json.Unmarshal(*response.Body, subscriptions); err != nil { |
48
|
|
|
return nil, response, err |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return subscriptions, response, nil |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Get returns the subscription with the given ID. |
55
|
|
|
// |
56
|
|
|
// https://docs.lemonsqueezy.com/api/subscriptions#retrieve-a-subscription |
57
|
|
|
func (service *SubscriptionsService) Get(ctx context.Context, subscriptionID string) (*SubscriptionApiResponse, *Response, error) { |
58
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscriptions/"+subscriptionID) |
59
|
|
|
if err != nil { |
60
|
|
|
return nil, response, err |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
subscription := new(SubscriptionApiResponse) |
64
|
|
|
if err = json.Unmarshal(*response.Body, subscription); err != nil { |
65
|
|
|
return nil, response, err |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return subscription, response, nil |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Cancel an active subscription the given ID. |
72
|
|
|
// |
73
|
|
|
// https://docs.lemonsqueezy.com/api/subscriptions#retrieve-a-subscription |
74
|
|
|
func (service *SubscriptionsService) Cancel(ctx context.Context, subscriptionID string) (*SubscriptionApiResponse, *Response, error) { |
75
|
|
|
response, err := service.client.do(ctx, http.MethodDelete, "/v1/subscriptions/"+subscriptionID) |
76
|
|
|
if err != nil { |
77
|
|
|
return nil, response, err |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
subscription := new(SubscriptionApiResponse) |
81
|
|
|
if err = json.Unmarshal(*response.Body, subscription); err != nil { |
82
|
|
|
return nil, response, err |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return subscription, response, nil |
86
|
|
|
} |
87
|
|
|
|