diff --git a/src/wc_api_clj/v3/order_refunds.clj b/src/wc_api_clj/v3/order_refunds.clj new file mode 100644 index 0000000..6f5c51f --- /dev/null +++ b/src/wc_api_clj/v3/order_refunds.clj @@ -0,0 +1,55 @@ +(ns wc-api-clj.v3.order-refunds + "Helper functions to communicate with the WooCommerce REST API's order refund endpoints. + These functions need authentication by `consumer_key` and `consumer_secret`.
+ https://woocommerce.github.io/woocommerce-rest-api-docs/#order-refunds" + (:require [wc-api-clj.core :as woo] + [wc-api-clj.util :as util])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Order Refunds REST API v3 wrapper functions ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn create-order-refund + "Create an order refund." + [{:keys [url consumer_key consumer_secret order api_refund body exception insecure]}] + (try (woo/post-req {:siteurl url + :uri (str "/wp-json/wc/v3/orders/" order "/refunds" (util/edn-to-query-str {:api_refund api_refund})) + :username consumer_key + :password consumer_secret + :body body + :exception (not (not exception)) + :insecure (not (not insecure))}) + (catch clojure.lang.ExceptionInfo e (str (.getMessage e))))) + +(defn get-order-refund-by-id + "Retrieve an order refund by the order and refund ID." + [{:keys [url consumer_key consumer_secret order refund exception insecure]}] + (try (woo/get-req {:siteurl url + :uri (str "/wp-json/wc/v3/orders/" order "/refunds/" refund) + :username consumer_key + :password consumer_secret + :exception (not (not exception)) + :insecure (not (not insecure))}) + (catch clojure.lang.ExceptionInfo e (str (.getMessage e))))) + +(defn get-order-refunds + "Retrieve all order refunds." + [{:keys [url consumer_key consumer_secret order exception insecure]}] + (try (woo/get-req {:siteurl url + :uri (str "/wp-json/wc/v3/orders/" order "/refunds") + :username consumer_key + :password consumer_secret + :exception (not (not exception)) + :insecure (not (not insecure))}) + (catch clojure.lang.ExceptionInfo e (str (.getMessage e))))) + +(defn delete-order-refund-by-id + "Delete an order by the order and refund ID." + [{:keys [url consumer_key consumer_secret order refund exception insecure]}] + (try (woo/delete-req {:siteurl url + :uri (str "/wp-json/wc/v3/orders/" order "/refunds/" refund (util/edn-to-query-str {:force true})) + :username consumer_key + :password consumer_secret + :exception (not (not exception)) + :insecure (not (not insecure))}) + (catch clojure.lang.ExceptionInfo e (str (.getMessage e))))) diff --git a/test/wc_api_clj/core_test.clj b/test/wc_api_clj/core_test.clj index 209cee2..5ab5ee0 100644 --- a/test/wc_api_clj/core_test.clj +++ b/test/wc_api_clj/core_test.clj @@ -47,6 +47,7 @@ :shipping_lines [{:method_id "flat_rate" :method_title "Flat Rate" :total "10.00"}]} + :refund_to_create {:amount "5"} :ckr "ck_86e637eef49ca8f2c864ad7b84dc17b0a7b78750" :csr "cs_0890a046f5b9a30825970a65d8ab8e9c7fa0deb6" :ckw "ck_469b67ce2e12bd1a1b290abf4b1c108e61b4f71c" diff --git a/test/wc_api_clj/v3/order_refunds_test.clj b/test/wc_api_clj/v3/order_refunds_test.clj new file mode 100644 index 0000000..ab36636 --- /dev/null +++ b/test/wc_api_clj/v3/order_refunds_test.clj @@ -0,0 +1,66 @@ +(ns wc-api-clj.v3.order-refunds-test + (:require [clojure.test :refer :all] + [wc-api-clj.v3.orders :refer :all] + [wc-api-clj.v3.order-refunds :refer :all] + [wc-api-clj.core-test :refer [credentials]] + [clojure.data.json :as json])) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Test for v3/order-refunds API ;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(deftest order-refund-v3-api-create-read-delete-success-test + (testing "Testing order-refund v3 API for create, read and delete." + (let [order (-> {:url (:siteurl credentials) + :consumer_key (:ckw credentials) + :consumer_secret (:csw credentials) + :exception false + :insecure true + :body (json/write-str (:order_to_create credentials))} + (create-order))] + (is (< 0 (:id order))) + (let [refund (-> {:url (:siteurl credentials) + :consumer_key (:ckw credentials) + :consumer_secret (:csw credentials) + :exception false + :order (:id order) + :api_refund false + :insecure true + :body (json/write-str {:refund_to_create credentials})} + create-order-refund)] + (is (< 0 (:id refund))) + (is (< 0 (-> {:url (:siteurl credentials) + :consumer_key (:ckr credentials) + :consumer_secret (:csr credentials) + :order (:id order) + :exception false + :insecure true} + get-order-refunds + first + :id))) + (is (= (:id refund) (-> {:url (:siteurl credentials) + :consumer_key (:ckr credentials) + :consumer_secret (:csr credentials) + :order (:id order) + :refund (:id refund) + :exception false + :insecure true} + get-order-refund-by-id + :id))) + (is (= (:id refund) (-> {:url (:siteurl credentials) + :consumer_key (:ckw credentials) + :consumer_secret (:csw credentials) + :order (:id order) + :refund (:id refund) + :exception false + :insecure true} + delete-order-refund-by-id + :id)))) + (is (= (:id order) (-> {:url (:siteurl credentials) + :consumer_key (:ckw credentials) + :consumer_secret (:csw credentials) + :order (:id order) + :exception false + :insecure true} + delete-order-by-id + :id))))))