Skip to content

Commit 5ede5f7

Browse files
authored
combine credit card and bank account functions into a generic one. (#205)
1 parent a2367e9 commit 5ede5f7

File tree

8 files changed

+113
-108
lines changed

8 files changed

+113
-108
lines changed

CHANGELOG.md

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

33
## NEXT RELEASE
44

5-
- Adds `PaymentMethod.all()`, `CreditCard.fund()`, and `CreditCard.delete()` functions
5+
- Adds `Billing.retrieve_payment_methods()`, `Billing.fund_wallet()`, and `Billing.delete_payment_method()` functions
66
- Removes the unusable `carrier` param from `Address.verify()` along with the dead `message` conditional check that was missed in v7.0.0
77
- Adds OS specific details to the user-agent header
88

easypost/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import easypost.beta
22
from easypost.address import Address
33
from easypost.batch import Batch
4+
from easypost.billing import Billing
45
from easypost.brand import Brand
56
from easypost.carrier_account import CarrierAccount
6-
from easypost.credit_card import CreditCard
77
from easypost.customs_info import CustomsInfo
88
from easypost.customs_item import CustomsItem
99
from easypost.error import Error
1010
from easypost.event import Event
1111
from easypost.insurance import Insurance
1212
from easypost.order import Order
1313
from easypost.parcel import Parcel
14-
from easypost.payment_method import PaymentMethod
1514
from easypost.pickup import Pickup
1615
from easypost.pickup_rate import PickupRate
1716
from easypost.postage_label import PostageLabel

easypost/billing.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from typing import (
2+
Any,
3+
Dict,
4+
List,
5+
Optional,
6+
)
7+
8+
from easypost.easypost_object import convert_to_easypost_object
9+
from easypost.error import Error
10+
from easypost.requestor import (
11+
RequestMethod,
12+
Requestor,
13+
)
14+
from easypost.resource import (
15+
CreateResource,
16+
Resource,
17+
)
18+
19+
20+
class Billing(CreateResource, Resource):
21+
@classmethod
22+
def fund_wallet(cls, amount: str, primary_or_secondary: str = "primary", api_key: Optional[str] = None) -> bool:
23+
"""Fund your EasyPost wallet by charging your primary or secondary payment method on file."""
24+
endpoint, payment_method_id = Billing._get_payment_method_info(primary_or_secondary=primary_or_secondary)
25+
26+
requestor = Requestor(local_api_key=api_key)
27+
url = f"{endpoint}/{payment_method_id}/charges"
28+
wrapped_params = {"amount": amount}
29+
requestor.request(method=RequestMethod.POST, url=url, params=wrapped_params)
30+
31+
# Return true if the request succeeds, an error will be thrown if it fails
32+
return True
33+
34+
@classmethod
35+
def delete_payment_method(cls, primary_or_secondary: str, api_key: Optional[str] = None) -> bool:
36+
"""Delete a payment method."""
37+
endpoint, payment_method_id = Billing._get_payment_method_info(primary_or_secondary=primary_or_secondary)
38+
39+
requestor = Requestor(local_api_key=api_key)
40+
url = f"{endpoint}/{payment_method_id}"
41+
requestor.request(method=RequestMethod.DELETE, url=url)
42+
43+
# Return true if the request succeeds, an error will be thrown if it fails
44+
return True
45+
46+
@classmethod
47+
def retrieve_payment_methods(cls, api_key: Optional[str] = None, **params) -> Dict[str, Any]:
48+
"""Retrieve payment methods."""
49+
requestor = Requestor(local_api_key=api_key)
50+
response, api_key = requestor.request(method=RequestMethod.GET, url="/payment_methods", params=params)
51+
52+
if response.get("id") is None:
53+
raise Error(message="Billing has not been setup for this user. Please add a payment method.")
54+
55+
return convert_to_easypost_object(response=response, api_key=api_key)
56+
57+
@classmethod
58+
def _get_payment_method_info(cls, primary_or_secondary: str = "primary") -> List[str]:
59+
"""Get payment method info (type of the payment method and ID of the payment method)"""
60+
payment_methods = Billing.retrieve_payment_methods()
61+
62+
payment_method_map = {
63+
"primary": "primary_payment_method",
64+
"secondary": "secondary_payment_method",
65+
}
66+
67+
payment_method_to_use = payment_method_map.get(primary_or_secondary)
68+
error_string = "The chosen payment method is not valid. Please try again."
69+
70+
if payment_method_to_use and payment_methods[payment_method_to_use]:
71+
payment_method_id = payment_methods[payment_method_to_use]["id"]
72+
if payment_method_id.startswith("card_"):
73+
endpoint = "/credit_cards"
74+
elif payment_method_id.startswith("bank_"):
75+
endpoint = "/bank_accounts"
76+
else:
77+
raise Error(message=error_string)
78+
else:
79+
raise Error(message=error_string)
80+
81+
return [endpoint, payment_method_id]

easypost/credit_card.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

easypost/payment_method.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/test_billing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
3+
import easypost
4+
5+
6+
@pytest.mark.vcr()
7+
@pytest.mark.skip("Skipping due to the lack of an available real payment method in tests")
8+
def test_billing_fund_wallet(prod_api_key):
9+
success = easypost.Billing.fund_wallet(
10+
amount="2000",
11+
primary_or_secondary="primary",
12+
)
13+
14+
assert success is True
15+
16+
17+
@pytest.mark.vcr()
18+
@pytest.mark.skip("Skipping due to the lack of an available real payment method in tests")
19+
def test_billing_payment_method_delete(prod_api_key):
20+
success = easypost.Billing.delete_payment_method(primary_or_secondary="primary")
21+
22+
assert success is True
23+
24+
25+
@pytest.mark.vcr()
26+
@pytest.mark.skip("Skipping due to the lack of an available real payment method in tests")
27+
def test_billing_retrieve_payment_methods(prod_api_key):
28+
payment_methods = easypost.Billing.retrieve_payment_methods()
29+
30+
assert payment_methods is not None

tests/test_credit_card.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/test_payment_method.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)