1
|
|
|
package lemonsqueezy |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"net/http" |
7
|
|
|
"strconv" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// DiscountsService is the API client for the `/v1/discounts` endpoint |
11
|
|
|
type DiscountsService service |
12
|
|
|
|
13
|
|
|
// Create a discount. |
14
|
|
|
// |
15
|
|
|
// https://docs.lemonsqueezy.com/api/discounts#create-a-discount |
16
|
|
|
func (service *DiscountsService) Create(ctx context.Context, params *DiscountCreateParams) (*DiscountAPIResponse, *Response, error) { |
17
|
|
|
payload := map[string]any{ |
18
|
|
|
"data": map[string]any{ |
19
|
|
|
"type": "discounts", |
20
|
|
|
"attributes": map[string]any{ |
21
|
|
|
"name": params.Name, |
22
|
|
|
"code": params.Code, |
23
|
|
|
"amount": params.Amount, |
24
|
|
|
"amount_type": params.AmountType, |
25
|
|
|
}, |
26
|
|
|
"relationships": map[string]any{ |
27
|
|
|
"store": map[string]any{ |
28
|
|
|
"data": map[string]any{ |
29
|
|
|
"type": "stores", |
30
|
|
|
"id": strconv.Itoa(params.StoreID), |
31
|
|
|
}, |
32
|
|
|
}, |
33
|
|
|
}, |
34
|
|
|
}, |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
response, err := service.client.do(ctx, http.MethodPost, "/v1/discounts/", payload) |
38
|
|
|
if err != nil { |
39
|
|
|
return nil, response, err |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
discount := new(DiscountAPIResponse) |
43
|
|
|
if err = json.Unmarshal(*response.Body, discount); err != nil { |
44
|
|
|
return nil, response, err |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return discount, response, nil |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Get the discount with the given ID. |
51
|
|
|
// |
52
|
|
|
// https://docs.lemonsqueezy.com/api/discounts#retrieve-a-discount |
53
|
|
|
func (service *DiscountsService) Get(ctx context.Context, discountID string) (*DiscountAPIResponse, *Response, error) { |
54
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/discounts/"+discountID) |
55
|
|
|
if err != nil { |
56
|
|
|
return nil, response, err |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
discount := new(DiscountAPIResponse) |
60
|
|
|
if err = json.Unmarshal(*response.Body, discount); err != nil { |
61
|
|
|
return nil, response, err |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return discount, response, nil |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Delete a discount with the given ID. |
68
|
|
|
// |
69
|
|
|
// https://docs.lemonsqueezy.com/api/discounts#delete-a-discount |
70
|
|
|
func (service *DiscountsService) Delete(ctx context.Context, discountID string) (*Response, error) { |
71
|
|
|
return service.client.do(ctx, http.MethodDelete, "/v1/discounts/"+discountID) |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// List returns a paginated list of discounts. |
75
|
|
|
// |
76
|
|
|
// https://docs.lemonsqueezy.com/api/discounts#list-all-discounts |
77
|
|
|
func (service *DiscountsService) List(ctx context.Context) (*DiscountsAPIResponse, *Response, error) { |
78
|
|
|
response, err := service.client.do(ctx, http.MethodGet, "/v1/discounts") |
79
|
|
|
if err != nil { |
80
|
|
|
return nil, response, err |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
discounts := new(DiscountsAPIResponse) |
84
|
|
|
if err = json.Unmarshal(*response.Body, discounts); err != nil { |
85
|
|
|
return nil, response, err |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return discounts, response, nil |
89
|
|
|
} |
90
|
|
|
|