Skip to content

Commit

Permalink
[bug] Cannot determine payment method when funding/deleting (#332)
Browse files Browse the repository at this point in the history
- Use payment method object type to determine payment method type
  • Loading branch information
nwithan8 committed Apr 10, 2024
1 parent aebdf30 commit 8b20f82
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next Release

- Fix payment method funding and deletion failures due to undetermined payment method type
- Adds `refund` function in Insurance service for requesting a refund for a standalone insurance

## v9.1.0 (2024-01-08)
Expand Down
5 changes: 3 additions & 2 deletions easypost/services/billing_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ def _get_payment_method_info(self, priority: str = "primary") -> List[str]:

if payment_method_to_use and payment_methods[payment_method_to_use]:
payment_method_id = payment_methods[payment_method_to_use]["id"]
if payment_method_id.startswith("card_"):
payment_method_object_type = payment_methods[payment_method_to_use].get("object", None)
if payment_method_object_type == "CreditCard":
endpoint = "/credit_cards"
elif payment_method_id.startswith("bank_"):
elif payment_method_object_type == "BankAccount":
endpoint = "/bank_accounts"
else:
raise InvalidObjectError(message=INVALID_PAYMENT_METHOD_ERROR)
Expand Down
28 changes: 24 additions & 4 deletions tests/test_billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ def test_billing_fund_wallet(mock_request, mock_get_payment_info, prod_client):

@patch(
"easypost.services.billing_service.BillingService.retrieve_payment_methods",
return_value={"primary_payment_method": {"id": "card_123"}},
return_value={"primary_payment_method": {"id": "pm_123", "object": "CreditCard"}},
)
@patch("easypost.services.billing_service.Requestor.request", return_value={"mock": "response"})
def test_billing_payment_method_delete_credit_card(mock_request, mock_payment_methods, prod_client):
"""Tests we make a valid call to delete a credit card."""
prod_client.billing.delete_payment_method(priority="primary")

mock_request.assert_called_once_with(method=easypost.requestor.RequestMethod.DELETE, url="/credit_cards/card_123")
mock_request.assert_called_once_with(method=easypost.requestor.RequestMethod.DELETE, url="/credit_cards/pm_123")


@patch(
"easypost.services.billing_service.BillingService.retrieve_payment_methods",
return_value={"primary_payment_method": {"id": "bank_123"}},
return_value={"primary_payment_method": {"id": "pm_123", "object": "BankAccount"}},
)
@patch("easypost.services.billing_service.Requestor.request", return_value={"mock": "response"})
def test_billing_payment_method_delete_bank_account(mock_request, mock_payment_methods, prod_client):
"""Tests we make a valid call to delete a bank account."""
prod_client.billing.delete_payment_method(priority="primary")

mock_request.assert_called_once_with(method=easypost.requestor.RequestMethod.DELETE, url="/bank_accounts/bank_123")
mock_request.assert_called_once_with(method=easypost.requestor.RequestMethod.DELETE, url="/bank_accounts/pm_123")


@patch(
Expand Down Expand Up @@ -85,3 +85,23 @@ def test_billing_retrieve_payment_methods_no_billing_setup(mock_request, prod_cl

mock_request.assert_called_once()
assert str(error.value) == "Billing has not been setup for this user. Please add a payment method."


@patch(
"easypost.services.billing_service.BillingService.retrieve_payment_methods",
return_value={
"primary_payment_method": {"id": "pm_123", "object": "CreditCard"},
"secondary_payment_method": {"id": "pm_456", "object": "BankAccount"},
},
)
def test_billing__get_payment_method_info_by_object_type(mock_request, prod_client):
"""Tests we can determine the payment method type/endpoint by object type."""
endpoint, payment_method_id = prod_client.billing._get_payment_method_info(priority="primary")

assert endpoint == "/credit_cards"
assert payment_method_id == "pm_123"

endpoint, payment_method_id = prod_client.billing._get_payment_method_info(priority="secondary")

assert endpoint == "/bank_accounts"
assert payment_method_id == "pm_456"

0 comments on commit 8b20f82

Please sign in to comment.