-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsogen.go
494 lines (470 loc) · 16.7 KB
/
sogen.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Copyright 2013 Mathias Monnerville. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sogenactif provides support for the online payment solution provided
// by la Société Générale.
//
// https://www.sogenactif.com/
//
// Load a config file with LoadConfig() then initialize the framework:
// conf, err := LoadConfig("conf/demo.cfg")
// s, err := NewSogen(conf)
//
// Given a <merchant_id>, a NewSogen() call will check that the merchant's certificate
// is available in ${merchants_rootdir}/<merchant_id> (see conf/demo.cfg) and will
// create (or overwrite) some files required by the Sogenactif plateform:
// certif.fr.<merchant_id>.php # Your certificate file
// parcom.<merchant_id> # Generated, defines locations for cancel and return urls
// parcom.sogenactif # Generated, defines some parameters for the platform
// pathfile # Generated, defines location of all files
//
// Now, using the API is a matter of serving content and calling Checkout() to initiate a
// payment, then calling HandlePayment() to get results back from the payment server.
//
// Initiate a payment with:
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// t := sogenactif.NewTransaction(&sogenactif.Customer{Id: "funkyab"}, 4.99)
// fmt.Fprintf(w, "<html><body>")
// sogen.Checkout(t, w) // Will add credit card logos and a link to the secure payment server
// fmt.Fprintf(w, "</body></html>")
// })
//
// Handle the return_url after a successful payment (provided the user click on the "return to site"
// button):
// http.HandleFunc(conf.ReturnUrl.Path, func(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "<html><body>")
// fmt.Fprintf(w, "<h2>Thank you!</h2>")
// p := sogen.HandlePayment(w, r)
// fmt.Fprintf(w, "</body></html>")
// })
//
// Also handle the cancel_url in case a payment is cancelled:
// http.HandleFunc(conf.CancelUrl.Path, func(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "<html><body>")
// fmt.Fprintf(w, "<h2>The transaction has been cancelled.</h2>")
// fmt.Fprintf(w, "</body></html>")
// })
//
// Finally, serve static content (to display credit card logos etc.) with:
// http.Handle(conf.LogoPath, http.StripPrefix(conf.LogoPath, http.FileServer(http.Dir(conf.MediaPath))))
// log.Fatal(http.ListenAndServe(":6060", nil))
package sogenactif
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"runtime"
"strconv"
"strings"
"time"
)
// Sogen holds information for the Sogenactif platform.
type Sogen struct {
config *Config // Config file
requestFile string // Path to request (proprietary) binary
responseFile string // Path to response (proprietary) binary
merchantBaseDir string // maps to merchant/marchant_id
certificatePrefix string // Merchant certificate prefix
parametersPrefix string // Merchant parameters file prefix
parametersSogenActif string // Merchant parameters file sogenactif
pathFile string // pathfile name
}
// Config holds attributes required by the platform.
type Config struct {
Debug bool
LogoPath string
LibraryPath string // Path to the provided closed-source binaries
MerchantsRootDir string // maps to merchant/
MediaPath string // Path to static files (credit cards logos etc.)
MerchantId string // Merchant Id
MerchantCountry string // Merchant country
MerchantCurrencyCode string // Merchant currency code
AutoResponseUrl *url.URL
CancelUrl *url.URL
ReturnUrl *url.URL
// Custom parameters used to generate parmcom.sogenactif. Default values are
// provided when calling LoadConfig(). Just override some of them before calling
// NewSogen().
Advert string // ADVERT!sg.gif!
BgColor string // BGCOLOR!ffffff!
BlockAlign string // BLOCK_ALIGN!center!
BlockOrder string // BLOCK_ORDER!1,2,3,4,5,6,7,8!
Condition string // CONDITION!SSL!
Currency int // CURRENCY!978!
HeaderFlag bool // HEADER_FLAG!yes!
Logo2 string // LOGO2!sogenactif.gif!
PaymentMeans string // PAYMENT_MEANS!CB,2,VISA,2,MASTERCARD,2,PAYLIB,2!
Target string // TARGET!_top!
TextColor string // TEXTCOLOR!000000!
}
// Customer defines some attributes that can be transmitted to the
// payment server.
type Customer struct {
// Unique customer ID. If defined, it will be passed to
// the sogen payment server and transmitted back after a
// successful or cancelled payment.
Id string
// Caddie is a free field which is sent back unmodified after a successful
// payment. It can contains up to 2048 chars.
Caddie string
// CancelUrl can be provided to override the cancel_url variable in the config
// file, on a per-customer basis.
CancelUrl *url.URL
// ReturnUrl can be provided to override the return_url variable in the config
// file, on a per-customer basis.
ReturnUrl *url.URL
// AutomaticUrl can be provided to override the auto_response_url variable in the config
// file, on a per-customer basis.
AutomaticUrl *url.URL
// Merchant custom data. Can be used to pass custom CSS url to the request
Data string
}
type Transaction struct {
customer *Customer
amount float64
}
// Payment holds data filled (and returned) by the secure payment server.
type Payment struct {
MerchantId string // Unique merchant's identifier (generally 0 followed by SIRET number)
MerchantCountry string // A 2-letter country code
Amount float64
TransactionId string
PaymentMeans string // Payment mean chosen by the customer
TransmissionDate time.Time
PaymentDate time.Time
ResponseCode string
PaymentCertificate string
AuthorizationId string
CurrencyCode string
CardNumber, CVVFlag, CVVResponseCode string
BankResponseCode string
ComplementaryCode, ComplementaryInfo string
ReturnContext string // Customer's buying context. Sent back unmodified.
Caddie string // Free field. Sent back unmodified.
ReceiptComplement string
MerchantLanguage, Language string
CustomerId string
CustomerEmail, CustomerIpAddress string
CaptureDay, CaptureMode string
Data string
OrderValidity string
ScoreValue, ScoreColor, ScoreInfo string
ScoreThreshold, ScoreProfile string
}
func (p *Payment) String() string {
return fmt.Sprintf(`========================================
Merchant ID: %s
Merchant Country: %s
Amount: %.2f
Transaction ID: %s
Payment Means: %s
----------------------------------------
Transmission Date: %s
Payment Date: %s
Response Code: %s
Payment Certificate: %s
----------------------------------------
Authorization ID: %s
Currency Code: %s
Card Number: %s
CVV Flag: %s
CVV Response Code: %s
Bank Response Code: %s
Complementary Code: %s
Complementary Info: %s
----------------------------------------
Return Context: %s
Caddie: %s
Receipt Complement: %s
Merchant Language: %s
Language: %s
----------------------------------------
Customer ID: %s
Customer Email: %s
Customer IP Address: %s
----------------------------------------
Capture Day: %s
Capture Mode: %s
Data: %s
Order Validity: %s
----------------------------------------
Score Value: %s
Score Color: %s
Score Info: %s
Score Threshold: %s
Score Profile: %s`,
p.MerchantId, p.MerchantCountry, p.Amount, p.TransactionId, p.PaymentMeans, p.TransmissionDate,
p.PaymentDate, p.PaymentCertificate, p.ResponseCode, p.AuthorizationId, p.CurrencyCode, p.CardNumber,
p.CVVFlag, p.CVVResponseCode, p.BankResponseCode, p.ComplementaryCode, p.ComplementaryInfo,
p.ReturnContext, p.Caddie, p.ReceiptComplement, p.MerchantLanguage, p.Language, p.CustomerId,
p.CustomerEmail, p.CustomerIpAddress, p.CaptureDay, p.CaptureMode, p.Data, p.OrderValidity,
p.ScoreValue, p.ScoreColor, p.ScoreInfo, p.ScoreThreshold, p.ScoreProfile)
}
// requestParams defines some request parameters in the Checkout() process.
func (s *Sogen) requestParams(t *Transaction) []string {
params := map[string]string{
"merchant_id": s.config.MerchantId,
"merchant_country": s.config.MerchantCountry,
"amount": strconv.Itoa(int(t.amount * 100)),
"currency_code": s.config.MerchantCurrencyCode,
"pathfile": s.pathFile,
"caddie": t.customer.Caddie,
}
if t.customer.Id != "" {
params["customer_id"] = t.customer.Id
}
if t.customer.CancelUrl != nil {
params["cancel_return_url"] = t.customer.CancelUrl.String()
}
if t.customer.ReturnUrl != nil {
params["normal_return_url"] = t.customer.ReturnUrl.String()
}
if t.customer.AutomaticUrl != nil {
params["automatic_response_url"] = t.customer.AutomaticUrl.String()
}
if t.customer.Data != "" {
params["data"] = t.customer.Data
}
plist := make([]string, 0)
for k, v := range params {
plist = append(plist, fmt.Sprintf("%s=%s", k, v))
}
return plist
}
// NewTransaction creates a new transaction for a customer that can be
// used to checkout. A nil customer or a null amount returns a nil
// transaction.
func NewTransaction(c *Customer, amount float64) *Transaction {
if c == nil || amount == 0 {
return nil
}
return &Transaction{customer: c, amount: amount}
}
// NewSogen sets up all the files required by the Sogen API for
// a giver merchant.
func NewSogen(c *Config) (*Sogen, error) {
if c == nil {
return nil, errors.New("can't initialize Sogen framework: nil config.")
}
c.MerchantId = strings.Trim(c.MerchantId, " ")
if c.MerchantId == "" {
return nil, errors.New("missing merchant ID")
}
c.MerchantsRootDir = strings.Trim(c.MerchantsRootDir, " ")
if c.MerchantsRootDir == "" {
return nil, errors.New("missing merchant root directory (for config files and certificates)")
}
if _, err := os.Stat(c.LibraryPath); err != nil {
return nil, errors.New("bad library_path: " + err.Error())
}
log.Printf("Initializing the Sogenactif payment system (%s)", c.MerchantId)
s := new(Sogen)
s.config = c
s.merchantBaseDir = path.Join(c.MerchantsRootDir, c.MerchantId)
s.certificatePrefix = path.Join(s.merchantBaseDir, "certif")
s.parametersPrefix = path.Join(s.merchantBaseDir, "parcom")
s.parametersSogenActif = path.Join(s.merchantBaseDir, "parcom.sogenactif")
s.pathFile = path.Join(s.merchantBaseDir, "pathfile")
s.requestFile = path.Join(c.LibraryPath, runtime.GOOS+"_"+runtime.GOARCH, "request")
s.responseFile = path.Join(c.LibraryPath, runtime.GOOS+"_"+runtime.GOARCH, "response")
if _, err := os.Stat(s.requestFile); err != nil {
return nil, errors.New("request binary: " + err.Error())
}
if _, err := os.Stat(s.responseFile); err != nil {
return nil, errors.New("request binary: " + err.Error())
}
if _, err := os.Stat(s.merchantBaseDir); err != nil {
return nil, errors.New(fmt.Sprintf("missing certificate file in directory %s", s.merchantBaseDir))
}
certFile := fmt.Sprintf("%s.%s.%s.php", s.certificatePrefix, c.MerchantCountry, c.MerchantId)
if _, err := os.Stat(certFile); err != nil {
return nil, errors.New(fmt.Sprintf("missing certificate file %s", certFile))
}
log.Printf("Found certificate file %s", certFile)
// Write pathfile
f, err := os.Create(s.pathFile)
defer f.Close()
if err != nil {
return nil, err
}
debug := "NO"
if s.config.Debug {
debug = "YES"
}
_, err = f.Write([]byte(fmt.Sprintf(`DEBUG!%s!
D_LOGO!%s!
F_CERTIFICATE!%s!
F_CTYPE!php!
F_PARAM!%s!
F_DEFAULT!%s!
`, debug, s.config.LogoPath, s.certificatePrefix, s.parametersPrefix, s.parametersSogenActif)))
if err != nil {
return nil, err
}
log.Printf("Created file %s", s.pathFile)
// Write parmcom.merchant_id
parmcom := fmt.Sprintf("%s.%s", s.parametersPrefix, c.MerchantId)
f, err = os.Create(parmcom)
defer f.Close()
if err != nil {
return nil, err
}
_, err = f.Write([]byte(fmt.Sprintf(`LOGO!/bf/chrome/common/logo.png!
CANCEL_URL!%s!
RETURN_URL!%s!
`, s.config.CancelUrl, s.config.ReturnUrl)))
// auto_response_url config parameter is optional
if s.config.AutoResponseUrl != nil {
_, err = f.Write([]byte(fmt.Sprintf("AUTO_RESPONSE_URL!%s!\n", s.config.AutoResponseUrl)))
}
if err != nil {
return nil, err
}
log.Printf("Created file %s", parmcom)
// Write parmcom.sogenactif
f, err = os.Create(s.parametersSogenActif)
defer f.Close()
if err != nil {
return nil, err
}
mpars := map[string]string{
"ADVERT": s.config.Advert,
"BGCOLOR": s.config.BgColor,
"BLOCK_ALIGN": s.config.BlockAlign,
"BLOCK_ORDER": s.config.BlockOrder,
"CONDITION": s.config.Condition,
"CURRENCY": strconv.FormatInt(int64(s.config.Currency), 10),
"LOGO2": s.config.Logo2,
"PAYMENT_MEANS": s.config.PaymentMeans,
"TARGET": s.config.Target,
"TEXTCOLOR": s.config.TextColor,
"LANGUAGE": s.config.MerchantCountry,
"MERCHANT_COUNTRY": s.config.MerchantCountry,
"MERCHANT_LANGUAGE": s.config.MerchantCountry,
}
if s.config.HeaderFlag {
mpars["HEADER_FLAG"] = "yes"
} else {
mpars["HEADER_FLAG"] = "no"
}
for k, v := range mpars {
_, err = f.Write([]byte(fmt.Sprintf("%s!%s!\n", k, v)))
if err != nil {
return nil, err
}
}
log.Printf("Created file %s", s.parametersSogenActif)
return s, nil
}
// Checkout generates an HTML form suitable to redirect the buyer
// to the payment server.
func (s *Sogen) Checkout(t *Transaction, w io.Writer) error {
// Execute binary
cmd := exec.Command(s.requestFile, s.requestParams(t)...)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return err
}
res := strings.Split(out.String(), "!")
code, sogerr, body := res[1], res[2], res[3]
if code == "" && sogerr == "" {
return errors.New("error: request executable not found!")
} else if code != "0" {
return errors.New(fmt.Sprintf("error using API (error code %s)", sogerr))
}
// No error; sogerr may hold debug info if DEBUG is set to YES
fmt.Fprintf(w, sogerr)
fmt.Fprintf(w, body)
return nil
}
func formatToRFC3339(dt, offset string) (time.Time, error) {
format := "%s-%s-%sT%s:%s:%s" + offset
rfc, err := time.Parse(time.RFC3339, fmt.Sprintf(format, dt[:4], dt[4:6], dt[6:8], dt[8:10],
dt[10:12], dt[12:14]))
return rfc, err
}
// HandlePayment generates a payment from the Sogen's server
// response.
func (s *Sogen) HandlePayment(w io.Writer, r *http.Request) (*Payment, error) {
if r == nil {
return nil, errors.New("can't handle payment for nil request")
}
data := r.PostFormValue("DATA")
if len(data) == 0 {
return nil, errors.New("missing sogen data in request")
}
cmd := exec.Command(s.responseFile, "pathfile="+s.pathFile, "message="+data)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return nil, err
}
res := strings.Split(out.String(), "!")
code, sogerr := res[1], res[2]
if code == "" && sogerr == "" {
return nil, errors.New("request executable not found!")
} else if code != "0" {
return nil, errors.New(fmt.Sprintf("error using API (error code %s)", sogerr))
}
// sogerr holds debug info if DEBUG is set to YES
fmt.Fprintf(w, sogerr)
v := res[3:]
amount, err := strconv.ParseFloat(v[2], 32)
if err != nil {
return nil, errors.New("amount conversion error: " + err.Error())
}
amount /= 100
tDate, err := formatToRFC3339(v[5], "+01:00")
if err != nil {
return nil, errors.New("transmission date conversion error: " + err.Error())
}
pDateTime, err := formatToRFC3339(v[7]+v[6], "+01:00")
if err != nil {
return nil, errors.New("payment datetime conversion error: " + err.Error())
}
p := Payment{
MerchantId: v[0],
MerchantCountry: v[1],
Amount: amount,
TransactionId: v[3],
PaymentMeans: v[4],
TransmissionDate: tDate,
PaymentDate: pDateTime,
PaymentCertificate: v[8],
ResponseCode: v[9],
AuthorizationId: v[10],
CurrencyCode: v[11],
CardNumber: v[12],
CVVFlag: v[13],
CVVResponseCode: v[14],
BankResponseCode: v[15],
ComplementaryCode: v[16],
ComplementaryInfo: v[17],
ReturnContext: v[18],
Caddie: v[19],
ReceiptComplement: v[20],
MerchantLanguage: v[21],
Language: v[22],
CustomerId: v[23],
CustomerEmail: v[24],
CustomerIpAddress: v[25],
CaptureDay: v[26],
CaptureMode: v[27],
Data: v[28],
OrderValidity: v[29],
ScoreValue: v[30],
ScoreColor: v[31],
ScoreInfo: v[32],
ScoreThreshold: v[33],
ScoreProfile: v[34],
}
return &p, nil
}