AtlatestRepositorysigil-lemonsqueezy
1;;; (lemonsqueezy checkout) - Checkout creation and retrieval.
2;;;
3;;; Checkouts create purchasable links for specific variants. This is the
4;;; primary way to initiate purchases programmatically. Custom data passed
5;;; through checkouts flows to webhooks via meta.custom_data.
6
7(define-library (lemonsqueezy checkout)
8 (import (sigil core)
9 (sigil dict)
10 (sigil string)
11 (sigil struct)
12 (sigil json)
13 (lemonsqueezy))
15 (export ;; Records
16 ls-checkout
17 ls-checkout?
18 ls-checkout-id
19 ls-checkout-url
20 ls-checkout-store-id
21 ls-checkout-variant-id
22 ls-checkout-custom-price
23 ls-checkout-expires-at
24 ls-checkout-created-at
25 ls-checkout-updated-at
26 ls-checkout-test-mode
28 ;; Parsing
29 parse-checkout
31 ;; API functions
32 ls-create-checkout
33 ls-checkout-get
34 ls-checkouts)
36 (begin
38 ;; ---------------------------------------------------------------
39 ;; Records
40 ;; ---------------------------------------------------------------
42 (define-struct ls-checkout
43 (id)
44 (url default: #f)
45 (store-id default: #f)
46 (variant-id default: #f)
47 (custom-price default: #f)
48 (expires-at default: #f)
49 (created-at default: #f)
50 (updated-at default: #f)
51 (test-mode default: #f))
53 ;; ---------------------------------------------------------------
54 ;; Parsing
55 ;; ---------------------------------------------------------------
57 ;;; Parse a JSON:API checkout resource into an ls-checkout record.
58 (define (parse-checkout resource)
59 (ls-checkout
60 id: (jsonapi-id resource)
61 url: (jsonapi-attr resource url: #f)
62 store-id: (jsonapi-attr resource store_id: #f)
63 variant-id: (jsonapi-attr resource variant_id: #f)
64 custom-price: (jsonapi-attr resource custom_price: #f)
65 expires-at: (jsonapi-attr resource expires_at: #f)
66 created-at: (jsonapi-attr resource created_at: #f)
67 updated-at: (jsonapi-attr resource updated_at: #f)
68 test-mode: (jsonapi-attr resource test_mode: #f)))
70 ;; ---------------------------------------------------------------
71 ;; API functions
72 ;; ---------------------------------------------------------------
74 ;;; Create a checkout for a store and variant.
75 ;;;
76 ;;; store-id and variant-id are required (as strings).
77 ;;; opts is a dict with optional keys:
78 ;;; custom-price: — price in cents (integer)
79 ;;; product-options: — dict of product display overrides
80 ;;; checkout-options: — dict of checkout UI options
81 ;;; checkout-data: — dict with email:, name:, custom:, discount_code:, etc.
82 ;;; expires-at: — ISO 8601 expiration time
83 ;;; preview: — boolean, include pricing breakdown
84 ;;;
85 ;;; Returns an ls-checkout record.
86 (define (ls-create-checkout client store-id variant-id . rest)
87 (let* ((opts (if (null? rest) #{} (car rest)))
88 ;; Build attributes from optional fields
89 (optional-fields
90 (filter cdr
91 (list (cons custom_price: (dict-ref opts custom-price: #f))
92 (cons product_options: (dict-ref opts product-options: #f))
93 (cons checkout_options: (dict-ref opts checkout-options: #f))
94 (cons checkout_data: (dict-ref opts checkout-data: #f))
95 (cons expires_at: (dict-ref opts expires-at: #f))
96 (cons preview: (dict-ref opts preview: #f)))))
97 (attrs (apply dict (apply append
98 (map (lambda (p) (list (car p) (cdr p))) optional-fields))))
99 (body #{ data:
100 #{ type: "checkouts"
101 attributes: attrs
102 relationships:
103 #{ store:
104 #{ data: #{ type: "stores" id: store-id } }
105 variant:
106 #{ data: #{ type: "variants" id: variant-id } } } } })
107 (url (ls-api-url client "v1" "checkouts"))
108 (response (ls-post/json client url body)))
109 (parse-checkout (jsonapi-data response))))
111 ;;; Get a single checkout by ID.
112 (define (ls-checkout-get client checkout-id)
113 (ls-get-endpoint client parse-checkout '("v1" "checkouts") checkout-id))
115 ;;; List checkouts with optional filtering and pagination.
116 (define (ls-checkouts client . rest)
117 (apply ls-list-endpoint client parse-checkout '("v1" "checkouts") rest))
119 ))