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) (*ApiResponseSubscription, *Response, error) { |
16
|
|
|
typeParam := "subscriptions" |
17
|
|
|
if len(params.Type) > 0 { |
18
|
|
|
typeParam = params.Type |
19
|
|
|
} |
20
|
|
|
payload := map[string]any{ |
21
|
|
|
"data": map[string]any{ |
22
|
|
|
"id": params.ID, |
23
|
|
|
"type": typeParam, |
24
|
|
|
"attributes": params.Attributes, |
25
|
|
|
}, |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
response, err := service.client.do(ctx, http.MethodPatch, "/v1/subscriptions/"+params.ID, payload) |
29
|
|
|
if err != nil { |
30
|
|
|
return nil, response, err |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
subscription := new(ApiResponseSubscription) |
34
|
|
|
if err = json.Unmarshal(*response.Body, subscription); err != nil { |
35
|
|
|
return nil, response, err |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return subscription, response, nil |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// List returns a paginated list of subscriptions ordered by created_at (descending) |
42
|
|
|
// |
43
|
|
|
// https://docs.lemonsqueezy.com/api/subscriptions#list-all-subscriptions |
44
|
|
|
func (service *SubscriptionsService) List(ctx context.Context) (*ApiResponseSubscriptionList, *Response, error) { |
45
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscriptions") |
46
|
|
|
if err != nil { |
47
|
|
|
return nil, response, err |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
subscriptions := new(ApiResponseSubscriptionList) |
51
|
|
|
if err = json.Unmarshal(*response.Body, subscriptions); err != nil { |
52
|
|
|
return nil, response, err |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return subscriptions, response, nil |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Get returns the subscription with the given ID. |
59
|
|
|
// |
60
|
|
|
// https://docs.lemonsqueezy.com/api/subscriptions#retrieve-a-subscription |
61
|
|
|
func (service *SubscriptionsService) Get(ctx context.Context, subscriptionID string) (*ApiResponseSubscription, *Response, error) { |
62
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/subscriptions/"+subscriptionID) |
63
|
|
|
if err != nil { |
64
|
|
|
return nil, response, err |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
subscription := new(ApiResponseSubscription) |
68
|
|
|
if err = json.Unmarshal(*response.Body, subscription); err != nil { |
69
|
|
|
return nil, response, err |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return subscription, response, nil |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Cancel an active subscription the given ID. |
76
|
|
|
// |
77
|
|
|
// https://docs.lemonsqueezy.com/api/subscriptions#retrieve-a-subscription |
78
|
|
|
func (service *SubscriptionsService) Cancel(ctx context.Context, subscriptionID string) (*ApiResponseSubscription, *Response, error) { |
79
|
|
|
response, err := service.client.do(ctx, http.MethodDelete, "/v1/subscriptions/"+subscriptionID) |
80
|
|
|
if err != nil { |
81
|
|
|
return nil, response, err |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
subscription := new(ApiResponseSubscription) |
85
|
|
|
if err = json.Unmarshal(*response.Body, subscription); err != nil { |
86
|
|
|
return nil, response, err |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return subscription, response, nil |
90
|
|
|
} |
91
|
|
|
|