generated from NdoleStudio/go-http-client
-
Notifications
You must be signed in to change notification settings - Fork 17
/
checkouts_service.go
83 lines (71 loc) · 2.19 KB
/
checkouts_service.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
package lemonsqueezy
import (
"context"
"encoding/json"
"net/http"
"strconv"
)
// CheckoutsService is the API client for the `/v1/checkouts` endpoint
type CheckoutsService service
// Create a custom checkout.
//
// https://docs.lemonsqueezy.com/api/checkouts#create-a-checkout
func (service *CheckoutsService) Create(ctx context.Context, storeID int, variantID int, attributes *CheckoutCreateAttributes) (*CheckoutAPIResponse, *Response, error) {
payload := map[string]any{
"data": map[string]any{
"type": "checkouts",
"attributes": attributes,
"relationships": map[string]any{
"store": map[string]any{
"data": map[string]any{
"id": strconv.Itoa(storeID),
"type": "stores",
},
},
"variant": map[string]any{
"data": map[string]any{
"id": strconv.Itoa(variantID),
"type": "variants",
},
},
},
},
}
response, err := service.client.do(ctx, http.MethodPost, "/v1/checkouts", payload)
if err != nil {
return nil, response, err
}
checkout := new(CheckoutAPIResponse)
if err = json.Unmarshal(*response.Body, checkout); err != nil {
return nil, response, err
}
return checkout, response, nil
}
// Get the checkout with the given ID.
//
// https://docs.lemonsqueezy.com/api/checkouts#retrieve-a-checkout
func (service *CheckoutsService) Get(ctx context.Context, checkoutID string) (*CheckoutAPIResponse, *Response, error) {
response, err := service.client.do(ctx, http.MethodGet, "/v1/checkouts/"+checkoutID)
if err != nil {
return nil, response, err
}
checkout := new(CheckoutAPIResponse)
if err = json.Unmarshal(*response.Body, checkout); err != nil {
return nil, response, err
}
return checkout, response, nil
}
// List returns a paginated list of checkouts.
//
// https://docs.lemonsqueezy.com/api/checkouts#list-all-checkouts
func (service *CheckoutsService) List(ctx context.Context) (*CheckoutsAPIResponse, *Response, error) {
response, err := service.client.do(ctx, http.MethodGet, "/v1/checkouts")
if err != nil {
return nil, response, err
}
checkouts := new(CheckoutsAPIResponse)
if err = json.Unmarshal(*response.Body, checkouts); err != nil {
return nil, response, err
}
return checkouts, response, nil
}