-
Notifications
You must be signed in to change notification settings - Fork 17
/
payment_method.go
95 lines (81 loc) · 3.63 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
package easypost
type PaymentMethodPriority int64
const (
PrimaryPaymentMethodPriority PaymentMethodPriority = iota
SecondaryPaymentMethodPriority
)
type PaymentMethodType int64
const (
CreditCardPaymentType PaymentMethodType = iota
BankAccountPaymentType
)
type PaymentMethod struct {
ID string `json:"id,omitempty" url:"id,omitempty"`
Object string `json:"object,omitempty" url:"object,omitempty"`
PrimaryPaymentMethod *PaymentMethodObject `json:"primary_payment_method,omitempty" url:"primary_payment_method,omitempty"`
SecondaryPaymentMethod *PaymentMethodObject `json:"secondary_payment_method,omitempty" url:"secondary_payment_method,omitempty"`
}
type PaymentMethodObject struct {
BankName string `json:"bank_name,omitempty" url:"bank_name,omitempty"` // bank account
Brand string `json:"brand,omitempty" url:"brand,omitempty"` // credit card
Country string `json:"country,omitempty" url:"country,omitempty"` // bank account
DisabledAt string `json:"disabled_at,omitempty" url:"disabled_at,omitempty"` // both
ExpirationMonth int `json:"exp_month,omitempty" url:"exp_month,omitempty"` // credit card
ExpirationYear int `json:"exp_year,omitempty" url:"exp_year,omitempty"` // credit card
ID string `json:"id,omitempty" url:"id,omitempty"` // both
Last4 string `json:"last4,omitempty" url:"last4,omitempty"` // both
Name string `json:"name,omitempty" url:"name,omitempty"` // credit card
Object string `json:"object,omitempty" url:"object,omitempty"` // both
Verified bool `json:"verified,omitempty" url:"verified,omitempty"` // bank account
}
// getPaymentMethodObjectType returns the PaymentMethodType enum of a PaymentMethodObject.
func (c *Client) getPaymentMethodObjectType(object *PaymentMethodObject) (out PaymentMethodType, err error) {
if object.Object == "CreditCard" {
out = CreditCardPaymentType
} else if object.Object == "BankAccount" {
out = BankAccountPaymentType
} else {
return out, newInvalidObjectError(NoMatchingPaymentMethod)
}
return
}
// getPaymentMethodEndpoint returns the associated endpoint for a PaymentMethodObject based on its type.
func (c *Client) getPaymentMethodEndpoint(object *PaymentMethodObject) (out string, err error) {
paymentMethodType, _ := c.getPaymentMethodObjectType(object)
switch paymentMethodType {
case CreditCardPaymentType:
out = "credit_cards"
case BankAccountPaymentType:
out = "bank_accounts"
default:
return out, newInvalidObjectError(NoMatchingPaymentMethod)
}
return
}
// getPaymentMethodByPriority returns the PaymentMethodObject associated with the given PaymentMethodPriority.
func (c *Client) getPaymentMethodByPriority(priority PaymentMethodPriority) (out *PaymentMethodObject, err error) {
paymentMethods, err := c.RetrievePaymentMethods()
if err != nil {
return out, err
}
switch priority {
case PrimaryPaymentMethodPriority:
out = paymentMethods.PrimaryPaymentMethod
case SecondaryPaymentMethodPriority:
out = paymentMethods.SecondaryPaymentMethod
}
if out == nil {
return out, newInvalidObjectError(PaymentMethodNotSetUp)
}
return out, nil
}
// GetPaymentEndpointByPrimaryOrSecondary gets the payment priority based on primaryOrSecondary parameter.
func (c *Client) GetPaymentEndpointByPrimaryOrSecondary(primaryOrSecondary PaymentMethodPriority) (out string) {
switch primaryOrSecondary {
case PrimaryPaymentMethodPriority:
out = "primary"
case SecondaryPaymentMethodPriority:
out = "secondary"
}
return
}