-
Notifications
You must be signed in to change notification settings - Fork 1
/
signature.go
306 lines (254 loc) · 7.99 KB
/
signature.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package redsys
import (
"context"
"crypto/cipher"
"crypto/des"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"slices"
"strconv"
"time"
)
const (
EndpointProduction = "https://sis.redsys.es/sis/realizarPago"
EndpointDebug = "https://sis-t.redsys.es:25443/sis/realizarPago"
)
// Signed TPV transaction to send to the bank.
type Signed struct {
// Signature of the parameters.
Signature string
// Version of the signature.
SignatureVersion string
// Params to send.
Params string
// Output only. It will return the endpoint of the call.
Endpoint string
}
// Merchant data
type Merchant struct {
// Merchant code assigned by the bank.
Code string
// Merchant name to show in the receipt.
Name string
// Terminal number assigned by the bank.
Terminal int64
// Secret to sign transactions assigned by the bank.
Secret string
// URL where the notification will be sent.
URLNotification string
// Enable debug endpoint.
Debug bool
}
// Session data
type Session struct {
// Code of the session. It should have 4 digits and 8 characters.
Order string
// Two-letters code of the language. Use English if unknown, please.
Lang Lang
// Name of the client to show in the receipt.
Client string
// Amount in cents to pay.
Amount int32
// Product name to show in the receipt.
Product string
// URL to return to when the transaction is approved.
URLOK string
// URL to return to when the transaction is cancelled.
URLKO string
// Raw data that will be sent back in the confirmation.
Data string
}
type TransactionType int64
const (
TransactionTypeSimpleAuthorization = TransactionType(0)
)
type Currency int64
const (
CurrencyEuros = Currency(978)
)
type Lang string
const (
LangES = Lang("001")
LangEN = Lang("002")
LangCA = Lang("003")
LangFR = Lang("004")
LangDE = Lang("005")
LangIT = Lang("007")
LangPT = Lang("009")
)
type tpvRequest struct {
MerchantCode string `json:"Ds_Merchant_MerchantCode"`
Terminal int64 `json:"Ds_Merchant_Terminal"`
TransactionType TransactionType `json:"Ds_Merchant_TransactionType"`
Amount int32 `json:"Ds_Merchant_Amount"`
Currency Currency `json:"Ds_Merchant_Currency"`
Order string `json:"Ds_Merchant_Order"`
MerchantURL string `json:"Ds_Merchant_MerchantURL"`
Product string `json:"Ds_Merchant_ProductDescription"`
Client string `json:"Ds_Merchant_Titular"`
Lang Lang `json:"Ds_Merchant_ConsumerLanguage"`
URLOK string `json:"Ds_Merchant_UrlOK"`
URLKO string `json:"Ds_Merchant_UrlKO"`
MerchantName string `json:"Ds_Merchant_MerchantName"`
Data string `json:"Ds_Merchant_MerchantData,omitempty"`
}
func Sign(ctx context.Context, merchant Merchant, session Session) (Signed, error) {
if len(session.Client) > 59 {
session.Client = session.Client[:59]
}
params := tpvRequest{
MerchantCode: merchant.Code,
Terminal: merchant.Terminal,
TransactionType: TransactionTypeSimpleAuthorization,
Amount: session.Amount,
Currency: CurrencyEuros,
Order: session.Order,
MerchantURL: merchant.URLNotification,
Product: session.Product,
Client: session.Client,
Lang: session.Lang,
URLOK: session.URLOK,
URLKO: session.URLKO,
MerchantName: merchant.Name,
Data: session.Data,
}
paramsJSON, err := json.Marshal(params)
if err != nil {
return Signed{}, fmt.Errorf("cannot marshal params: %v", err)
}
paramsStr := base64.StdEncoding.EncodeToString(paramsJSON)
signature, err := sign(merchant.Secret, params.Order, paramsStr)
if err != nil {
return Signed{}, fmt.Errorf("%v", err)
}
signed := Signed{
Signature: base64.StdEncoding.EncodeToString(signature),
SignatureVersion: "HMAC_SHA256_V1",
Params: paramsStr,
Endpoint: EndpointProduction,
}
if merchant.Debug {
signed.Endpoint = EndpointDebug
}
return signed, nil
}
type Status string
const (
StatusUnknown = Status("")
StatusApproved = Status("approved")
StatusCancelled = Status("cancelled")
StatusRepeated = Status("repeated")
)
type Operation struct {
Status Status
Sent time.Time
Params Params
IsCreditCard bool
// Raw response code of the bank.
ResponseCode int64
}
type Params struct {
Response int64 `json:"-"`
Order string `json:"Ds_Order"`
Date string `json:"Ds_Date"`
Time string `json:"Ds_Hour"`
Country string `json:"Ds_Card_Country"`
AuthCode string `json:"Ds_AuthorisationCode"`
CardType string `json:"Ds_Card_Type"`
Data string `json:"Ds_MerchantData"`
RawResponse string `json:"Ds_Response"`
}
func ParseParams(signed Signed) (Params, error) {
if signed.SignatureVersion != "HMAC_SHA256_V1" {
return Params{}, fmt.Errorf("unknown signature version: %s", signed.SignatureVersion)
}
decoded, err := base64.StdEncoding.DecodeString(signed.Params)
if err != nil {
return Params{}, fmt.Errorf("cannot decode params: %v", err)
}
params := Params{}
if err = json.Unmarshal(decoded, ¶ms); err != nil {
return Params{}, fmt.Errorf("cannot unmarshal params: %v", err)
}
if params.RawResponse != "" {
params.Response, err = strconv.ParseInt(params.RawResponse, 10, 64)
if err != nil {
return Params{}, fmt.Errorf("cannot parse response %q: %v", params.RawResponse, err)
}
}
params.Data, err = url.QueryUnescape(params.Data)
if err != nil {
return Params{}, fmt.Errorf("cannot unescape data %q: %v", params.Data, err)
}
return params, nil
}
func Confirm(ctx context.Context, secret string, signed Signed) (Operation, error) {
params, err := ParseParams(signed)
if err != nil {
return Operation{}, fmt.Errorf("cannot parse params: %v", err)
}
signature, err := sign(secret, params.Order, signed.Params)
if err != nil {
return Operation{}, fmt.Errorf("%v", err)
}
decodedSignature, err := base64.URLEncoding.DecodeString(signed.Signature)
if err != nil {
return Operation{}, fmt.Errorf("cannot decode signature: %v", err)
}
if !hmac.Equal(signature, decodedSignature) {
return Operation{}, fmt.Errorf("bad signature, got %q expected %q", signed.Signature, base64.URLEncoding.EncodeToString(signature))
}
operation := Operation{
Params: params,
ResponseCode: params.Response,
}
dt, err := url.QueryUnescape(fmt.Sprintf("%s %s", params.Date, params.Time))
if err != nil {
return Operation{}, fmt.Errorf("cannot unescape datetime %q %q: %v", params.Date, params.Time, err)
}
operation.Sent, err = time.Parse("02/01/2006 15:04", dt)
if err != nil {
return Operation{}, fmt.Errorf("failed to parse datetime %q: %v", dt, err)
}
badData := []int64{
101, // Expired card
129, // Wrong CVV
180, // Alien card service
184, // Error with the owner authentication
191, // Wrong expiration date
9142, // Excess time for payment
}
switch {
case params.Response == 9915:
operation.Status = StatusCancelled
case params.Response == 913:
operation.Status = StatusRepeated
case slices.Contains(badData, params.Response):
operation.Status = StatusCancelled
case params.Response >= 0 && params.Response <= 99:
operation.Status = StatusApproved
operation.IsCreditCard = (params.CardType == "C")
}
return operation, nil
}
func sign(secret, order, content string) ([]byte, error) {
decodedSecret, err := base64.StdEncoding.DecodeString(secret)
if err != nil {
return nil, fmt.Errorf("cannot decode secret: %v", err)
}
block, err := des.NewTripleDESCipher(decodedSecret)
if err != nil {
return nil, fmt.Errorf("failed to initialize cipher: %v", err)
}
// Zeros IV obtained from the official implementation in PHP.
mode := cipher.NewCBCEncrypter(block, []byte("\x00\x00\x00\x00\x00\x00\x00\x00"))
key := make([]byte, 16)
mode.CryptBlocks(key, []byte(fmt.Sprintf("%s\x00\x00\x00\x00", order)))
mac := hmac.New(sha256.New, key)
_, _ = mac.Write([]byte(content))
return mac.Sum(nil), nil
}