/
sources.clj
73 lines (66 loc) · 2.46 KB
/
sources.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(ns zebra.sources
(:refer-clojure :exclude [update])
(:require [zebra.utils :refer [transform-params
transform-type-data]])
(:import [com.stripe.model Source Source$Card]
[com.stripe.net RequestOptions]
[java.util Map]))
(def three-d-secure-requirements
{:required "required"
:recommended "recommended"
:optional "optional"
:not-supported "not_supported"})
(def status-codes
{:pending "pending"
:chargeable "chargeable"
:failed "failed"})
(defn- card-source->map [^Source$Card card]
{:address_line1_check (.getAddressLine1Check card)
:address_zip_check (.getAddressZipCheck card)
:brand (.getBrand card)
:country (.getCountry card)
:cvc_check (.getCvcCheck card)
:dynamic_last4 (.getDynamicLast4 card)
:exp_month (.getExpMonth card)
:exp_year (.getExpYear card)
:fingerprint (.getFingerprint card)
:funding (.getFunding card)
:last4 (.getLast4 card)
:name (.getName card)
:three_d_secure (.getThreeDSecure card)
:tokenization_method (.getTokenizationMethod card)})
(defn source->map [source]
(merge
{:id (.getId source)
:customer (.getCustomer source)
:status (.getStatus source)
:type (.getType source)}
(when (= "card" (.getType source))
(let [card (card-source->map (.getCard source))]
;; TODO: deprecate `type-data`, it does not match API.
{:type-data card
:card card}))
(when-let [redirect (.getRedirect source)]
{:redirect {:url (.getUrl redirect)
:return-url (.getReturnUrl redirect)
:status (.getStatus redirect)
:failure-reason (.getFailureReason redirect)}})))
(defn create
[params api-key]
(source->map
(Source/create ^Map (transform-params params)
(-> (RequestOptions/builder) (.setApiKey api-key) .build))))
(defn create-old
[params api-key]
(try
;; TODO: Why does this catch the exception?
(source->map
(Source/create ^Map (transform-params params)
(-> (RequestOptions/builder) (.setApiKey api-key) .build)))
(catch Exception e
(throw (ex-info "Failed to create stripe source" {:error e})))))
(defn retrieve
[id api-key]
(source->map
(Source/retrieve id
(-> (RequestOptions/builder) (.setApiKey api-key) .build))))