Skip to content

Instantly share code, notes, and snippets.

@laheadle
Created March 27, 2024 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laheadle/a6b5f4c6df3218a586a9a043c1de18aa to your computer and use it in GitHub Desktop.
Save laheadle/a6b5f4c6df3218a586a9a043c1de18aa to your computer and use it in GitHub Desktop.
Using stripe api via clojure interop in a biff app
(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