1
|
|
|
package client |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"net/http" |
7
|
|
|
"time" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// CheckoutsService is the API client for the `/v1/checkouts` endpoint |
11
|
|
|
type CheckoutsService service |
12
|
|
|
|
13
|
|
|
// Create a custom checkout. |
14
|
|
|
// |
15
|
|
|
// https://docs.lemonsqueezy.com/api/checkouts#create-a-checkout |
16
|
|
|
func (service *CheckoutsService) Create(ctx context.Context, params *CheckoutCreateParams) (*CheckoutApiResponse, *Response, error) { |
17
|
|
|
checkoutData := map[string]any{ |
18
|
|
|
"custom": params.CustomData, |
19
|
|
|
} |
20
|
|
|
if params.DiscountCode != nil { |
21
|
|
|
checkoutData["discount_code"] = params.DiscountCode |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
payload := map[string]any{ |
25
|
|
|
"data": map[string]any{ |
26
|
|
|
"type": "checkouts", |
27
|
|
|
"attributes": map[string]any{ |
28
|
|
|
"custom_price": params.CustomPrice, |
29
|
|
|
"product_options": map[string]any{ |
30
|
|
|
"enabled_variants": params.EnabledVariants, |
31
|
|
|
}, |
32
|
|
|
"checkout_options": map[string]any{ |
33
|
|
|
"button_color": params.ButtonColor, |
34
|
|
|
}, |
35
|
|
|
"checkout_data": checkoutData, |
36
|
|
|
"expires_at": params.ExpiresAt.Format(time.RFC3339), |
37
|
|
|
"preview": true, |
38
|
|
|
}, |
39
|
|
|
"relationships": map[string]any{ |
40
|
|
|
"store": map[string]any{ |
41
|
|
|
"data": map[string]any{ |
42
|
|
|
"id": params.StoreID, |
43
|
|
|
"type": "stores", |
44
|
|
|
}, |
45
|
|
|
}, |
46
|
|
|
"variant": map[string]any{ |
47
|
|
|
"data": map[string]any{ |
48
|
|
|
"id": params.VariantID, |
49
|
|
|
"type": "variants", |
50
|
|
|
}, |
51
|
|
|
}, |
52
|
|
|
}, |
53
|
|
|
}, |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
response, err := service.client.do(ctx, http.MethodPost, "/v1/checkouts", payload) |
57
|
|
|
if err != nil { |
58
|
|
|
return nil, response, err |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
checkout := new(CheckoutApiResponse) |
62
|
|
|
if err = json.Unmarshal(*response.Body, checkout); err != nil { |
63
|
|
|
return nil, response, err |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return checkout, response, nil |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Get the checkout with the given ID. |
70
|
|
|
// |
71
|
|
|
// https://docs.lemonsqueezy.com/api/checkouts#retrieve-a-checkout |
72
|
|
|
func (service *CheckoutsService) Get(ctx context.Context, checkoutID string) (*CheckoutApiResponse, *Response, error) { |
73
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/checkouts/"+checkoutID) |
74
|
|
|
if err != nil { |
75
|
|
|
return nil, response, err |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
checkout := new(CheckoutApiResponse) |
79
|
|
|
if err = json.Unmarshal(*response.Body, checkout); err != nil { |
80
|
|
|
return nil, response, err |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return checkout, response, nil |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// List returns a paginated list of checkouts. |
87
|
|
|
// |
88
|
|
|
// https://docs.lemonsqueezy.com/api/checkouts#list-all-checkouts |
89
|
|
|
func (service *CheckoutsService) List(ctx context.Context) (*CheckoutsApiResponse, *Response, error) { |
90
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/checkouts") |
91
|
|
|
if err != nil { |
92
|
|
|
return nil, response, err |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
checkouts := new(CheckoutsApiResponse) |
96
|
|
|
if err = json.Unmarshal(*response.Body, checkouts); err != nil { |
97
|
|
|
return nil, response, err |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return checkouts, response, nil |
101
|
|
|
} |
102
|
|
|
|