Skip to content

Commit 9af5da5

Browse files
authored
Merge pull request #355 from EasyPost/new_partner_white_label_billing
feat: adds new cc and bank functions for ReferralCustomers
2 parents 504c590 + a706d4a commit 9af5da5

9 files changed

+428
-7
lines changed

CHANGELOG.md

+5
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.create_credit_card_client_secret`
8+
- `BetaReferralCustomerService.create_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

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import (
22
Any,
3+
Optional,
34
)
45

56
from easypost.easypost_object import convert_to_easypost_object
@@ -24,7 +25,7 @@ def add_payment_method(
2425
EasyPost, we will associate your Stripe payment method with either your primary
2526
or secondary EasyPost payment method.
2627
"""
27-
wrapped_params = {
28+
params = {
2829
"payment_method": {
2930
"stripe_customer_id": stripe_customer_id,
3031
"payment_method_reference": payment_method_reference,
@@ -35,33 +36,56 @@ def add_payment_method(
3536
response = Requestor(self._client).request(
3637
method=RequestMethod.POST,
3738
url="/referral_customers/payment_method",
38-
params=wrapped_params,
39+
params=params,
3940
beta=True,
4041
)
4142

4243
return convert_to_easypost_object(response=response)
4344

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

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

5556
return convert_to_easypost_object(response=response)
5657

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

6162
response = Requestor(self._client).request(
6263
method=RequestMethod.POST,
6364
url="/referral_customers/refunds",
64-
params=wrapped_params,
65+
params=params,
66+
beta=True,
67+
)
68+
69+
return convert_to_easypost_object(response=response)
70+
71+
def create_credit_card_client_secret(self) -> dict[str, Any]:
72+
"""Creates a client secret to use with Stripe when adding a credit card."""
73+
response = Requestor(self._client).request(
74+
method=RequestMethod.POST,
75+
url="/setup_intents",
76+
beta=True,
77+
)
78+
79+
return convert_to_easypost_object(response=response)
80+
81+
def create_bank_account_client_secret(self, return_url: Optional[str] = None) -> dict[str, Any]:
82+
"""Creates a client secret to use with Stripe when adding a bank account."""
83+
params = {"return_url": return_url}
84+
85+
response = Requestor(self._client).request(
86+
method=RequestMethod.POST,
87+
url="/financial_connections_sessions",
88+
params=params if return_url else None,
6589
beta=True,
6690
)
6791

easypost/services/referral_customer_service.py

+59-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def add_credit_card(
103103
cvc: str,
104104
priority: str = "primary",
105105
) -> dict[str, Any]:
106-
"""Add credit card to a referral customer.
106+
"""Add a credit card to EasyPost for a ReferralCustomer without needing a Stripe account.
107107
108108
This function requires the ReferralCustomer User's API key.
109109
"""
@@ -128,6 +128,64 @@ def add_credit_card(
128128

129129
return convert_to_easypost_object(response)
130130

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

tests/cassettes/test_beta_referral_customer_create_bank_account_client_secret.yaml

+69
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_beta_referral_customer_create_credit_card_client_secret.yaml

+69
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

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)