Skip to content

Commit

Permalink
feat: adds new cc and bank functions for ReferralCustomers
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Jan 7, 2025
1 parent 0f38da0 commit 4b393d0
Show file tree
Hide file tree
Showing 9 changed files with 431 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## Next Release

- Adds the following functions to assist ReferralCustomers add credit cards and bank accounts:
- `BetaReferralCustomerService.retrieve_credit_card_client_secret`
- `BetaReferralCustomerService.retrieve_bank_account_client_secret`
- `ReferralCustomerService.add_credit_card_from_stripe`
- `ReferralCustomerService.add_bank_account_from_stripe`

## v9.5.0 (2024-10-24)

- Adds `tracking_codes` as a parameter of the `all` method on the TrackerService
Expand Down
36 changes: 30 additions & 6 deletions easypost/services/beta_referral_customer_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import (
Any,
Dict,
Optional,
)

from easypost.easypost_object import convert_to_easypost_object
Expand All @@ -25,7 +26,7 @@ def add_payment_method(
EasyPost, we will associate your Stripe payment method with either your primary
or secondary EasyPost payment method.
"""
wrapped_params = {
params = {
"payment_method": {
"stripe_customer_id": stripe_customer_id,
"payment_method_reference": payment_method_reference,
Expand All @@ -36,33 +37,56 @@ def add_payment_method(
response = Requestor(self._client).request(
method=RequestMethod.POST,
url="/referral_customers/payment_method",
params=wrapped_params,
params=params,
beta=True,
)

return convert_to_easypost_object(response=response)

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

response = Requestor(self._client).request(
method=RequestMethod.POST,
url="/referral_customers/refunds",
params=wrapped_params,
params=params,
beta=True,
)

return convert_to_easypost_object(response=response)

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

response = Requestor(self._client).request(
method=RequestMethod.POST,
url="/referral_customers/refunds",
params=wrapped_params,
params=params,
beta=True,
)

return convert_to_easypost_object(response=response)

def retrieve_credit_card_client_secret(self) -> Dict[str, Any]:
"""Retrieves a client secret to use with Stripe when adding a credit card."""
response = Requestor(self._client).request(
method=RequestMethod.POST,
url="/setup_intents",
beta=True,
)

return convert_to_easypost_object(response=response)

def retrieve_bank_account_client_secret(self, return_url: Optional[str] = None) -> Dict[str, Any]:
"""Retrieves a client secret to use with Stripe when adding a bank account."""
params = {"return_url": return_url}

response = Requestor(self._client).request(
method=RequestMethod.POST,
url="/financial_connections_sessions",
params=params if return_url else None,
beta=True,
)

Expand Down
60 changes: 59 additions & 1 deletion easypost/services/referral_customer_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def add_credit_card(
cvc: str,
priority: str = "primary",
) -> Dict[str, Any]:
"""Add credit card to a referral customer.
"""Add a credit card to EasyPost for a ReferralCustomer without needing a Stripe account.
This function requires the ReferralCustomer User's API key.
"""
Expand All @@ -129,6 +129,64 @@ def add_credit_card(

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(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4b393d0

Please sign in to comment.