Skip to content

Commit 4b393d0

Browse files
committed
feat: adds new cc and bank functions for ReferralCustomers
1 parent 0f38da0 commit 4b393d0

9 files changed

+431
-7
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# CHANGELOG
22

3+
## Next Release
4+
5+
- Adds the following functions to assist ReferralCustomers add credit cards and bank accounts:
6+
- `BetaReferralCustomerService.retrieve_credit_card_client_secret`
7+
- `BetaReferralCustomerService.retrieve_bank_account_client_secret`
8+
- `ReferralCustomerService.add_credit_card_from_stripe`
9+
- `ReferralCustomerService.add_bank_account_from_stripe`
10+
311
## v9.5.0 (2024-10-24)
412

513
- Adds `tracking_codes` as a parameter of the `all` method on the TrackerService

easypost/services/beta_referral_customer_service.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import (
22
Any,
33
Dict,
4+
Optional,
45
)
56

67
from easypost.easypost_object import convert_to_easypost_object
@@ -25,7 +26,7 @@ def add_payment_method(
2526
EasyPost, we will associate your Stripe payment method with either your primary
2627
or secondary EasyPost payment method.
2728
"""
28-
wrapped_params = {
29+
params = {
2930
"payment_method": {
3031
"stripe_customer_id": stripe_customer_id,
3132
"payment_method_reference": payment_method_reference,
@@ -36,33 +37,56 @@ def add_payment_method(
3637
response = Requestor(self._client).request(
3738
method=RequestMethod.POST,
3839
url="/referral_customers/payment_method",
39-
params=wrapped_params,
40+
params=params,
4041
beta=True,
4142
)
4243

4344
return convert_to_easypost_object(response=response)
4445

4546
def refund_by_amount(self, refund_amount: int) -> Dict[str, Any]:
4647
"""Refund a ReferralCustomer wallet by specifying an amount."""
47-
wrapped_params = {"refund_amount": refund_amount}
48+
params = {"refund_amount": refund_amount}
4849

4950
response = Requestor(self._client).request(
5051
method=RequestMethod.POST,
5152
url="/referral_customers/refunds",
52-
params=wrapped_params,
53+
params=params,
5354
beta=True,
5455
)
5556

5657
return convert_to_easypost_object(response=response)
5758

5859
def refund_by_payment_log(self, payment_log_id: str) -> Dict[str, Any]:
5960
"""Refund a ReferralCustomer wallet by specifying a payment log ID to completely refund."""
60-
wrapped_params = {"payment_log_id": payment_log_id}
61+
params = {"payment_log_id": payment_log_id}
6162

6263
response = Requestor(self._client).request(
6364
method=RequestMethod.POST,
6465
url="/referral_customers/refunds",
65-
params=wrapped_params,
66+
params=params,
67+
beta=True,
68+
)
69+
70+
return convert_to_easypost_object(response=response)
71+
72+
def retrieve_credit_card_client_secret(self) -> Dict[str, Any]:
73+
"""Retrieves a client secret to use with Stripe when adding a credit card."""
74+
response = Requestor(self._client).request(
75+
method=RequestMethod.POST,
76+
url="/setup_intents",
77+
beta=True,
78+
)
79+
80+
return convert_to_easypost_object(response=response)
81+
82+
def retrieve_bank_account_client_secret(self, return_url: Optional[str] = None) -> 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,
6690
beta=True,
6791
)
6892

easypost/services/referral_customer_service.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def add_credit_card(
104104
cvc: str,
105105
priority: str = "primary",
106106
) -> Dict[str, Any]:
107-
"""Add credit card to a referral customer.
107+
"""Add a credit card to EasyPost for a ReferralCustomer without needing a Stripe account.
108108
109109
This function requires the ReferralCustomer User's API key.
110110
"""
@@ -129,6 +129,64 @@ def add_credit_card(
129129

130130
return convert_to_easypost_object(response)
131131

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