-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
64 lines (56 loc) · 1.8 KB
/
core.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(ns payeer.core
(:import
java.util.Base64
java.net.URLEncoder)
(:require
[url-utils.core :refer [encode-params]]
[org.httpkit.client :as http]
[clojure.string :as string]
[digest :as digest]))
; base url
(defonce url "https://payeer.com/merchant/")
; aproved ip adresses
(defonce ips ["185.71.65.92" "185.71.65.189" "149.202.17.210"])
(defn- encode
[to-encode]
(.encodeToString (Base64/getEncoder) (.getBytes to-encode)))
(defn- format-amount
[amount]
(format "%.2f" (float amount)))
(defn- sign
"Hash map to string with ':' separated values "
[payload]
(->> payload
vals
(string/join ":")
digest/sha-256
string/upper-case))
(defn generate-merchant-link
"Generate link for replanish balance"
[{:keys [shop-id order-id payeer-key]}
{:keys [amount currency description]}
{:keys [lang]}]
(let [wrap-sign #(assoc %1 :m_sign (sign (assoc %1 :m_key payeer-key)))
make #(str url "?" (encode-params %1))]
(-> {:m_shop shop-id
:m_orderid order-id
:m_amount (format-amount amount)
:m_curr currency
:m_desc (encode description)}
wrap-sign
make
(str "&lang=" lang))))
(defn ips-valid
"Check if ip address in range 185.71.65.92, 185.71.65.189, 149.202.17.210"
[{:keys [remote-addr]}]
(when-not (.indexOf ips remote-addr)
(throw (Exception. "IP address of request out of range"))))
(defn handler
"Handler for 'status' endpoint"
[api-key {:keys [body] :as request} callback]
(ips-valid request)
(when-not (and (nil? (:m_operation_id body)) (nil? (:m_sign body)))
(let [hash (-> (assoc body :m_key api-key) sign)]
(if (and (= (:m_sign body) hash) (= (:m_status body) "success"))
(callback "success" (:m_orderid body))
(callback "fail" (:m_orderid body))))))