AtlatestRepositorysigil-lemonsqueezy
sigil-lemonsqueezy / tree / src / lemonsqueezyproduct.sgl
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 purchasable5
;;; configurations (tiers, plans).7
(define-library (lemonsqueezy product)8
(import (sigil core)9
(sigil dict)10
(sigil string)11
(sigil struct)12
(lemonsqueezy))14
(export ;; Records15
ls-product16
ls-product?17
ls-product-id18
ls-product-name19
ls-product-slug20
ls-product-description21
ls-product-status22
ls-product-price23
ls-product-price-formatted24
ls-product-buy-now-url25
ls-product-store-id26
ls-product-test-mode27
ls-product-created-at28
ls-product-updated-at30
ls-variant31
ls-variant?32
ls-variant-id33
ls-variant-name34
ls-variant-slug35
ls-variant-description36
ls-variant-price37
ls-variant-sort38
ls-variant-status39
ls-variant-product-id40
ls-variant-has-license-keys41
ls-variant-license-activation-limit42
ls-variant-created-at43
ls-variant-updated-at45
;; Parsing46
parse-product47
parse-variant49
;; API functions50
ls-products51
ls-product-get52
ls-variants53
ls-variant-get)55
(begin57
;; ---------------------------------------------------------------58
;; Records59
;; ---------------------------------------------------------------61
(define-struct ls-product62
(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-variant76
(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
;; Parsing91
;; ---------------------------------------------------------------93
;;; Parse a JSON:API product resource into an ls-product record.94
(define (parse-product resource)95
(ls-product96
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-variant112
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 functions127
;; ---------------------------------------------------------------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
))