-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
83 lines (66 loc) · 1.76 KB
/
options.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
package pg
import (
"reflect"
)
var (
True bool = true
False bool = false
)
// Options is the wrap of the params needed for API Call
type Options struct {
// Environment used for create transaction
// Possible values are 'Production' or 'SandBox'
//
// Default: SandBox
Environment EnvironmentType
// ClientId is client_id from payment gateway
// it can be apiKey or something like that
ClientId string
// ServerKey is client_secret from payment gateway
ServerKey string
// ApiCall interface to request outside
//
// Default: DefaultApiRequest
ApiCall ApiRequestInterface
// When set to true, this will log your request, response or error to stdout
// Use logrus as logging
//
// Default: true
Logging *bool
}
// DefaultOptions define all default value of configuration options payment gateway
var DefaultOptions = &Options{
Environment: SandBox,
}
// NewOption will create an instance of configuration Options
func NewOption(opts ...*Options) (*Options, error) {
opt := DefaultOptions
if len(opts) > 0 {
opt = opts[0]
}
// init standard logging
NewLogger()
// if environment not exists
if reflect.ValueOf(opt.Environment).IsZero() {
opt.Environment = SandBox
}
if opt.Logging == nil {
opt.Logging = &True
}
// if logging is false or logging function is not exists
// disable logging for handle an error
if !*opt.Logging {
DisableLogging()
}
// make sure clientId or clientSecret exists
// if not exists, just given error ErrMissingCredentials
if reflect.ValueOf(opt.ClientId).IsZero() || reflect.ValueOf(opt.ServerKey).IsZero() {
return nil, ErrMissingCredentials
}
// make default apiRequestInterface if not exists
if opt.ApiCall == nil {
opt.ApiCall = DefaultApiRequest()
}
// return an instance of Options
return opt, nil
}