biff

2024-03-27T16:02:02.947389Z

Here's an example of using the stripe api to create a checkout session

(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))))))

2024-03-27T17:25:06.887779Z

Maybe this is too wordy for your tastes, but I take comfort from its officialness

2024-03-27T18:26:02.236699Z

Nice! Would you want to publish it in a gist/blog post? Then I can link to it from https://biffweb.com/docs/library/

2024-03-27T20:21:28.876349Z

sure -- https://gist.github.com/laheadle/a6b5f4c6df3218a586a9a043c1de18aa

👌 1