-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
159 lines (113 loc) · 3.75 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
(ns moy-nalog.core
(:require
[moy-nalog.const :as const]
[moy-nalog.util :as util]
[cheshire.core :as json]
[org.httpkit.client :as http]
[clojure.math :as math]
[clojure.string :as str]))
(defn get-token
[{:keys [login password]}]
(let [device-id
(util/random-id 21)
device-info
{:sourceDeviceId device-id
:sourceType "WEB"
:appVersion "1.0.0"
:metaDetails
{:userAgent const/USER-AGENT}}
{:keys [body]}
@(http/request
{:method :post
:url (str const/ENDPOINT "/auth/lkfl")
:headers
(assoc const/HEADERS "referer" "https://lknpd.nalog.ru")
:body
(json/generate-string
{:username login
:password password
:deviceInfo
device-info})})
{:keys [refreshToken]}
(json/parse-string body true)
{:keys [body]}
@(http/request
{:method :post
:url (str const/ENDPOINT "/auth/token")
:headers
(assoc const/HEADERS "referer" "https://lknpd.nalog.ru/sales")
:body
(json/generate-string
{:deviceInfo
device-info
:refreshToken
refreshToken})})
{:keys [token] :as response}
(json/parse-string body true)]
token))
(defn api-request
([config api-method http-method]
(api-request config api-method http-method nil))
([config api-method http-method payload]
(let
[{:keys [body]}
@(http/request
{:url
(str const/ENDPOINT "/" (name api-method))
:method
http-method
:headers
(assoc const/HEADERS
"authorization" (str "Bearer " (get-token config))
"referer" "https://lknpd.nalog.ru/sales/create")
:body (json/generate-string payload)})]
(json/parse-string body true))))
(defn add-income
([config title amount]
(add-income config title amount nil))
([config title amount
{:keys [paymentType
ignoreMaxTotalIncomeRestriction
contactPhone
displayName
incomeType
inn
quantity
operationTime
timezone]
:or {timezone "+03:00"
paymentType "CASH"
ignoreMaxTotalIncomeRestriction false
incomeType "FROM_INDIVIDUAL"
quantity 1
operationTime (util/make-timestamp timezone)}}]
{:pre [(contains?
#{"FROM_FOREIGN_AGENCY" "FROM_INDIVIDUAL" "FROM_LEGAL_ENTITY"}
incomeType)
(contains?
#{"CASH" "WIRE"}
paymentType)]}
(api-request config :income :post
{:paymentType paymentType
:ignoreMaxTotalIncomeRestriction ignoreMaxTotalIncomeRestriction
:client
{:contactPhone contactPhone
:displayName displayName
:incomeType incomeType
:inn inn}
:requestTime (util/make-timestamp timezone)
:operationTime operationTime
:services
[{:name title
:amount (util/money-format amount)
:quantity quantity}]
:totalAmount
(util/money-format (* amount quantity))})))
(defn user-info
[config]
(api-request config :user :get))
(comment
(def CONFIG {:login "340000000000" :password "..."})
(add-income CONFIG "Сведение" 4000)
(user-info CONFIG)
)