Skip to content

Commit 8560c1b

Browse files
committed
order to xml
1 parent 444c289 commit 8560c1b

File tree

4 files changed

+79
-26
lines changed

4 files changed

+79
-26
lines changed

clojure/wheel/project.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
[clj-http "3.10.0"]
1616
[com.ibm.mq/com.ibm.mq.allclient "9.1.0.0"]
1717
[clojurewerkz/quartzite "2.1.0"]
18-
[funcool/cuerdas "2.0.5"]]
18+
[funcool/cuerdas "2.0.5"]
19+
[org.clojure/data.xml "0.0.8"]]
1920
:main ^:skip-aot wheel.core
2021
:target-path "target/%s"
2122
:profiles {:uberjar {:aot :all}

clojure/wheel/src/wheel/oms/address.clj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,4 @@
1111
(s/def ::pincode (s/int-in 110001 855118))
1212

1313
(s/def ::address (s/keys :req-un [::first-name ::line1 ::city ::state ::pincode]
14-
:opt-un [::last-name ::line2]))
15-
16-
(s/def ::shipping ::address)
17-
(s/def ::billing ::address)
14+
:opt-un [::last-name ::line2]))

clojure/wheel/src/wheel/oms/order.clj

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,69 @@
33
[wheel.oms.address :as addr]
44
[wheel.oms.payment :as payment]
55
[wheel.oms.order-line :as order-line]
6-
[wheel.string :as w-str]))
6+
[wheel.string :as w-str]
7+
[clojure.data.xml :as xml]))
78

89
(s/def ::order-no w-str/not-blank?)
910
(s/def ::payments (s/coll-of ::payment/payment :min-count 1))
1011
(s/def ::order-lines (s/coll-of ::order-line/order-line :min-count 1))
11-
(s/def ::order (s/keys :req-un [::order-no ::addr/shipping ::addr/billing ::payments
12+
(s/def ::billing-address ::addr/address)
13+
(s/def ::shipping-address ::addr/address)
14+
(s/def ::order (s/keys :req-un [::order-no ::shipping-address ::billing-address ::payments
1215
::order-lines]))
1316

17+
(defn address-to-xml [{:keys [first-name last-name line1
18+
line2 city state pincode]}]
19+
{:attrs {:FirstName first-name
20+
:LastName last-name
21+
:State state
22+
:City city
23+
:Pincode pincode}
24+
:ext [:Extn {:IRLAddressLine1 line1
25+
:IRLAddressLine2 line2}]})
26+
27+
(defn to-xml [order]
28+
{:pre [(s/assert ::order order)]}
29+
(let [{:keys [order-no billing-address order-lines
30+
shipping-address payments]} order
31+
{bill-to-attrs :attrs
32+
bill-to-ext :ext} (address-to-xml billing-address)
33+
{ship-to-attrs :attrs
34+
ship-to-ext :ext} (address-to-xml shipping-address)]
35+
(-> [:Order {:OrderNo order-no}
36+
[:PersonInfoBillTo bill-to-attrs bill-to-ext]
37+
[:PersonInfoShipTo ship-to-attrs ship-to-ext]
38+
[:PaymentDetailsList
39+
(map (fn [{:keys [amount reference-id]}]
40+
[:PaymentDetails {:ProcessedAmount amount
41+
:Reference1 reference-id}]) payments)]
42+
[:OrderLines
43+
(map (fn [{:keys [id sale-price]}]
44+
[:OrderLine
45+
[:Item {:ItemID id}]
46+
[:LinePriceInfo {:LineTotal sale-price}]])
47+
order-lines)]]
48+
xml/sexp-as-element
49+
xml/indent-str)))
50+
1451
(comment
15-
(s/valid? ::order {:order-no "181219-001-345786"
16-
:payments [{:amount 900M
17-
:reference-id "000000-1545216772601"}]
18-
:order-lines [{:id "200374"
19-
:sale-price 900M}]
20-
:billing {:first-name "Tamizhvendan"
21-
:last-name "Sembiyan"
22-
:line1 "Plot No 222"
23-
:line2 "Ashok Nagar 42nd Street"
24-
:city "Chennai"
25-
:state "TamilNadu"
26-
:pincode 600001}
27-
:shipping {:first-name "Tamizhvendan"
28-
:last-name "Sembiyan"
29-
:line1 "Plot No 222"
30-
:line2 "Ashok Nagar 42nd Street"
31-
:city "Chennai"
32-
:state "TamilNadu"
33-
:pincode 600001}}))
52+
(s/check-asserts true)
53+
(spit "test.xml" (to-xml {:order-no "181219-001-345786"
54+
:payments [{:amount 900M
55+
:reference-id "000000-1545216772601"}]
56+
:order-lines [{:id "200374"
57+
:sale-price 900M}]
58+
:billing-address {:first-name "Tamizhvendan"
59+
:last-name "Sembiyan"
60+
:line1 "Plot No 222"
61+
:line2 "Ashok Nagar 42nd Street"
62+
:city "Chennai"
63+
:state "TamilNadu"
64+
:pincode 600001}
65+
:shipping-address {:first-name "Tamizhvendan"
66+
:last-name "Sembiyan"
67+
:line1 "Plot No 222"
68+
:line2 "Ashok Nagar 42nd Street"
69+
:city "Chennai"
70+
:state "TamilNadu"
71+
:pincode 600001}})))

clojure/wheel/test.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?><Order OrderNo="181219-001-345786">
2+
<PersonInfoBillTo FirstName="Tamizhvendan" LastName="Sembiyan" State="TamilNadu" City="Chennai" Pincode="600001">
3+
<Extn IRLAddressLine1="Plot No 222" IRLAddressLine2="Ashok Nagar 42nd Street"/>
4+
</PersonInfoBillTo>
5+
<PersonInfoShipTo FirstName="Tamizhvendan" LastName="Sembiyan" State="TamilNadu" City="Chennai" Pincode="600001">
6+
<Extn IRLAddressLine1="Plot No 222" IRLAddressLine2="Ashok Nagar 42nd Street"/>
7+
</PersonInfoShipTo>
8+
<PaymentDetailsList>
9+
<PaymentDetails ProcessedAmount="900" Reference1="000000-1545216772601"/>
10+
</PaymentDetailsList>
11+
<OrderLines>
12+
<OrderLine>
13+
<Item ItemID="200374"/>
14+
<LinePriceInfo LineTotal="900"/>
15+
</OrderLine>
16+
</OrderLines>
17+
</Order>

0 commit comments

Comments
 (0)