Skip to content

Commit

Permalink
Merge pull request #22 from codemascot/f/13/refunds
Browse files Browse the repository at this point in the history
FIX #13
  • Loading branch information
khanrn authored Jan 16, 2021
2 parents f5f7f14 + 0123e40 commit 7f8c56e
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/wc_api_clj/v3/order_refunds.clj
Original file line number Diff line number Diff line change
@@ -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`.</br>
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)))))
1 change: 1 addition & 0 deletions test/wc_api_clj/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
66 changes: 66 additions & 0 deletions test/wc_api_clj/v3/order_refunds_test.clj
Original file line number Diff line number Diff line change
@@ -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))))))

0 comments on commit 7f8c56e

Please sign in to comment.