Skip to content

Commit 6b002bb

Browse files
authored
Merge pull request #249 from EasyPost/payment_method_refund
feat: add payment method and refund functions
2 parents a7743c3 + 3f8fa69 commit 6b002bb

10 files changed

+346
-13
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## Next Release
4+
5+
- Adds new beta billing functionality for ReferralCustomer users
6+
- `add_payment_method` can add a pre-existing Stripe bank account or credit card to your EasyPost account
7+
- `refund_by_amount` refunds your wallet by a dollar amount
8+
- `refund_by_payment_log` refunds you wallet by a PaymentLog ID
9+
310
## v7.7.0 (2022-12-07)
411

512
- Routes requests for creating a carrier account with a custom workflow (eg: FedEx, UPS) to the correct endpoint when using the `create` function

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@ Some tests may require an EasyPost user with a particular set of enabled feature
118118

119119
- `USPS_CARRIER_ACCOUNT_ID` (eg: one-call buying a shipment for non-EasyPost employees)
120120
- `PARTNER_USER_PROD_API_KEY` (eg: creating a referral user)
121-
- `REFERRAL_USER_PROD_API_KEY` (eg: adding a credit card to a referral user)
121+
- `REFERRAL_CUSTOMER_PROD_API_KEY` (eg: adding a credit card to a referral user)

easypost/beta/referral.py

+61
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,67 @@ def add_credit_card(
109109
)
110110
return convert_to_easypost_object(response)
111111

112+
@staticmethod
113+
def add_payment_method(
114+
stripe_customer_id: str,
115+
payment_method_reference: str,
116+
primary_or_secondary: str = "primary",
117+
) -> Dict[str, Any]:
118+
"""Add a Stripe payment method to your EasyPost account.
119+
120+
This endpoint uses a user's personal Stripe account. The `stripe_customer_id`
121+
and `payment_method_reference` IDs both come from Stripe. By adding these to
122+
EasyPost, we will associate your Stripe payment method with either your primary
123+
or secondary EasyPost payment method.
124+
"""
125+
requestor = Requestor()
126+
wrapped_params = {
127+
"payment_method": {
128+
"stripe_customer_id": stripe_customer_id,
129+
"payment_method_reference": payment_method_reference,
130+
"priority": primary_or_secondary,
131+
}
132+
}
133+
134+
response, api_key = requestor.request(
135+
method=RequestMethod.POST,
136+
url="/referral_customers/payment_method",
137+
params=wrapped_params,
138+
beta=True,
139+
)
140+
141+
return convert_to_easypost_object(response=response, api_key=api_key)
142+
143+
@staticmethod
144+
def refund_by_amount(refund_amount: int) -> Dict[str, Any]:
145+
"""Refund a ReferralCustomer wallet by specifying an amount."""
146+
requestor = Requestor()
147+
wrapped_params = {"refund_amount": refund_amount}
148+
149+
response, api_key = requestor.request(
150+
method=RequestMethod.POST,
151+
url="/referral_customers/refunds",
152+
params=wrapped_params,
153+
beta=True,
154+
)
155+
156+
return convert_to_easypost_object(response=response, api_key=api_key)
157+
158+
@staticmethod
159+
def refund_by_payment_log(payment_log_id: str) -> Dict[str, Any]:
160+
"""Refund a ReferralCustomer wallet by specifying a payment log ID to completely refund."""
161+
requestor = Requestor()
162+
wrapped_params = {"payment_log_id": payment_log_id}
163+
164+
response, api_key = requestor.request(
165+
method=RequestMethod.POST,
166+
url="/referral_customers/refunds",
167+
params=wrapped_params,
168+
beta=True,
169+
)
170+
171+
return convert_to_easypost_object(response=response, api_key=api_key)
172+
112173
@staticmethod
113174
def _retrieve_easypost_stripe_api_key() -> str:
114175
"""Retrieve EasyPost's Stripe public API key."""

easypost/referral.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
)
1616

1717

18+
# TODO: Rename `Referral` to `ReferralCustomer` and have the module name match as well
1819
class Referral:
1920
@staticmethod
2021
def create(
@@ -94,7 +95,11 @@ def add_credit_card(
9495

9596
try:
9697
stripe_token = Referral._create_stripe_token(
97-
number, expiration_month, expiration_year, cvc, easypost_stripe_api_key
98+
number,
99+
expiration_month,
100+
expiration_year,
101+
cvc,
102+
easypost_stripe_api_key,
98103
)
99104
except Exception:
100105
raise Error(message="Could not send card details to Stripe, please try again later")

tests/cassettes/test_beta_referral_add_payment_method.yaml

+72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_beta_referral_refund_by_amount.yaml

+69
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_beta_referral_refund_by_payment_log.yaml

+68
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/conftest.py

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
EASYPOST_TEST_API_KEY = os.getenv("EASYPOST_TEST_API_KEY")
1818
EASYPOST_PROD_API_KEY = os.getenv("EASYPOST_PROD_API_KEY")
1919
PARTNER_USER_PROD_API_KEY = os.getenv("PARTNER_USER_PROD_API_KEY", "123")
20+
REFERRAL_CUSTOMER_PROD_API_KEY = os.getenv("REFERRAL_CUSTOMER_PROD_API_KEY", "123")
2021

2122
SCRUBBED_STRING = "<REDACTED>"
2223
SCRUBBED_ARRAY: List = []
@@ -71,6 +72,15 @@ def partner_prod_api_key():
7172
easypost.api_key = default_key
7273

7374

75+
@pytest.fixture
76+
def referral_customer_prod_api_key():
77+
"""If a test needs to use the referral customer prod api key, make it depend on this fixture."""
78+
default_key = easypost.api_key
79+
easypost.api_key = REFERRAL_CUSTOMER_PROD_API_KEY
80+
yield
81+
easypost.api_key = default_key
82+
83+
7484
@pytest.fixture(autouse=True)
7585
def check_expired_cassettes(expiration_days: int = 180, throw_error: bool = False):
7686
"""Checks for expired cassettes and throws errors if they are too old and must be re-recorded."""

0 commit comments

Comments
 (0)