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 }})))
0 commit comments