-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpayment.go
271 lines (233 loc) · 7.39 KB
/
payment.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package unitpay
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
type Payment struct {
paymentType PaymentType
account string
sum int
projectId string
resultUrl *string
desc string
secretKey string
currency *string
locale *string
signature *string
ip *string
backUrl *string
subscription *bool
subscriptionId *int
preauth *bool
preauthExpireLogic *int
test *bool
}
type PaymentCreationResultSuccess struct {
Message string `json:"message"`
PaymentId int64 `json:"paymentId"`
RecieptUrl string `json:"receiptUrl"`
ResponseType string `json:"type"`
RedirectUrl string `json:"redirectUrl,omitempty"`
Response string `json:"responce,omitempty"`
InvoiceId string `json:"invoiceId,omitempty"`
}
type PaymentCreationResultError struct {
Message string `json:"message"`
}
type PaymentCreationResult struct {
Success *PaymentCreationResultSuccess `json:"result,omitempty"`
Error *PaymentCreationResultError `json:"error,omitempty"`
}
type PaymentResult struct {
Method string
UnitpayId int
ProjectId string
Account string
PayerSum int
PayerCurrency string
Profit int
PaymentType string
OrderSum int
OrderCurrency string
Operator string
Date time.Time
ErrorMessage string
Test string
Phone *int64
ThreeDS *int
SubscriptionId *int
Signature *string
}
type PaymentResultResponseSuccess struct {
Message string `json:"message"`
}
type PaymentResultResponseError struct {
Message string `json:"message"`
}
type PaymentResultResponse struct {
Success *PaymentResultResponseSuccess `json:"result,omitempty"`
Error *PaymentResultResponseError `json:"error,omitempty"`
}
type PaymentInfoResponse struct {
Result *PaymentInfoResponseResult `json:"result,omitempty"`
Error *PaymentInfoResponseError `json:"error,omitempty"`
}
type UnitpayTime time.Time
func (j *UnitpayTime) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
var (
y, d, h, min, sec int
m time.Month
)
_, err := fmt.Sscanf(s, "%v-%v-%v %v:%v:%v", &y, &m, &d, &h, &min, &sec)
if err != nil {
return err
}
t := time.Date(y, m, d, h, min, sec, 0, time.UTC)
*j = UnitpayTime(t)
return nil
}
type PaymentInfoResponseResult struct {
PaymentId string `json:"paymentId"`
Status PaymentStatus `json:"status"`
PaymentType string `json:"paymentType"`
Date UnitpayTime `json:"-"`
Purse string `json:"purse"`
Account string `json:"account"`
Profit string `json:"profit"`
ProjectId int `json:"projectId"`
OrderSum string `json:"orderSum"`
OrderCurrency string `json:"orderCurrency"`
PayerSum string `json:"payerSum"`
PayerCurrency string `json:"payerCurrency"`
AvailableForRefund string `json:"availableForRefund"`
IsPreauth int `json:"isPreauth"`
Refunds []interface{} `json:"refunds"`
ReceiptUrl string `json:"receiptUrl,omitempty"`
ErrorMessage string `json:"errorMessage,omitempty"`
}
type PaymentInfoResponseError struct {
Message string `json:"message"`
}
func CreatePayment(paymentType PaymentType, account string, sum int, projectID string, description string, resultURL *string, secretKey string) *Payment {
return &Payment{
paymentType: paymentType,
account: account,
sum: sum,
projectId: projectID,
desc: description,
resultUrl: resultURL,
secretKey: secretKey,
}
}
func (payment *Payment) SetSignature(signature string) {
payment.signature = &signature
}
func (payment *Payment) GetSignature() string {
return *payment.signature
}
func (payment *Payment) SetLocale(locale string) {
payment.locale = &locale
}
func (payment *Payment) SetCurrency(currency string) {
payment.currency = ¤cy
}
func (payment *Payment) GetCurrency() string {
return *payment.currency
}
func (payment *Payment) SetIPAddress(IPAddress string) {
payment.ip = &IPAddress
}
func (payment *Payment) SetBackURL(backUrl string) {
payment.backUrl = &backUrl
}
func (payment *Payment) SetSubscription(subscription bool) {
payment.subscription = &subscription
}
func (payment *Payment) SetSubscriptionID(subscriptionId int) {
payment.subscriptionId = &subscriptionId
}
func (payment *Payment) SetPreAuth(preauth bool) {
payment.preauth = &preauth
}
func (payment *Payment) SetPreAuthExpireLogic(preauthExpireLogic int) {
payment.preauthExpireLogic = &preauthExpireLogic
}
func (payment *Payment) SetTestMode(test bool) {
payment.test = &test
}
func (payment *Payment) CreateSignature() string {
hashString := fmt.Sprintf("%s{up}%s{up}%d{up}%s{up}%s{up}%s", payment.paymentType, payment.account, payment.sum, payment.projectId, payment.desc, payment.secretKey)
hash := sha256.Sum256([]byte(hashString))
signature := hex.EncodeToString(hash[:])
return signature
}
func CreatePaymentURL(payment *Payment) string {
paymentUrl := &url.URL{
Scheme: "https",
Host: UnitpayAPIHost,
Path: "/api",
}
paymentUrlParams := paymentUrl.Query()
paymentUrlParams.Set("method", UnitpayMethodInitPayment)
paymentUrlParams.Set("params[paymentType]", string(payment.paymentType))
paymentUrlParams.Set("params[account]", payment.account)
paymentUrlParams.Set("params[sum]", strconv.Itoa(payment.sum))
paymentUrlParams.Set("params[projectId]", payment.projectId)
if payment.resultUrl != nil {
paymentUrlParams.Set("params[resultUrl]", *payment.resultUrl)
}
paymentUrlParams.Set("params[desc]", payment.desc)
paymentUrlParams.Set("params[secretKey]", payment.secretKey)
paymentUrlParams.Set("params[signature]", payment.CreateSignature())
if payment.test != nil {
paymentUrlParams.Set("params[test]", "1")
}
if payment.currency != nil {
paymentUrlParams.Set("params[currency]", *payment.currency)
}
if payment.locale != nil {
paymentUrlParams.Set("params[locale]", *payment.locale)
}
if payment.signature != nil {
paymentUrlParams.Set("params[signature]", *payment.signature)
}
if payment.ip != nil {
paymentUrlParams.Set("params[ip]", *payment.ip)
}
if payment.backUrl != nil {
paymentUrlParams.Set("params[backUrl]", *payment.backUrl)
}
if payment.subscription != nil {
paymentUrlParams.Set("params[subscription]", strconv.FormatBool(*payment.subscription))
}
if payment.subscriptionId != nil {
paymentUrlParams.Set("params[subscriptionId]", strconv.Itoa(*payment.subscriptionId))
}
if payment.preauth != nil {
paymentUrlParams.Set("params[preauth]", strconv.FormatBool(*payment.preauth))
}
if payment.preauthExpireLogic != nil {
paymentUrlParams.Set("params[preauthExpireLogic]", strconv.Itoa(*payment.preauthExpireLogic))
}
paymentUrl.RawQuery = paymentUrlParams.Encode()
return paymentUrl.String()
}
func CreatePaymentInfoURL(paymentId string, secretKey string) string {
paymentUrl := &url.URL{
Scheme: "https",
Host: UnitpayAPIHost,
Path: "/api",
}
paymentUrlParams := paymentUrl.Query()
paymentUrlParams.Set("method", UnitpayMethodGetPayment)
paymentUrlParams.Set("params[paymentId]", paymentId)
paymentUrlParams.Set("params[secretKey]", secretKey)
paymentUrl.RawQuery = paymentUrlParams.Encode()
return paymentUrl.String()
}