Created
March 27, 2024 20:21
-
-
Save laheadle/a6b5f4c6df3218a586a9a043c1de18aa to your computer and use it in GitHub Desktop.
Using stripe api via clojure interop in a biff app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns org.stinkless.rekonstruction.api.entry | |
(:require | |
[cheshire.core :as cheshire] | |
[com.biffweb :as biff] | |
[org.stinkless.rekonstruction.raw.anomalies :as an] | |
[org.stinkless.rekonstruction.util :as util] | |
[taoensso.truss :as truss]) | |
(:import | |
(com.stripe.model.checkout Session) | |
(com.stripe.param.checkout SessionCreateParams SessionCreateParams$LineItem SessionCreateParams$LineItem$PriceData SessionCreateParams$LineItem$PriceData$ProductData SessionCreateParams$Mode))) | |
(defn create-checkout-session [{:keys [biff/secret biff/base-url]} {:keys [price success-path]}] | |
(truss/have? int? price) | |
(let [api-key (secret :stripe/api-key) | |
success-path (format "%s/%s" base-url success-path) | |
price-data (.. (SessionCreateParams$LineItem$PriceData/builder) | |
(setCurrency "USD") | |
(setUnitAmount price) | |
(setProductData (.. (SessionCreateParams$LineItem$PriceData$ProductData/builder) | |
(setName "Entry Ticket") | |
(setDescription "One ticket allowing access for the duration of the event.") | |
build)) | |
build) | |
params (.. (SessionCreateParams/builder) | |
(setSuccessUrl success-path) | |
(addLineItem (.. (SessionCreateParams$LineItem/builder) | |
(setPriceData price-data) | |
(setQuantity 1) | |
build)) | |
(setMode SessionCreateParams$Mode/PAYMENT) | |
build)] | |
(set! com.stripe.Stripe/apiKey api-key) | |
(try (-> (.. (Session/create params) | |
toJson) | |
(cheshire/parse-string keyword) | |
(select-keys [:id :payment_status :currency :status :amount_total :url])) | |
(catch Exception e | |
(throw (an/exception :checkout-session-create-failed {} e)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment