-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
option.go
82 lines (73 loc) · 1.85 KB
/
option.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
package payment
import (
"net/http"
"strconv"
)
// Option is type closure accepting a Options
type Option func(*Options)
// CreditCard tells information about the acquire bank and
// installment used
type CreditCard struct {
Bank Bank
Installment Installment
}
// Installment tells installent type and term
type Installment struct {
Type InstallmentType
Term int
}
// Options stores all optional properties for payment purposes
type Options struct {
Price *Money
CreditCard *CreditCard
}
// WithPrice can be used if user want to add optional price information
// used for estimating the admin/installment fee
func WithPrice(price float64, currency string) Option {
return func(o *Options) {
o.Price = &Money{
Value: price,
Currency: currency,
}
}
}
// WithCreditCard can be used if user want to use the installment feature. It accepts the acquire bank,
// installment type and term
func WithCreditCard(bank Bank, installmentType InstallmentType, installmentTerm int) Option {
if bank == "" {
bank = BankBCA
}
if installmentType == "" {
installmentType = InstallmentOffline
}
return func(o *Options) {
o.CreditCard = &CreditCard{
Bank: bank,
Installment: Installment{
Type: installmentType,
Term: installmentTerm,
},
}
}
}
// NewPaymentMethodListOptions accepts http.Request and returns set of option containing the price and its currency.
func NewPaymentMethodListOptions(r *http.Request) ([]Option, error) {
r.ParseForm()
var options []Option
var price float64
var currency string
var err error
if len(r.Form["price"]) > 0 {
price, err = strconv.ParseFloat(r.Form["price"][0], 64)
if err != nil {
return nil, err
}
}
if len(r.Form["currency"]) > 0 {
currency = r.Form["currency"][0]
}
if price > 0 && currency != "" {
options = append(options, WithPrice(price, currency))
}
return options, nil
}