1
|
|
|
package lemonsqueezy |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"net/http" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// LicensesService is the API client for the `/v1/licenses` endpoint |
10
|
|
|
type LicensesService service |
11
|
|
|
|
12
|
|
|
// Activate a license key. |
13
|
|
|
// |
14
|
|
|
// https://docs.lemonsqueezy.com/help/licensing/license-api |
15
|
|
|
func (service *LicensesService) Activate(ctx context.Context, licenseKey, instanceName string) (*LicenseActivateApiResponse, *Response, error) { |
16
|
|
|
payload := map[string]any{ |
17
|
|
|
"license_key": licenseKey, |
18
|
|
|
"instance_name": instanceName, |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
response, err := service.client.do(ctx, http.MethodPost, "/v1/licenses/activate", payload) |
22
|
|
|
if err != nil { |
23
|
|
|
return nil, response, err |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
activation := new(LicenseActivateApiResponse) |
27
|
|
|
if err = json.Unmarshal(*response.Body, activation); err != nil { |
28
|
|
|
return nil, response, err |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return activation, response, nil |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Validate a license key. |
35
|
|
|
// |
36
|
|
|
// https://docs.lemonsqueezy.com/help/licensing/license-api |
37
|
|
|
func (service *LicensesService) Validate(ctx context.Context, licenseKey, instanceID string) (*LicenseValidateApiResponse, *Response, error) { |
38
|
|
|
payload := map[string]any{ |
39
|
|
|
"license_key": licenseKey, |
40
|
|
|
"instance_id": instanceID, |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
response, err := service.client.do(ctx, http.MethodPost, "/v1/licenses/validate", payload) |
44
|
|
|
if err != nil { |
45
|
|
|
return nil, response, err |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
validation := new(LicenseValidateApiResponse) |
49
|
|
|
if err = json.Unmarshal(*response.Body, validation); err != nil { |
50
|
|
|
return nil, response, err |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return validation, response, nil |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Deactivate a license key. |
57
|
|
|
// |
58
|
|
|
// https://docs.lemonsqueezy.com/help/licensing/license-api |
59
|
|
|
func (service *LicensesService) Deactivate(ctx context.Context, licenseKey, instanceID string) (*LicenseDeactivateApiResponse, *Response, error) { |
60
|
|
|
payload := map[string]any{ |
61
|
|
|
"license_key": licenseKey, |
62
|
|
|
"instance_id": instanceID, |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
response, err := service.client.do(ctx, http.MethodPost, "/v1/licenses/deactivate", payload) |
66
|
|
|
if err != nil { |
67
|
|
|
return nil, response, err |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
deactivation := new(LicenseDeactivateApiResponse) |
71
|
|
|
if err = json.Unmarshal(*response.Body, deactivation); err != nil { |
72
|
|
|
return nil, response, err |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return deactivation, response, nil |
76
|
|
|
} |
77
|
|
|
|