Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Cannot determine payment method when funding/deleting #332

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Justintime50 marked this conversation as resolved.
Show resolved Hide resolved
if payment_method_id.startswith("card_") or payment_method_object_type == "CreditCard":
endpoint = "/credit_cards"
elif payment_method_id.startswith("bank_"):
elif payment_method_id.startswith("bank_") or payment_method_object_type == "BankAccount":
endpoint = "/bank_accounts"
else:
raise InvalidObjectError(message=INVALID_PAYMENT_METHOD_ERROR)
Expand Down
48 changes: 44 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,43 @@ 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"


@patch(
"easypost.services.billing_service.BillingService.retrieve_payment_methods",
return_value={
"primary_payment_method": {"id": "card_123", "object": None},
"secondary_payment_method": {"id": "bank_123", "object": None},
},
)
def test_billing__get_payment_method_info_by_legacy_id_prefix(mock_request, prod_client):
"""Tests we can determine the payment method type/endpoint by legacy ID prefix."""
endpoint, payment_method_id = prod_client.billing._get_payment_method_info(priority="primary")

assert endpoint == "/credit_cards"
assert payment_method_id == "card_123"

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

assert endpoint == "/bank_accounts"
assert payment_method_id == "bank_123"
Loading