AtlatestRepositorysigil-lemonsqueezy
sigil-lemonsqueezy / tree / src / lemonsqueezycheckout.sgl
1
;;; (lemonsqueezy checkout) - Checkout creation and retrieval.2
;;;3
;;; Checkouts create purchasable links for specific variants. This is the4
;;; primary way to initiate purchases programmatically. Custom data passed5
;;; through checkouts flows to webhooks via meta.custom_data.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 ;; Records16
ls-checkout17
ls-checkout?18
ls-checkout-id19
ls-checkout-url20
ls-checkout-store-id21
ls-checkout-variant-id22
ls-checkout-custom-price23
ls-checkout-expires-at24
ls-checkout-created-at25
ls-checkout-updated-at26
ls-checkout-test-mode28
;; Parsing29
parse-checkout31
;; API functions32
ls-create-checkout33
ls-checkout-get34
ls-checkouts)36
(begin38
;; ---------------------------------------------------------------39
;; Records40
;; ---------------------------------------------------------------42
(define-struct ls-checkout43
(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
;; Parsing55
;; ---------------------------------------------------------------57
;;; Parse a JSON:API checkout resource into an ls-checkout record.58
(define (parse-checkout resource)59
(ls-checkout60
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 functions72
;; ---------------------------------------------------------------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 overrides80
;;; checkout-options: — dict of checkout UI options81
;;; checkout-data: — dict with email:, name:, custom:, discount_code:, etc.82
;;; expires-at: — ISO 8601 expiration time83
;;; preview: — boolean, include pricing breakdown84
;;;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 fields89
(optional-fields90
(filter cdr91
(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 append98
(map (lambda (p) (list (car p) (cdr p))) optional-fields))))99
(body #{ data:100
#{ type: "checkouts"101
attributes: attrs102
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
))