Line data Source code
1 4 : (ns schemas.api.payment-methods
2 : (:require
3 : [clojure.string :as str]))
4 :
5 :
6 9 : (defn- non-blank? [value] (not (str/blank? value)))
7 :
8 :
9 4 : (defn- http-url?
10 : [value]
11 0 : (try
12 0 : (let [uri (java.net.URI. value)
13 0 : scheme (some->
14 0 : (.getScheme uri)
15 0 : str/lower-case)]
16 0 : (and (contains? #{"http" "https"} scheme) (not (str/blank? (.getHost uri)))))
17 : (catch Exception _ false)))
18 :
19 :
20 4 : (defn luhn-valid?
21 : [value]
22 9 : (let [digits (mapv #(Character/digit ^char % 10) value)
23 5 : parity (mod (count digits) 2)]
24 7 : (and
25 3 : (seq digits)
26 7 : (every? #(<= 0 %) digits)
27 : (zero?
28 3 : (mod
29 3 : (reduce-kv
30 1 : (fn [sum index digit]
31 12 : (let [digit (if (= parity (mod index 2)) (* 2 digit) digit)]
32 7 : (+ sum (if (> digit 9) (- digit 9) digit))))
33 : 0
34 1 : digits)
35 : 10)))))
36 :
37 :
38 4 : (defn iban-valid?
39 : [value]
40 5 : (and
41 5 : (boolean (re-matches #"UA\d{27}" value))
42 : (=
43 : 1
44 3 : (reduce
45 1 : (fn [remainder ch]
46 4 : (if (Character/isLetter ^char ch)
47 3 : (reduce
48 8 : #(mod (+ (* %1 10) %2) 97)
49 1 : remainder
50 13 : (map #(Character/digit ^char % 10) (str (- (int ch) 55))))
51 11 : (mod (+ (* remainder 10) (Character/digit ^char ch 10)) 97)))
52 : 0
53 11 : (str (subs value 4) (subs value 0 4))))))
54 :
55 :
56 4 : (def PaymentMethodRequestSchema
57 8 : [:and
58 20 : [:map {:swagger/title "PaymentMethodRequest"}
59 36 : [:kind [:enum "card" "iban" "link" "cash" "other"]]
60 8 : [:label
61 8 : [:and
62 28 : [:string {:min 1
63 : :max 64}]
64 20 : [:fn {:error/message "Must not be blank"}
65 4 : non-blank?]]]
66 20 : [:value {:optional true}
67 28 : [:maybe [:string {:max 256}]]]
68 20 : [:note {:optional true}
69 28 : [:maybe [:string {:max 256}]]]
70 20 : [:bank-id {:optional true}
71 28 : [:maybe [:string {:max 16}]]]
72 20 : [:bank-name {:optional true}
73 28 : [:maybe [:string {:max 64}]]]
74 20 : [:recipient-name {:optional true}
75 28 : [:maybe [:string {:max 128}]]]
76 20 : [:tax-id {:optional true}
77 28 : [:maybe [:string {:max 10}]]]
78 20 : [:purpose {:optional true}
79 28 : [:maybe [:string {:max 160}]]]]
80 20 : [:fn {:error/message "Value is required for every payment method except cash"}
81 4 : (fn [{:keys [kind value]}]
82 16 : (or (= "cash" kind) (and (string? value) (non-blank? value))))]
83 20 : [:fn {:error/message "Payment links must be valid HTTP(S) URLs"}
84 4 : (fn [{:keys [kind value]}]
85 8 : (or (not= "link" kind) (and (string? value) (http-url? (str/trim value)))))]])
86 :
87 :
88 4 : (def PaymentMethodOrderRequestSchema
89 20 : [:map {:swagger/title "PaymentMethodOrderRequest"}
90 8 : [:uuids
91 20 : [:vector {:distinct true}
92 12 : [:ref :types/UuidString]]]])
93 :
94 :
95 4 : (def PaymentMethodPathParamsSchema
96 20 : [:map {:swagger/title "PaymentMethodPathParams"}
97 20 : [:payment-method-uuid [:ref :types/UuidString]]])
98 :
99 :
100 4 : (def PaymentMethodSchema
101 8 : [:map
102 20 : [:uuid [:ref :types/UuidString]]
103 36 : [:kind [:enum "card" "iban" "link" "cash" "other"]]
104 12 : [:label :string]
105 20 : [:value [:maybe :string]]
106 20 : [:note [:maybe :string]]
107 20 : [:bank-id [:maybe :string]]
108 20 : [:bank-name [:maybe :string]]
109 20 : [:recipient-name [:maybe :string]]
110 20 : [:tax-id [:maybe :string]]
111 20 : [:purpose [:maybe :string]]
112 12 : [:position :int]])
113 :
114 :
115 4 : (def PaymentMethodListResponseSchema
116 76 : [:map [:status [:= 200]] [:body [:map [:message :string] [:data [:vector PaymentMethodSchema]]]]])
117 :
118 :
119 4 : (def PaymentMethodCreateResponseSchema
120 8 : [:or
121 68 : [:map [:status [:= 201]] [:body [:map [:message :string] [:data PaymentMethodSchema]]]]
122 68 : [:map [:status [:= 400]] [:body [:map [:message :string] [:errors :map]]]]])
123 :
124 :
125 4 : (def PaymentMethodUpdateResponseSchema
126 8 : [:or
127 68 : [:map [:status [:= 200]] [:body [:map [:message :string] [:data PaymentMethodSchema]]]]
128 56 : [:map [:status [:= 404]] [:body [:map [:message :string]]]]
129 68 : [:map [:status [:= 400]] [:body [:map [:message :string] [:errors :map]]]]])
130 :
131 :
132 4 : (def PaymentMethodDeleteResponseSchema
133 8 : [:or
134 8 : [:map
135 20 : [:status [:= 200]]
136 64 : [:body [:map [:message :string] [:data [:map [:uuid [:ref :types/UuidString]]]]]]]
137 56 : [:map [:status [:= 404]] [:body [:map [:message :string]]]]])
138 :
139 :
140 4 : (def PaymentMethodOrderResponseSchema
141 68 : [:or PaymentMethodListResponseSchema [:map [:status [:= 400]] [:body [:map [:message :string]]]]])
142 :
143 :
144 4 : (def SelectedPaymentMethodSchema
145 20 : [:map {:swagger/title "SelectedPaymentMethod"}
146 20 : [:uuid [:ref :types/UuidString]]
147 36 : [:kind [:enum "card" "iban" "link" "cash" "other"]]
148 12 : [:label :string]
149 20 : [:bank-id [:maybe :string]]
150 20 : [:bank-name [:maybe :string]]
151 20 : [:last4 [:maybe :string]]
152 20 : [:owner-id [:ref :types/UuidString]]
153 20 : [:owner-name [:maybe :string]]
154 12 : [:own :boolean]])
155 :
156 :
157 4 : (def SelectedPaymentMethodListResponseSchema
158 8 : [:map
159 20 : [:status [:= 200]]
160 48 : [:body [:map [:message :string] [:data [:vector SelectedPaymentMethodSchema]]]]])
161 :
162 :
163 4 : (def SendPaymentDetailsRequestSchema
164 20 : [:map {:swagger/title "SendPaymentDetailsRequest"}
165 8 : [:payment-method-ids
166 36 : [:vector {:min 1
167 : :max 20
168 : :distinct true}
169 12 : [:ref :types/UuidString]]]])
170 :
171 :
172 4 : (def SendPaymentDetailsResponseSchema
173 8 : [:or
174 8 : [:map
175 20 : [:status [:= 202]]
176 64 : [:body [:map [:message :string] [:data [:map [:event-uuid [:ref :types/UuidString]]]]]]]
177 60 : [:map [:status [:enum 400 404]] [:body [:map [:message :string]]]]])
|