From 09a03835dcb3f24216be3114610e7a9059664342 Mon Sep 17 00:00:00 2001 From: Roman Rudakov Date: Mon, 26 Feb 2024 20:22:04 +0100 Subject: [PATCH] [#26] WIP: Add option to return embeds for payments --- deps.edn | 4 +- spec/com/adgoji/mollie.clj | 8 ++++ spec/com/adgoji/mollie/order.clj | 11 +++++ spec/com/adgoji/mollie/order_line.clj | 8 ++++ spec/com/adgoji/mollie/refund.clj | 26 ++++++++++++ spec/com/adgoji/mollie/refund/request.clj | 8 ++++ spec/com/adgoji/mollie/settlement.clj | 17 ++++++++ src/com/adgoji/mollie/api.clj | 13 ++++-- src/com/adgoji/mollie/api/payments.clj | 16 +++++--- src/com/adgoji/mollie/client.clj | 2 +- test/com/adgoji/mollie/api_test.clj | 49 +++++++++++++++++------ 11 files changed, 136 insertions(+), 26 deletions(-) create mode 100644 spec/com/adgoji/mollie/order.clj create mode 100644 spec/com/adgoji/mollie/order_line.clj create mode 100644 spec/com/adgoji/mollie/refund.clj create mode 100644 spec/com/adgoji/mollie/refund/request.clj create mode 100644 spec/com/adgoji/mollie/settlement.clj diff --git a/deps.edn b/deps.edn index e68b693..e0edb32 100644 --- a/deps.edn +++ b/deps.edn @@ -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"}} diff --git a/spec/com/adgoji/mollie.clj b/spec/com/adgoji/mollie.clj index 464977f..3b328e8 100644 --- a/spec/com/adgoji/mollie.clj +++ b/spec/com/adgoji/mollie.clj @@ -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) diff --git a/spec/com/adgoji/mollie/order.clj b/spec/com/adgoji/mollie/order.clj new file mode 100644 index 0000000..9b2e998 --- /dev/null +++ b/spec/com/adgoji/mollie/order.clj @@ -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) diff --git a/spec/com/adgoji/mollie/order_line.clj b/spec/com/adgoji/mollie/order_line.clj new file mode 100644 index 0000000..07e2e61 --- /dev/null +++ b/spec/com/adgoji/mollie/order_line.clj @@ -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) diff --git a/spec/com/adgoji/mollie/refund.clj b/spec/com/adgoji/mollie/refund.clj new file mode 100644 index 0000000..4328e28 --- /dev/null +++ b/spec/com/adgoji/mollie/refund.clj @@ -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?) diff --git a/spec/com/adgoji/mollie/refund/request.clj b/spec/com/adgoji/mollie/refund/request.clj new file mode 100644 index 0000000..c74fda4 --- /dev/null +++ b/spec/com/adgoji/mollie/refund/request.clj @@ -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?) diff --git a/spec/com/adgoji/mollie/settlement.clj b/spec/com/adgoji/mollie/settlement.clj new file mode 100644 index 0000000..c87c3e8 --- /dev/null +++ b/spec/com/adgoji/mollie/settlement.clj @@ -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?) diff --git a/src/com/adgoji/mollie/api.clj b/src/com/adgoji/mollie/api.clj index b5c165a..e5698f2 100644 --- a/src/com/adgoji/mollie/api.clj +++ b/src/com/adgoji/mollie/api.clj @@ -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 diff --git a/src/com/adgoji/mollie/api/payments.clj b/src/com/adgoji/mollie/api/payments.clj index a3dc0cb..da4b3a5 100644 --- a/src/com/adgoji/mollie/api/payments.clj +++ b/src/com/adgoji/mollie/api/payments.clj @@ -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`." diff --git a/src/com/adgoji/mollie/client.clj b/src/com/adgoji/mollie/client.clj index 33c691e..14462a0 100644 --- a/src/com/adgoji/mollie/client.clj +++ b/src/com/adgoji/mollie/client.clj @@ -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 diff --git a/test/com/adgoji/mollie/api_test.clj b/test/com/adgoji/mollie/api_test.clj index 3d2dbd3..e0c2261 100644 --- a/test/com/adgoji/mollie/api_test.clj +++ b/test/com/adgoji/mollie/api_test.clj @@ -21,19 +21,23 @@ (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 @@ -41,7 +45,9 @@ (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)] @@ -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)] @@ -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) @@ -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) @@ -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)] @@ -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)