-
Notifications
You must be signed in to change notification settings - Fork 5
/
payment_method.go
103 lines (85 loc) · 2.4 KB
/
payment_method.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
package payment_method
import (
"database/sql/driver"
"errors"
"fmt"
"time"
"github.com/fritzpay/paymentd/pkg/paymentd/provider"
)
type methodStatus string
// returns a valid paymentMethodStatus or error
func ParseMethodStatus(s string) (methodStatus, error) {
if s == PaymentMethodStatusActive.String() {
return PaymentMethodStatusActive, nil
} else if s == PaymentMethodStatusInactive.String() {
return PaymentMethodStatusInactive, nil
} else if s == PaymentMethodStatusDisabled.String() {
return PaymentMethodStatusDisabled, nil
} else {
return methodStatus(""), errors.New("invalid")
}
}
func (s methodStatus) String() string {
if s == "" {
return "invalid"
}
return string(s)
}
// Scan implements the (database/sql).Scanner
func (s *methodStatus) Scan(src interface{}) error {
switch v := src.(type) {
case []byte:
*s = methodStatus(string(v))
return nil
case string:
*s = methodStatus(v)
return nil
default:
return fmt.Errorf("error scanning into PaymentMethodStatus type. got invalid type %T", src)
}
}
// Value implements the (database/sql/driver).Valuer so it can be used in SQL statements
// as a value
func (s methodStatus) Value() (driver.Value, error) {
return string(s), nil
}
const (
PaymentMethodStatusActive methodStatus = "active"
PaymentMethodStatusInactive methodStatus = "inactive"
PaymentMethodStatusDisabled methodStatus = "disabled"
)
// PaymentMethod represents a mode (method of payment)
//
// It is associated with a Provider and can be configured on a per-project base.
type Method struct {
ID int64 `json:",string"`
ProjectID int64 `json:",string"`
Provider provider.Provider
MethodKey string
Created time.Time
CreatedBy string
Status methodStatus
StatusChanged time.Time
StatusCreatedBy string
Metadata map[string]string
}
// Active returns true if the payment method is considered active
func (m *Method) Active() bool {
return m.Status == PaymentMethodStatusActive
}
// Active returns true if the payment method is considered disabled
func (m *Method) Disabled() bool {
return m.Status == PaymentMethodStatusDisabled
}
const (
metadataTable = "payment_method_metadata"
metadataPrimaryField = "payment_method_id"
)
const MetadataModel metadataModel = 0
type metadataModel int
func (m metadataModel) Table() string {
return metadataTable
}
func (m metadataModel) PrimaryField() string {
return metadataPrimaryField
}