forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreferral.go
190 lines (156 loc) · 6.79 KB
/
referral.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package easypost
import (
"context"
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
// A ReferralCustomer contains data about an EasyPost referral customer.
type ReferralCustomer struct {
User
}
// ListReferralCustomersResult holds the results from the list referral customers API call.
type ListReferralCustomersResult struct {
ReferralCustomers []*ReferralCustomer `json:"referral_customers,omitempty"`
// HasMore indicates if there are more responses to be fetched. If True,
// additional responses can be fetched by updating the ListOptions
// parameter's AfterID field with the ID of the last item in this object's
// ReferralCustomers field.
HasMore bool `json:"has_more,omitempty"`
}
// CreditCardOptions specifies options for creating or updating a credit card.
type CreditCardOptions struct {
Number string `json:"number,omitempty"`
ExpMonth string `json:"expiration_month,omitempty"`
ExpYear string `json:"expiration_year,omitempty"`
Cvc string `json:"cvc,omitempty"`
}
type stripeApiKeyResponse struct {
PublicKey string `json:"public_key"`
}
type stripeTokenResponse struct {
Id string `json:"id"`
}
type referralCustomerRequest struct {
UserOptions *UserOptions `json:"user,omitempty"`
}
type creditCardCreateRequest struct {
CreditCard *easypostCreditCardCreateOptions `json:"credit_card,omitempty"`
}
type easypostCreditCardCreateOptions struct {
StripeToken string `json:"stripe_object_id"`
Priority string `json:"priority"`
}
// CreateReferralCustomer creates a new referral customer.
func (c *Client) CreateReferralCustomer(in *UserOptions) (out *ReferralCustomer, err error) {
return c.CreateReferralCustomerWithContext(context.Background(), in)
}
// CreateReferralCustomerWithContext performs the same operation as CreateReferralCustomer, but allows
// specifying a context that can interrupt the request.
func (c *Client) CreateReferralCustomerWithContext(ctx context.Context, in *UserOptions) (out *ReferralCustomer, err error) {
err = c.post(ctx, "referral_customers", &referralCustomerRequest{UserOptions: in}, &out)
return
}
// ListReferralCustomers provides a paginated result of ReferralCustomer objects.
func (c *Client) ListReferralCustomers(opts *ListOptions) (out *ListReferralCustomersResult, err error) {
return c.ListReferralCustomersWithContext(context.Background(), opts)
}
// ListReferralCustomersWithContext performs the same operation as ListReferralCustomers, but
// allows specifying a context that can interrupt the request.
func (c *Client) ListReferralCustomersWithContext(ctx context.Context, opts *ListOptions) (out *ListReferralCustomersResult, err error) {
err = c.do(ctx, http.MethodGet, "referral_customers", c.convertOptsToURLValues(opts), &out)
return
}
// UpdateReferralCustomerEmail updates a ReferralCustomer's email address
func (c *Client) UpdateReferralCustomerEmail(userId string, email string) (out *ReferralCustomer, err error) {
return c.UpdateReferralCustomerEmailWithContext(context.Background(), userId, email)
}
// UpdateReferralCustomerEmailWithContext performs the same operation as UpdateReferralCustomerEmail, but allows
// specifying a context that can interrupt the request.
func (c *Client) UpdateReferralCustomerEmailWithContext(ctx context.Context, userId string, email string) (out *ReferralCustomer, err error) {
req := referralCustomerRequest{
UserOptions: &UserOptions{
Email: &email,
},
}
err = c.put(ctx, "referral_customers/"+userId, req, &out)
return
}
// AddReferralCustomerCreditCard adds a credit card to a referral customer's account.
func (c *Client) AddReferralCustomerCreditCard(referralCustomerApiKey string, creditCardOptions *CreditCardOptions, priority PaymentMethodPriority) (out *PaymentMethodObject, err error) {
return c.AddReferralCustomerCreditCardWithContext(context.Background(), referralCustomerApiKey, creditCardOptions, priority)
}
// AddReferralCustomerCreditCardWithContext performs the same operation as AddReferralCustomerCreditCard, but allows
// specifying a context that can interrupt the request.
func (c *Client) AddReferralCustomerCreditCardWithContext(ctx context.Context, referralCustomerApiKey string, creditCardOptions *CreditCardOptions, priority PaymentMethodPriority) (out *PaymentMethodObject, err error) {
stripeApiKeyResponse, err := c.retrieveEasypostStripeApiKey(ctx)
if err != nil || stripeApiKeyResponse == nil || stripeApiKeyResponse.PublicKey == "" {
return nil, errors.New("could not create Stripe token, please try again later")
}
stripeTokenResponse, err := c.createStripeToken(ctx, stripeApiKeyResponse.PublicKey, creditCardOptions)
if err != nil || stripeTokenResponse == nil || stripeTokenResponse.Id == "" {
return nil, errors.New("could not send card details to Stripe, please try again later")
}
return c.createEasypostCreditCard(ctx, referralCustomerApiKey, stripeTokenResponse.Id, priority)
}
func (c *Client) retrieveEasypostStripeApiKey(ctx context.Context) (out *stripeApiKeyResponse, err error) {
err = c.get(ctx, "partners/stripe_public_key", &out)
return
}
func (c *Client) createStripeToken(ctx context.Context, stripeApiKey string, creditCardOptions *CreditCardOptions) (out *stripeTokenResponse, err error) {
client := http.Client{}
data := url.Values{}
data.Set("card[number]", creditCardOptions.Number)
data.Set("card[exp_month]", creditCardOptions.ExpMonth)
data.Set("card[exp_year]", creditCardOptions.ExpYear)
data.Set("card[cvc]", creditCardOptions.Cvc)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "https://api.stripe.com/v1/tokens", strings.NewReader(data.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Authorization", "Bearer "+stripeApiKey)
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
body, err := ioutil.ReadAll(resp.Body) // deprecated, but we have to keep it for legacy compatibility
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &out)
if err != nil {
return nil, err
}
return
}
func (c *Client) createEasypostCreditCard(ctx context.Context, referralCustomerApiKey string, stripeToken string, priority PaymentMethodPriority) (out *PaymentMethodObject, err error) {
client := &Client{
APIKey: referralCustomerApiKey,
}
priorityString := ""
switch priority {
case PrimaryPaymentMethodPriority:
priorityString = "primary"
case SecondaryPaymentMethodPriority:
priorityString = "secondary"
default:
return nil, errors.New("invalid priority")
}
creditCardOptions := &easypostCreditCardCreateOptions{
StripeToken: stripeToken,
Priority: priorityString,
}
creditCardRequest := &creditCardCreateRequest{
CreditCard: creditCardOptions,
}
err = client.post(ctx, "credit_cards", creditCardRequest, &out)
return
}