-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathreferral_customer_service.py
More file actions
259 lines (212 loc) · 7.45 KB
/
referral_customer_service.py
File metadata and controls
259 lines (212 loc) · 7.45 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
from copy import deepcopy
from typing import (
Any,
Optional,
)
import requests
from easypost.constant import (
_FILTERS_KEY,
SEND_STRIPE_DETAILS_ERROR,
TIMEOUT,
)
from easypost.easypost_object import convert_to_easypost_object
from easypost.errors import ExternalApiError
from easypost.models import User
from easypost.requestor import (
RequestMethod,
Requestor,
)
from easypost.services.base_service import BaseService
class ReferralCustomerService(BaseService):
def __init__(self, client):
self._client = client
self._model_class = User.__name__
def create(self, **params) -> User:
"""Create a referral customer.
This function requires the Partner User's API key.
"""
wrapped_params = {"user": params}
response = Requestor(self._client).request(
method=RequestMethod.POST,
url="/referral_customers",
params=wrapped_params,
)
return convert_to_easypost_object(response=response)
def update_email(self, id: str, email: str) -> None:
"""Update a referral customer.
This function requires the Partner User's API key.
"""
url = f"/referral_customers/{id}"
wrapped_params = {
"user": {
"email": email,
}
}
Requestor(self._client).request(
method=RequestMethod.PUT,
url=url,
params=wrapped_params,
)
def all(self, **params) -> dict[str, Any]:
"""Retrieve a list of referral customers.
This function requires the Partner User's API key.
"""
filters = {
"key": "referral_customers",
}
url = "/referral_customers"
response = Requestor(self._client).request(method=RequestMethod.GET, url=url, params=params)
response[_FILTERS_KEY] = filters # Save the filters used to reference in potential get_next_page call
return convert_to_easypost_object(response=response)
def get_next_page(
self,
referral_customers: dict[str, Any],
page_size: int,
optional_params: Optional[dict[str, Any]] = None,
) -> dict[str, Any]:
"""Retrieve next page of referral customers."""
self._check_has_next_page(collection=referral_customers)
params = {
"before_id": referral_customers["referral_customers"][-1].id,
"page_size": page_size,
}
if optional_params:
params.update(optional_params)
return self.all(**params)
def add_credit_card(
self,
referral_api_key: str,
number: str,
expiration_month: int,
expiration_year: int,
cvc: str,
priority: str = "primary",
) -> dict[str, Any]:
"""Add a credit card to EasyPost for a ReferralCustomer without needing a Stripe account.
This function requires the ReferralCustomer User's API key.
"""
easypost_stripe_api_key = self._retrieve_easypost_stripe_api_key()
try:
stripe_token = self._create_stripe_token(
number,
expiration_month,
expiration_year,
cvc,
easypost_stripe_api_key,
)
except Exception:
raise ExternalApiError(message=SEND_STRIPE_DETAILS_ERROR)
response = self._create_easypost_credit_card(
referral_api_key,
stripe_token.get("id", ""),
priority=priority,
)
return convert_to_easypost_object(response)
def add_credit_card_from_stripe(
self,
referral_api_key: str,
payment_method_id: str,
priority: str = "primary",
) -> dict[str, Any]:
"""Add a credit card to EasyPost for a ReferralCustomer with a payment method ID from Stripe.
This function requires the ReferralCustomer User's API key.
"""
params = {
"credit_card": {
"payment_method_id": payment_method_id,
"priority": priority,
}
}
# Override the API key to use the referral's for this single request
referral_client = deepcopy(self._client)
referral_client.api_key = referral_api_key
response = Requestor(referral_client).request(
method=RequestMethod.POST,
params=params,
url="/credit_cards",
)
return convert_to_easypost_object(response)
def add_bank_account_from_stripe(
self,
referral_api_key: str,
financial_connections_id: str,
mandate_data: dict[str, Any],
priority: str = "primary",
) -> dict[str, Any]:
"""Add a bank account to EasyPost for a ReferralCustomer.
This function requires the ReferralCustomer User's API key.
"""
params = {
"financial_connections_id": financial_connections_id,
"mandate_data": mandate_data,
"priority": priority,
}
# Override the API key to use the referral's for this single request
referral_client = deepcopy(self._client)
referral_client.api_key = referral_api_key
response = Requestor(referral_client).request(
method=RequestMethod.POST,
params=params,
url="/bank_accounts",
)
return convert_to_easypost_object(response)
def _retrieve_easypost_stripe_api_key(self) -> str:
"""Retrieve EasyPost's Stripe public API key."""
public_key = Requestor(self._client).request(
method=RequestMethod.GET,
url="/partners/stripe_public_key",
)
return public_key.get("public_key", "")
def _create_stripe_token(
self,
number: str,
expiration_month: int,
expiration_year: int,
cvc: str,
easypost_stripe_key: str,
) -> dict[str, Any]:
"""Get credit card token from Stripe."""
headers = {
# This Stripe endpoint only accepts URL form encoded bodies
"Content-type": "application/x-www-form-urlencoded",
}
credit_card_dict = {
"card": {
"number": number,
"exp_month": expiration_month,
"exp_year": expiration_year,
"cvc": cvc,
}
}
form_encoded_params = Requestor.form_encode_params(credit_card_dict)
url = "https://api.stripe.com/v1/tokens"
stripe_response = requests.post(
url,
params=form_encoded_params,
headers=headers,
auth=requests.auth.HTTPBasicAuth(easypost_stripe_key, ""),
timeout=TIMEOUT,
)
return stripe_response.json()
def _create_easypost_credit_card(
self,
referral_api_key: str,
stripe_object_id: str,
priority: str = "primary",
) -> dict[str, Any]:
"""Submit Stripe credit card token to EasyPost."""
params = {
"credit_card": {
"stripe_object_id": stripe_object_id,
"priority": priority,
}
}
# Override the API key to use the referral's for this single request
referral_client = deepcopy(self._client)
referral_client.api_key = referral_api_key
response = Requestor(referral_client).request(
method=RequestMethod.POST,
params=params,
url="/credit_cards",
)
return response