-
Notifications
You must be signed in to change notification settings - Fork 82
/
account_payment_methods.go
165 lines (132 loc) · 5.17 KB
/
account_payment_methods.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
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/linode/linodego/internal/parseabletime"
)
// PaymentMethod represents a PaymentMethod object
type PaymentMethod struct {
// The unique ID of the Payment Method.
ID int `json:"id"`
// When the Payment Method was created.
Created *time.Time `json:"created"`
// Whether this Payment Method is the default method for automatically processing service charges.
IsDefault bool `json:"is_default"`
// The type of Payment Method.
Type string `json:"type"`
// The detailed data for the Payment Method, which can be of varying types.
Data interface{} `json:"data"`
}
// PaymentMethodDataCreditCard represents a PaymentMethodDataCreditCard object
type PaymentMethodDataCreditCard struct {
// The type of credit card.
CardType string `json:"card_type"`
// The expiration month and year of the credit card.
Expiry string `json:"expiry"`
// The last four digits of the credit card number.
LastFour string `json:"last_four"`
}
// PaymentMethodDataGooglePay represents a PaymentMethodDataGooglePay object
type PaymentMethodDataGooglePay struct {
// The type of credit card.
CardType string `json:"card_type"`
// The expiration month and year of the credit card.
Expiry string `json:"expiry"`
// The last four digits of the credit card number.
LastFour string `json:"last_four"`
}
// PaymentMethodDataPaypal represents a PaymentMethodDataPaypal object
type PaymentMethodDataPaypal struct {
// The email address associated with your PayPal account.
Email string `json:"email"`
// PayPal Merchant ID associated with your PayPal account.
PaypalID string `json:"paypal_id"`
}
// PaymentMethodCreateOptions fields are those accepted by CreatePaymentMethod
type PaymentMethodCreateOptions struct {
// Whether this Payment Method is the default method for automatically processing service charges.
IsDefault bool `json:"is_default"`
// The type of Payment Method. Alternative payment methods including Google Pay and PayPal can be added using the Cloud Manager.
Type string `json:"type"`
// An object representing the credit card information you have on file with Linode to make Payments against your Account.
Data *PaymentMethodCreateOptionsData `json:"data"`
}
type PaymentMethodCreateOptionsData struct {
// Your credit card number. No spaces or hyphens (-) allowed.
CardNumber string `json:"card_number"`
// CVV (Card Verification Value) of the credit card, typically found on the back of the card.
CVV string `json:"cvv"`
// A value from 1-12 representing the expiration month of your credit card.
ExpiryMonth int `json:"expiry_month"`
// A four-digit integer representing the expiration year of your credit card.
ExpiryYear int `json:"expiry_year"`
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (i *PaymentMethod) UnmarshalJSON(b []byte) error {
if len(b) == 0 || string(b) == "{}" || string(b) == "null" {
return nil
}
type Mask PaymentMethod
pm := &struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Data json.RawMessage `json:"data"`
}{
Mask: (*Mask)(i),
}
if err := json.Unmarshal(b, &pm); err != nil {
return err
}
// Process Data based on the Type field
switch i.Type {
case "credit_card":
var creditCardData PaymentMethodDataCreditCard
if err := json.Unmarshal(pm.Data, &creditCardData); err != nil {
return err
}
i.Data = creditCardData
case "google_pay":
var googlePayData PaymentMethodDataGooglePay
if err := json.Unmarshal(pm.Data, &googlePayData); err != nil {
return err
}
i.Data = googlePayData
case "paypal":
var paypalData PaymentMethodDataPaypal
if err := json.Unmarshal(pm.Data, &paypalData); err != nil {
return err
}
i.Data = paypalData
default:
return fmt.Errorf("unknown payment method type: %s", i.Type)
}
i.Created = (*time.Time)(pm.Created)
return nil
}
// ListPaymentMethods lists PaymentMethods
func (c *Client) ListPaymentMethods(ctx context.Context, opts *ListOptions) ([]PaymentMethod, error) {
return getPaginatedResults[PaymentMethod](ctx, c, "account/payment-methods", opts)
}
// GetPaymentMethod gets the payment method with the provided ID
func (c *Client) GetPaymentMethod(ctx context.Context, paymentMethodID int) (*PaymentMethod, error) {
e := formatAPIPath("account/payment-methods/%d", paymentMethodID)
return doGETRequest[PaymentMethod](ctx, c, e)
}
// DeletePaymentMethod deletes the payment method with the provided ID
func (c *Client) DeletePaymentMethod(ctx context.Context, paymentMethodID int) error {
e := formatAPIPath("account/payment-methods/%d", paymentMethodID)
return doDELETERequest(ctx, c, e)
}
// AddPaymentMethod adds the provided payment method to the account
func (c *Client) AddPaymentMethod(ctx context.Context, opts PaymentMethodCreateOptions) error {
_, err := doPOSTRequest[PaymentMethod, any](ctx, c, "account/payment-methods", opts)
return err
}
// SetDefaultPaymentMethod sets the payment method with the provided ID as the default
func (c *Client) SetDefaultPaymentMethod(ctx context.Context, paymentMethodID int) error {
e := formatAPIPath("account/payment-methods/%d", paymentMethodID)
_, err := doPOSTRequest[PaymentMethod, any](ctx, c, e)
return err
}