Skip to content

Instantly share code, notes, and snippets.

@WennderSantos
Last active June 2, 2019 03:35
Show Gist options
  • Save WennderSantos/4e0d9b403c34d274af08929016008bc6 to your computer and use it in GitHub Desktop.
Save WennderSantos/4e0d9b403c34d274af08929016008bc6 to your computer and use it in GitHub Desktop.
Transaction authorization
(defn- is-card-blocked?
[{:keys [account]}]
(when (:cardIsBlocked account)
"CARD BLOCKED"))
(defn- has-no-limit?
[{:keys [account transaction]}]
(when (> (:amount transaction) (:limit account))
"NO LIMIT AVAILABLE"))
(defn- is-first-transaction?
[last-transactions]
(empty? last-transactions))
(defn- is-transaction-amount-above-90%-limit?
[account transaction]
(> (:amount transaction) (* (:limit account) 0.9)))
(defn- is-transaction-amount-allowed?
[{:keys [account transaction last-transactions]}]
(when (and (is-first-transaction? last-transactions)
(is-transaction-amount-above-90%-limit? account transaction))
"TRANSACTION ABOVE 90% LIMIT IN FIRST TRANSACTION"))
(defn- is-merchant-on-deny-list?
[{:keys [account transaction]}]
(when (contains? (:denyList account) (:merchant transaction))
"MERCHANT ON DENY LIST"))
(defn- more-than-10-transactions-on-the-same-merchant?
[{:keys [last-transactions transaction]}]
(when (-> (filter #(= (:merchant transaction) (:merchant %)) last-transactions)
(count)
(> 10))
"MORE THAN 10 TRANSACTIONS ON THE SAME MERCHANT"))
(defn authorize
[account transaction last-transactions]
(let [args {:account account
:transaction transaction
:last-transactions last-transactions}
denied-reasons (keep #(% args) [has-no-limit?
is-card-blocked?
is-transaction-amount-allowed?
more-than-10-transactions-on-the-same-merchant?
is-merchant-on-deny-list?])]
(if (empty? denied-reasons)
{:approved true
:deniedReasons []
:newLimit (- (:limit account) (:amount transaction))}
{:approved false
:deniedReasons (into [] denied-reasons)
:newLimit (:limit account)})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment