/
paymentmethod.go
121 lines (91 loc) · 2.62 KB
/
paymentmethod.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
package stripeutil
import (
"encoding/json"
"strings"
"github.com/stripe/stripe-go/v72"
)
// PaymentMethod is the PaymentMethod resource from Stripe. Embedded in this
// struct is the stripe.PaymentMethod struct from Stripe.
type PaymentMethod struct {
*stripe.PaymentMethod
Default bool // Default is whether or not this is a default PaymentMethod for the Customer.
}
var (
_ Resource = (*PaymentMethod)(nil)
paymentMethodEndpoint = "/v1/payment_methods"
)
func postPaymentMethod(s *Stripe, uri string, params map[string]interface{}) (*PaymentMethod, error) {
pm := &PaymentMethod{
PaymentMethod: &stripe.PaymentMethod{},
}
resp, err := s.Post(uri, params)
if err != nil {
return pm, err
}
defer resp.Body.Close()
if !respCode2xx(resp.StatusCode) {
return pm, s.Error(resp)
}
err = json.NewDecoder(resp.Body).Decode(&pm.PaymentMethod)
return pm, err
}
// RetrievePaymentMethod will get the PaymentMethod of the given ID from Stripe
// and return it.
func RetrievePaymentMethod(s *Stripe, id string) (*PaymentMethod, error) {
pm := &PaymentMethod{
PaymentMethod: &stripe.PaymentMethod{
ID: id,
},
}
resp, err := s.Get(pm.Endpoint())
if err != nil {
return pm, err
}
defer resp.Body.Close()
if !respCode2xx(resp.StatusCode) {
return pm, s.Error(resp)
}
err = json.NewDecoder(resp.Body).Decode(&pm)
return pm, err
}
// Update will update the current PaymentMethod in Stripe with the given Params.
func (pm *PaymentMethod) Update(s *Stripe, params Params) error {
var err error
pm, err = postPaymentMethod(s, pm.Endpoint(), params)
return err
}
// Attach will attach the current PaymentMethod to the given Customer.
func (pm *PaymentMethod) Attach(s *Stripe, c *Customer) error {
var err error
pm, err = postPaymentMethod(s, pm.Endpoint("attach"), Params{"customer": c.ID})
return err
}
// Detach will detach the current PaymentMethod from the Customer it was
// previously attached to.
func (pm *PaymentMethod) Detach(s *Stripe) error {
_, err := postPaymentMethod(s, pm.Endpoint("detach"), nil)
return err
}
// Endpoint implements the Resource interface.
func (pm *PaymentMethod) Endpoint(uris ...string) string {
endpoint := paymentMethodEndpoint
if pm.ID != "" {
endpoint += "/" + pm.ID
}
if len(uris) > 0 {
endpoint += "/"
}
return endpoint + strings.Join(uris, "/")
}
// Load implements the Resource interface.
func (pm *PaymentMethod) Load(s *Stripe) error {
resp, err := s.Client.Get(pm.Endpoint())
if err != nil {
return err
}
defer resp.Body.Close()
if !respCode2xx(resp.StatusCode) {
return s.Error(resp)
}
return json.NewDecoder(resp.Body).Decode(&pm.PaymentMethod)
}