-
Notifications
You must be signed in to change notification settings - Fork 1
/
signature.go
371 lines (301 loc) · 11 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package redsys
import (
"context"
"crypto/cipher"
"crypto/des"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"regexp"
"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. Assign to Ds_Signature.
Signature string
// Version of the signature. Assign to Ds_SignatureVersion.
SignatureVersion string
// Params to send. Assign to Ds_MerchantParameters.
Params string
// Output only. It will return the endpoint of the call where the info should be sent.
Endpoint string
}
// Merchant data provided by the bank itself during the integration.
type Merchant struct {
// Merchant code assigned by the bank.
Code string
// Merchant name to show in the receipt. You can freely use any appropiate name.
Name string
// Terminal number assigned by the bank.
Terminal int64
// Secret to sign transactions assigned by the bank.
Secret string
// URL where the asynchronous background notification will be sent.
URLNotification string
// Send the data to the test endpoint of the bank.
Debug bool
}
// Session data that changes for each payment the merchant wants to make.
type Session struct {
// Code of the session. It should have 4 digits and 8 characters. It should be unique for each retry of the payment.
Order string
// Language code. Use English if unknown.
Lang Lang
// Name of the client to show in the receipt. Use any appropiate info available.
Client string
// Amount in cents to pay.
Amount int32
// Product name to show in the receipt.
Product string
// URL to return the user to when the transaction is approved.
URLOK string
// URL to return the user to when the transaction is cancelled.
URLKO string
// Raw custom data that will be sent back in the confirmation when the transaction finishes. You can use it to store
// identifiers or any other data that facilitates the verification afterwards.
Data string
// Payment method to use. By default it will be credit card if empty.
PaymentMethod PaymentMethod
}
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 PaymentMethod string
const (
PaymentMethodCreditCard = PaymentMethod("C")
PaymentMethodBizum = PaymentMethod("z")
PaymentMethodPaypal = PaymentMethod("P")
)
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"`
PaymentMethod PaymentMethod `json:"Ds_Merchant_PayMethods,omitempty"`
}
var reOrder = regexp.MustCompile(`^[0-9]{4}[0-9A-Za-z]{8}$`)
// Sign takes all the input data and returns the parameters to be sent to the bank.
func Sign(ctx context.Context, merchant Merchant, session Session) (Signed, error) {
if !reOrder.MatchString(session.Order) {
return Signed{}, fmt.Errorf("invalid order format %q", session.Order)
}
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,
PaymentMethod: session.PaymentMethod,
}
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
}
// Parsed parameters of the transaction.
type Params struct {
// Response code of the bank.
Response int64 `json:"-"`
// Order code of the transaction.
Order string `json:"Ds_Order"`
// Original order code as a string that sometimes comes back empty.
RawResponse string `json:"Ds_Response"`
// Date of the transaction in the format "02/01/2006".
Date string `json:"Ds_Date"`
// Hour of the transaction in the format "15:04".
Time string `json:"Ds_Hour"`
// Country code of the card.
Country string `json:"Ds_Card_Country"`
// Authorization code of the card. Should be stored to have a reference to the transaction.
AuthCode string `json:"Ds_AuthorisationCode"`
// Card type: MasterCard, Visa, etc.
CardType string `json:"Ds_Card_Type"`
// Custom data previously sent that comes back in the confirmation.
Data string `json:"Ds_MerchantData"`
}
// ParseParams reads the response from the bank and returns the parsed parameters if the signature is valid. If an error
// is returned the input data is compromised and should not be used, the returned params will also be empty.
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
}
// Status of a finished transaction.
type Status string
const (
// StatusUnknown means the transaction has not been correctly signed.
StatusUnknown = Status("")
// StatusApproved means the transaction was approved by the bank and the money was transferred.
StatusApproved = Status("approved")
// StatusCancelled means the transaction was cancelled by the user.
StatusCancelled = Status("cancelled")
// StatusRepeated means the transaction has been sent repeatedly to the bank. It is a programming error that should
// not happen if a different Order code is used for each retry.
StatusRepeated = Status("repeated")
)
// Operation represents the result of a payment operation.
type Operation struct {
// Status of the operation.
Status Status
// Sent date of the operation.
Sent time.Time
// All the parsed parameters of the transaction.
Params Params
// True if the transaction was a credit card payment.
IsCreditCard bool
// Raw response code of the bank.
ResponseCode int64
}
// Confirm reads the response from the bank and parses the response to determine the status of the transaction in a more
// easy to use way. If an error is returned the input data is compromised and should not be used, the returned operation
// will also be empty.
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
104, // Transaction not permitted with this type of card.
129, // Wrong CVV
180, // Alien card service
190, // Denied without any specific reason.
184, // Error with the owner authentication
191, // Wrong expiration date
9142, // Excess time for payment
}
switch {
// 9915 cancelled by the user.
// 9673 Bizum cancellation.
case params.Response == 9915 || params.Response == 9673:
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
}