Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## Next Release

- Removes the deprecated, unusable `add_credit_card` function
- Stripe has disabled the ability to pass plain credit card details over the wire and now requires using [Stripe.js/Elements/Checkout](https://support.stripe.com/questions/card-tokenization-restrictions-using-publishable-keys). Follow the [Decentralized (EasyPost-Manage Billing) Guide](https://docs.easypost.com/guides/get-started-with-forge/easypost-managed-billing-guide#referralcustomer-billing-management) for more details on the new flow to use.
- Makes `referral_customer.retrieve_easypost_stripe_api_key` public to help facilitate adding credit cards using Stripe.js

## v10.7.0 (2026-06-25)

- Adds `params` to `request_pin` ensuring users can pass `easypost_details` to the call.
Expand Down
103 changes: 1 addition & 102 deletions easypost/services/referral_customer_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
Optional,
)

import requests

from easypost.constant import (
_FILTERS_KEY,
SEND_STRIPE_DETAILS_ERROR,
TIMEOUT,
)
from easypost.easypost_object import convert_to_easypost_object
from easypost.errors import ExternalApiError
from easypost.models import User
from easypost.requestor import (
RequestMethod,
Expand Down Expand Up @@ -95,40 +90,6 @@ def get_next_page(

return self.all(**params)

def add_credit_card(
self,
referral_api_key: str,
number: str,
expiration_month: int,
expiration_year: int,
cvc: str,
priority: str = "primary",
) -> dict[str, Any]:
"""Add a credit card to EasyPost for a ReferralCustomer without needing a Stripe account.

This function requires the ReferralCustomer User's API key.
"""
easypost_stripe_api_key = self._retrieve_easypost_stripe_api_key()

try:
stripe_token = self._create_stripe_token(
number,
expiration_month,
expiration_year,
cvc,
easypost_stripe_api_key,
)
except Exception:
raise ExternalApiError(message=SEND_STRIPE_DETAILS_ERROR)

response = self._create_easypost_credit_card(
referral_api_key,
stripe_token.get("id", ""),
priority=priority,
)

return convert_to_easypost_object(response)

def add_credit_card_from_stripe(
self,
referral_api_key: str,
Expand Down Expand Up @@ -187,73 +148,11 @@ def add_bank_account_from_stripe(

return convert_to_easypost_object(response)

def _retrieve_easypost_stripe_api_key(self) -> str:
def retrieve_easypost_stripe_api_key(self) -> str:
"""Retrieve EasyPost's Stripe public API key."""
public_key = Requestor(self._client).request(
method=RequestMethod.GET,
url="/partners/stripe_public_key",
)

return public_key.get("public_key", "")

def _create_stripe_token(
self,
number: str,
expiration_month: int,
expiration_year: int,
cvc: str,
easypost_stripe_key: str,
) -> dict[str, Any]:
"""Get credit card token from Stripe."""
headers = {
# This Stripe endpoint only accepts URL form encoded bodies
"Content-type": "application/x-www-form-urlencoded",
}

credit_card_dict = {
"card": {
"number": number,
"exp_month": expiration_month,
"exp_year": expiration_year,
"cvc": cvc,
}
}

form_encoded_params = Requestor.form_encode_params(credit_card_dict)
url = "https://api.stripe.com/v1/tokens"

stripe_response = requests.post(
url,
params=form_encoded_params,
headers=headers,
auth=requests.auth.HTTPBasicAuth(easypost_stripe_key, ""),
timeout=TIMEOUT,
)

return stripe_response.json()

def _create_easypost_credit_card(
self,
referral_api_key: str,
stripe_object_id: str,
priority: str = "primary",
) -> dict[str, Any]:
"""Submit Stripe credit card token to EasyPost."""
params = {
"credit_card": {
"stripe_object_id": stripe_object_id,
"priority": priority,
}
}

# Override the API key to use the referral's for this single request
referral_client = deepcopy(self._client)
referral_client.api_key = referral_api_key

response = Requestor(referral_client).request(
method=RequestMethod.POST,
params=params,
url="/credit_cards",
)

return response
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ TEST_DIR := "tests"

# Build the project for release
build:
{{VIRTUAL_BIN}}/python -m build
uv build

# Clean the project
clean:
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ classifiers = [
dependencies = ["requests >= 2.4.3"]
optional-dependencies = { dev = [
"bandit == 1.8.*",
"build == 1.2.*",
"mypy == 1.15.*",
"pdoc == 15.*",
"pytest == 8.*",
Expand Down
212 changes: 0 additions & 212 deletions tests/cassettes/test_referral_customer_add_credit_card.yaml

This file was deleted.

Loading
Loading