AtlatestRepositorysigil-lemonsqueezy
1;;; (lemonsqueezy product) - Product and Variant management.
2;;;
3;;; Products and variants are read-only via the API (created in dashboard).
4;;; Products describe digital goods; variants represent purchasable
5;;; configurations (tiers, plans).
6
7(define-library (lemonsqueezy product)
8 (import (sigil core)
9 (sigil dict)
10 (sigil string)
11 (sigil struct)
12 (lemonsqueezy))
14 (export ;; Records
15 ls-product
16 ls-product?
17 ls-product-id
18 ls-product-name
19 ls-product-slug
20 ls-product-description
21 ls-product-status
22 ls-product-price
23 ls-product-price-formatted
24 ls-product-buy-now-url
25 ls-product-store-id
26 ls-product-test-mode
27 ls-product-created-at
28 ls-product-updated-at
30 ls-variant
31 ls-variant?
32 ls-variant-id
33 ls-variant-name
34 ls-variant-slug
35 ls-variant-description
36 ls-variant-price
37 ls-variant-sort
38 ls-variant-status
39 ls-variant-product-id
40 ls-variant-has-license-keys
41 ls-variant-license-activation-limit
42 ls-variant-created-at
43 ls-variant-updated-at
45 ;; Parsing
46 parse-product
47 parse-variant
49 ;; API functions
50 ls-products
51 ls-product-get
52 ls-variants
53 ls-variant-get)
55 (begin
57 ;; ---------------------------------------------------------------
58 ;; Records
59 ;; ---------------------------------------------------------------
61 (define-struct ls-product
62 (id)
63 (name default: "")
64 (slug default: "")
65 (description default: "")
66 (status default: "draft")
67 (price default: 0)
68 (price-formatted default: "")
69 (buy-now-url default: #f)
70 (store-id default: #f)
71 (test-mode default: #f)
72 (created-at default: #f)
73 (updated-at default: #f))
75 (define-struct ls-variant
76 (id)
77 (name default: "")
78 (slug default: "")
79 (description default: "")
80 (price default: 0)
81 (sort default: 0)
82 (status default: "pending")
83 (product-id default: #f)
84 (has-license-keys default: #f)
85 (license-activation-limit default: 0)
86 (created-at default: #f)
87 (updated-at default: #f))
89 ;; ---------------------------------------------------------------
90 ;; Parsing
91 ;; ---------------------------------------------------------------
93 ;;; Parse a JSON:API product resource into an ls-product record.
94 (define (parse-product resource)
95 (ls-product
96 id: (jsonapi-id resource)
97 name: (jsonapi-attr resource name: "")
98 slug: (jsonapi-attr resource slug: "")
99 description: (jsonapi-attr resource description: "")
100 status: (jsonapi-attr resource status: "draft")
101 price: (jsonapi-attr resource price: 0)
102 price-formatted: (jsonapi-attr resource price_formatted: "")
103 buy-now-url: (jsonapi-attr resource buy_now_url: #f)
104 store-id: (jsonapi-attr resource store_id: #f)
105 test-mode: (jsonapi-attr resource test_mode: #f)
106 created-at: (jsonapi-attr resource created_at: #f)
107 updated-at: (jsonapi-attr resource updated_at: #f)))
109 ;;; Parse a JSON:API variant resource into an ls-variant record.
110 (define (parse-variant resource)
111 (ls-variant
112 id: (jsonapi-id resource)
113 name: (jsonapi-attr resource name: "")
114 slug: (jsonapi-attr resource slug: "")
115 description: (jsonapi-attr resource description: "")
116 price: (jsonapi-attr resource price: 0)
117 sort: (jsonapi-attr resource sort: 0)
118 status: (jsonapi-attr resource status: "pending")
119 product-id: (jsonapi-attr resource product_id: #f)
120 has-license-keys: (jsonapi-attr resource has_license_keys: #f)
121 license-activation-limit: (jsonapi-attr resource license_activation_limit: 0)
122 created-at: (jsonapi-attr resource created_at: #f)
123 updated-at: (jsonapi-attr resource updated_at: #f)))
125 ;; ---------------------------------------------------------------
126 ;; API functions
127 ;; ---------------------------------------------------------------
129 ;;; List products with optional filtering and pagination.
130 ;;; Returns a list of ls-product records.
131 (define (ls-products client . rest)
132 (apply ls-list-endpoint client parse-product '("v1" "products") rest))
134 ;;; Get a single product by ID.
135 (define (ls-product-get client product-id)
136 (ls-get-endpoint client parse-product '("v1" "products") product-id))
138 ;;; List variants with optional filtering and pagination.
139 ;;; Returns a list of ls-variant records.
140 (define (ls-variants client . rest)
141 (apply ls-list-endpoint client parse-variant '("v1" "variants") rest))
143 ;;; Get a single variant by ID.
144 (define (ls-variant-get client variant-id)
145 (ls-get-endpoint client parse-variant '("v1" "variants") variant-id))
147 ))