Skip to content

Commit 5f99649

Browse files
committed
feat: adds new cc and bank functions for ReferralCustomers
1 parent 504c590 commit 5f99649

9 files changed

+437
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
## Next Release
44

55
- Drops support for Python 3.7 and 3.8
6+
- Adds the following functions to assist ReferralCustomers add credit cards and bank accounts:
7+
- `BetaReferralCustomerService.retrieve_credit_card_client_secret`
8+
- `BetaReferralCustomerService.retrieve_bank_account_client_secret`
9+
- `ReferralCustomerService.add_credit_card_from_stripe`
10+
- `ReferralCustomerService.add_bank_account_from_stripe`
611
- Fixes the payload wrapping for updating a webhook
712
- Removes deprecated `user.all_api_keys` and `user.api_keys`, use `api_key.all` and `api_key.retrieve_api_keys_for_user` respectively
813
- Bumps all dev dependencies

easypost/services/beta_referral_customer_service.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def add_payment_method(
2424
EasyPost, we will associate your Stripe payment method with either your primary
2525
or secondary EasyPost payment method.
2626
"""
27-
wrapped_params = {
27+
params = {
2828
"payment_method": {
2929
"stripe_customer_id": stripe_customer_id,
3030
"payment_method_reference": payment_method_reference,
@@ -35,33 +35,58 @@ def add_payment_method(
3535
response = Requestor(self._client).request(
3636
method=RequestMethod.POST,
3737
url="/referral_customers/payment_method",
38-
params=wrapped_params,
38+
params=params,
3939
beta=True,
4040
)
4141

4242
return convert_to_easypost_object(response=response)
4343

4444
def refund_by_amount(self, refund_amount: int) -> dict[str, Any]:
4545
"""Refund a ReferralCustomer wallet by specifying an amount."""
46-
wrapped_params = {"refund_amount": refund_amount}
46+
params = {"refund_amount": refund_amount}
4747

4848
response = Requestor(self._client).request(
4949
method=RequestMethod.POST,
5050
url="/referral_customers/refunds",
51-
params=wrapped_params,
51+
params=params,
5252
beta=True,
5353
)
5454

5555
return convert_to_easypost_object(response=response)
5656

5757
def refund_by_payment_log(self, payment_log_id: str) -> dict[str, Any]:
5858
"""Refund a ReferralCustomer wallet by specifying a payment log ID to completely refund."""
59-
wrapped_params = {"payment_log_id": payment_log_id}
59+
params = {"payment_log_id": payment_log_id}
6060

6161
response = Requestor(self._client).request(
6262
method=RequestMethod.POST,
6363
url="/referral_customers/refunds",
64-
params=wrapped_params,
64+
params=params,
65+
beta=True,
66+
)
67+
68+
return convert_to_easypost_object(response=response)
69+
70+
def retrieve_credit_card_client_secret(self) -> Dict[str, Any]:
71+
"""Retrieves a client secret to use with Stripe when adding a credit card."""
72+
response = Requestor(self._client).request(
73+
method=RequestMethod.POST,
74+
url="/setup_intents",
75+
beta=True,
76+
)
77+
78+
return convert_to_easypost_object(response=response)
79+
80+
def retrieve_bank_account_client_secret(
81+
self, return_url: Optional[str] = None
82+
) -> Dict[str, Any]:
83+
"""Retrieves a client secret to use with Stripe when adding a bank account."""
84+
params = {"return_url": return_url}
85+
86+
response = Requestor(self._client).request(
87+
method=RequestMethod.POST,
88+
url="/financial_connections_sessions",
89+
params=params if return_url else None,
6590
beta=True,
6691
)
6792

easypost/services/referral_customer_service.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
)
66

77
import requests
8+
89
from easypost.constant import (
910
_FILTERS_KEY,
1011
SEND_STRIPE_DETAILS_ERROR,
@@ -69,9 +70,13 @@ def all(self, **params) -> dict[str, Any]:
6970

7071
url = "/referral_customers"
7172

72-
response = Requestor(self._client).request(method=RequestMethod.GET, url=url, params=params)
73+
response = Requestor(self._client).request(
74+
method=RequestMethod.GET, url=url, params=params
75+
)
7376

74-
response[_FILTERS_KEY] = filters # Save the filters used to reference in potential get_next_page call
77+
response[_FILTERS_KEY] = (
78+
filters # Save the filters used to reference in potential get_next_page call
79+
)
7580

7681
return convert_to_easypost_object(response=response)
7782

@@ -102,8 +107,8 @@ def add_credit_card(
102107
expiration_year: int,
103108
cvc: str,
104109
priority: str = "primary",
105-
) -> dict[str, Any]:
106-
"""Add credit card to a referral customer.
110+
) -> Dict[str, Any]:
111+
"""Add a credit card to EasyPost for a ReferralCustomer without needing a Stripe account.
107112
108113
This function requires the ReferralCustomer User's API key.
109114
"""
@@ -128,6 +133,64 @@ def add_credit_card(
128133

129134
return convert_to_easypost_object(response)
130135

136+
def add_credit_card_from_stripe(
137+
self,
138+
referral_api_key: str,
139+
payment_method_id: str,
140+
priority: str = "primary",
141+
) -> Dict[str, Any]:
142+
"""Add a credit card to EasyPost for a ReferralCustomer with a payment method ID from Stripe.
143+
144+
This function requires the ReferralCustomer User's API key.
145+
"""
146+
params = {
147+
"credit_card": {
148+
"payment_method_id": payment_method_id,
149+
"priority": priority,
150+
}
151+
}
152+
153+
# Override the API key to use the referral's for this single request
154+
referral_client = deepcopy(self._client)
155+
referral_client.api_key = referral_api_key
156+
157+
response = Requestor(referral_client).request(
158+
method=RequestMethod.POST,
159+
params=params,
160+
url="/credit_cards",
161+
)
162+
163+
return convert_to_easypost_object(response)
164+
165+
def add_bank_account_from_stripe(
166+
self,
167+
referral_api_key: str,
168+
financial_connections_id: str,
169+
mandate_data: Dict[str, Any],
170+
priority: str = "primary",
171+
) -> Dict[str, Any]:
172+
"""Add a bank account to EasyPost for a ReferralCustomer.
173+
174+
This function requires the ReferralCustomer User's API key.
175+
"""
176+
params = {
177+
"financial_connections_id": financial_connections_id,
178+
"mandate_data": mandate_data,
179+
"priority": priority,
180+
}
181+
182+
# Override the API key to use the referral's for this single request
183+
referral_client = deepcopy(self._client)
184+
referral_client.api_key = referral_api_key
185+
186+
response = Requestor(referral_client).request(
187+
method=RequestMethod.POST,
188+
params=params,
189+
url="/bank_accounts",
190+
)
191+
192+
return convert_to_easypost_object(response)
193+
131194
def _retrieve_easypost_stripe_api_key(self) -> str:
132195
"""Retrieve EasyPost's Stripe public API key."""
133196
public_key = Requestor(self._client).request(

tests/cassettes/test_beta_referral_customer_retrieve_bank_account_client_secret.yaml

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_beta_referral_customer_retrieve_credit_card_client_secret.yaml

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_referral_customer_add_bank_account_from_stripe.yaml

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)