Skip to content

Commit 8b20f82

Browse files
authored
[bug] Cannot determine payment method when funding/deleting (#332)
- Use payment method object type to determine payment method type
1 parent aebdf30 commit 8b20f82

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Next Release
44

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

78
## v9.1.0 (2024-01-08)

easypost/services/billing_service.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ def _get_payment_method_info(self, priority: str = "primary") -> List[str]:
6666

6767
if payment_method_to_use and payment_methods[payment_method_to_use]:
6868
payment_method_id = payment_methods[payment_method_to_use]["id"]
69-
if payment_method_id.startswith("card_"):
69+
payment_method_object_type = payment_methods[payment_method_to_use].get("object", None)
70+
if payment_method_object_type == "CreditCard":
7071
endpoint = "/credit_cards"
71-
elif payment_method_id.startswith("bank_"):
72+
elif payment_method_object_type == "BankAccount":
7273
endpoint = "/bank_accounts"
7374
else:
7475
raise InvalidObjectError(message=INVALID_PAYMENT_METHOD_ERROR)

tests/test_billing.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,26 @@ def test_billing_fund_wallet(mock_request, mock_get_payment_info, prod_client):
2424

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

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

3636

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

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

4848

4949
@patch(
@@ -85,3 +85,23 @@ def test_billing_retrieve_payment_methods_no_billing_setup(mock_request, prod_cl
8585

8686
mock_request.assert_called_once()
8787
assert str(error.value) == "Billing has not been setup for this user. Please add a payment method."
88+
89+
90+
@patch(
91+
"easypost.services.billing_service.BillingService.retrieve_payment_methods",
92+
return_value={
93+
"primary_payment_method": {"id": "pm_123", "object": "CreditCard"},
94+
"secondary_payment_method": {"id": "pm_456", "object": "BankAccount"},
95+
},
96+
)
97+
def test_billing__get_payment_method_info_by_object_type(mock_request, prod_client):
98+
"""Tests we can determine the payment method type/endpoint by object type."""
99+
endpoint, payment_method_id = prod_client.billing._get_payment_method_info(priority="primary")
100+
101+
assert endpoint == "/credit_cards"
102+
assert payment_method_id == "pm_123"
103+
104+
endpoint, payment_method_id = prod_client.billing._get_payment_method_info(priority="secondary")
105+
106+
assert endpoint == "/bank_accounts"
107+
assert payment_method_id == "pm_456"

0 commit comments

Comments
 (0)