|
| 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] |
0 commit comments