Skip to content

Commit

Permalink
[#26] WIP: Add option to return embeds for payments
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-rudakov committed Feb 26, 2024
1 parent 469871f commit 09a0383
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 26 deletions.
4 changes: 2 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{:paths ["src" "spec"]

:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.clojure/data.json {:mvn/version "2.4.0"}
org.clojure/spec.alpha {:mvn/version "0.3.218"}
org.clojure/data.json {:mvn/version "2.5.0"}
org.clojure/spec.alpha {:mvn/version "0.4.233"}
camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.3"}
hato/hato {:mvn/version "0.9.0"}
com.cognitect/anomalies {:mvn/version "0.1.12"}}
Expand Down
8 changes: 8 additions & 0 deletions spec/com/adgoji/mollie.clj
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@
::pagination/previous
::pagination/self]))

(s/def ::embed-val #{:captures
:refunds
:chargebacks})

(s/def ::embed
(s/coll-of ::embed-val :into [] :distinct true))


;;; Mandate

(defmulti mandate-spec ::mandate/method)
Expand Down
11 changes: 11 additions & 0 deletions spec/com/adgoji/mollie/order.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(ns com.adgoji.mollie.order
(:require
[clojure.spec.alpha :as s]
[com.adgoji.common :as common]))

(s/def ::resource #{"order"})
(s/def ::id string?)
;; TODO: Add profiles
(s/def ::profile-id string?)
(s/def ::method ::common/payment-method)
(s/def ::mode ::common/mode)
8 changes: 8 additions & 0 deletions spec/com/adgoji/mollie/order_line.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns com.adgoji.mollie.order-line
(:require
[clojure.spec.alpha :as s]
[com.adgoji.mollie.order :as order]))

(s/def ::resource #{"orderline"})
(s/def ::id string?)
(s/def ::order-id ::order/id)
26 changes: 26 additions & 0 deletions spec/com/adgoji/mollie/refund.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns com.adgoji.mollie.refund
(:require
[clojure.spec.alpha :as s]
[com.adgoji.mollie.amount :as amount]
[com.adgoji.mollie.payment :as payment]
[com.adgoji.mollie.settlement :as settlement]))

(s/def ::resource #{"refund"})
(s/def ::id string?)
(s/def ::amount (s/keys :req [::amount/currency ::amount/value]))
(s/def ::description string?)
(s/def ::metadata map?)
(s/def ::status
#{:queued
:pending
:processing
:refunded
:failed
:canceled})
(s/def ::payment-id ::payment/id)
(s/def ::created-at inst?)
(s/def ::settlement-id ::settlement/id)
(s/def ::settlement-amount ::settlement/amount)
;; TODO: Replace with order
(s/def ::order-id string?)
(s/def ::lines any?)
8 changes: 8 additions & 0 deletions spec/com/adgoji/mollie/refund/request.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns com.adgoji.mollie.refund.request
(:require
[clojure.spec.alpha :as s]
[com.adgoji.mollie.amount :as amount]))

(s/def ::amount (s/keys :req-un [::amount/currency ::amount/value]))
(s/def ::description string?)
(s/def ::metadata map?)
17 changes: 17 additions & 0 deletions spec/com/adgoji/mollie/settlement.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns com.adgoji.mollie.settlement
(:require
[clojure.spec.alpha :as s]
[com.adgoji.mollie.amount :as amount]))

(s/def ::resource #{"settlement"})
(s/def ::id string?)
(s/def ::reference string?)
(s/def ::created-at inst?)
(s/def ::settled-at inst?)
(s/def ::status
#{:open
:pending
:paidout
:failed})
(s/def ::amount (s/keys :req [::amount/currency ::amount/value]))
(s/def ::periods map?)
13 changes: 9 additions & 4 deletions src/com/adgoji/mollie/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,17 @@

(defn get-payment-by-id
"Fetch a single payment by `payment-id`."
[client payment-id]
(payments/get-by-id client payment-id))
([client payment-id]
(get-payment-by-id client payment-id {}))
([client payment-id opts]
(payments/get-by-id client payment-id opts)))

(s/fdef get-payment-by-id
:args (s/cat :client map?
:payment-id ::payment/id)
:args (s/or :minimal (s/cat :client map?
:payment-id ::payment/id)
:with-opts (s/cat :client map?
:payment-id ::payment/id
:opts (s/keys :opt-un [::mollie/embed])))
:ret ::mollie/payment)

(defn update-payment-by-id
Expand Down
16 changes: 10 additions & 6 deletions src/com/adgoji/mollie/api/payments.clj
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,16 @@

(defn get-by-id
"Fetch a single payment by `payment-id`."
[client payment-id]
(mollie.client/http-get client
(str "/v2/payments/"
(spec/check payment-id ::payment/id))
{:response-transformer transform-payment
:spec ::mollie/payment}))
[client payment-id {:keys [embed]}]
(let [embed-strs (into [] (map name) embed)
opts (cond-> {:response-transformer transform-payment
:spec ::mollie/payment}
(seq embed-strs)
(assoc :query-params {:embed embed-strs}))]
(mollie.client/http-get client
(str "/v2/payments/"
(spec/check payment-id ::payment/id))
opts)))

(defn update-by-id
"Update a single payment by `payment-id`."
Expand Down
2 changes: 1 addition & 1 deletion src/com/adgoji/mollie/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
(cond-> body
:always (response-transformer)
check-response? (spec/check spec))
(let [anomaly (case status
(let [anomaly (case (long status)
(400 401 422) ::anomalies/incorrect
403 ::anomalies/forbidden
(404 410) ::anomalies/not-found
Expand Down
49 changes: 36 additions & 13 deletions test/com/adgoji/mollie/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,33 @@
(use-fixtures :once utils/with-mollie-client)

(defn- ensure-customer []
(if-let [customers (->> (sut/get-customers-list utils/*mollie-client* {})
(if-let [customers (->> (sut/get-customers-list utils/*mollie-client*
{:limit 50})
::mollie/customers
seq)]
(rand-nth customers)
(sut/create-customer utils/*mollie-client* {:metadata utils/default-metadata})))

(defn- ensure-customer-without-mandate []
(if-let [customers (->> (sut/get-customers-list utils/*mollie-client* {})
(if-let [customers (->> (sut/get-customers-list utils/*mollie-client*
{:limit 50})
::mollie/customers
(sequence (remove ::link/mandates))
seq)]
(let [customer (rand-nth customers)]
(if-not (->> (sut/get-mandates-list utils/*mollie-client* (::customer/id customer) {})
(if-not (->> (sut/get-mandates-list utils/*mollie-client*
(::customer/id customer)
{:limit 50})
::mollie/mandates
seq)
customer
(recur)))
(sut/create-customer utils/*mollie-client* {:metadata utils/default-metadata})))

(defn- ensure-mandate [customer-id]
(if-let [mandates (->> (sut/get-mandates-list utils/*mollie-client* customer-id {})
(if-let [mandates (->> (sut/get-mandates-list utils/*mollie-client*
customer-id
{:limit 50})
::mollie/mandates
(sequence (filter #(= (::mandate/status %) :valid)))
seq)]
Expand All @@ -57,7 +63,9 @@
customer-id (::customer/id customer)
mandate (ensure-mandate customer-id)
subscription (if-let [subscriptions
(->> (sut/get-subscriptions-list utils/*mollie-client* customer-id {})
(->> (sut/get-subscriptions-list utils/*mollie-client*
customer-id
{:limit 50})
::mollie/subscriptions
(sequence (filter #(= (::subscription/status %) :active)))
seq)]
Expand All @@ -77,7 +85,8 @@

(defn- ensure-payment
([]
(if-let [payments (->> (sut/get-payments-list utils/*mollie-client* {})
(if-let [payments (->> (sut/get-payments-list utils/*mollie-client*
{:limit 50})
::mollie/payments
seq)]
(rand-nth payments)
Expand All @@ -88,7 +97,9 @@
:description "Auto generated test payment"
:redirect-url "https://example.com"})))
([customer-id]
(if-let [payments (->> (sut/get-payments-list utils/*mollie-client* customer-id {})
(if-let [payments (->> (sut/get-payments-list utils/*mollie-client*
customer-id
{:limit 50})
::mollie/payments
seq)]
(rand-nth payments)
Expand All @@ -101,7 +112,8 @@
customer-id))))

(defn- ensure-cancelable-payment []
(if-let [payments (->> (sut/get-payments-list utils/*mollie-client* {})
(if-let [payments (->> (sut/get-payments-list utils/*mollie-client*
{:limit 50})
::mollie/payments
(sequence (filter ::payment/is-cancelable))
seq)]
Expand Down Expand Up @@ -510,11 +522,22 @@
::directdebit/signature-date)))))))

(deftest get-payment-by-id-test
(let [payment (ensure-payment)]
(is (= (assoc payment
::link/documentation {::link/type "text/html"
::link/href "https://docs.mollie.com/reference/v2/payments-api/get-payment"})
(sut/get-payment-by-id utils/*mollie-client* (::payment/id payment))))))
#_(testing "Default options"
(let [payment (ensure-payment)]
(is (= (assoc payment
::link/documentation {::link/type "text/html"
::link/href "https://docs.mollie.com/reference/v2/payments-api/get-payment"})
(sut/get-payment-by-id utils/*mollie-client* (::payment/id payment))))))

(testing "With embeds"
(let [payment (ensure-payment)]
(is (= (-> payment
(assoc ::link/documentation {::link/type "text/html"
::link/href "https://docs.mollie.com/reference/v2/payments-api/get-payment"})
(update-in [::link/self ::link/href] (fn [href] (str href "?embed=chargebacks"))))
(sut/get-payment-by-id utils/*mollie-client*
(::payment/id payment)
{:embed [:captures :refunds :chargebacks]}))))))

(deftest update-payment-by-id-test
(let [payment (ensure-payment)
Expand Down

0 comments on commit 09a0383

Please sign in to comment.