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 `addCreditCardToUser` 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 `referralCustomer.retrieveEasypostStripeApiKey` public to help facilitate adding credit cards using Stripe.js

## v8.8.0 (2026-06-25)

- Adds `params` to `requestPin` ensuring users can pass `easypost_details` to the call
Expand Down
149 changes: 1 addition & 148 deletions src/main/java/com/easypost/service/ReferralCustomerService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.easypost.service;

import com.easypost.Constants;
import com.easypost.EasyPost;
import com.easypost.exception.API.EncodingError;
import com.easypost.exception.API.ExternalApiError;
import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.EndOfPaginationError;
import com.easypost.http.Requestor;
Expand All @@ -12,15 +8,8 @@
import com.easypost.model.PaymentMethod;
import com.easypost.model.PaymentMethodObject;
import com.easypost.model.ReferralCustomer;
import com.easypost.utils.InternalUtilities;
import lombok.SneakyThrows;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -118,54 +107,6 @@
}, collection.getReferralCustomers(), pageSize);
}

/**
* Add credit card to a referral user. This function requires the Referral
* User's API key.
*
* @param referralApiKey API key of the referral user.
* @param number Credit card number.
* @param expirationMonth Expiration month of the credit card.
* @param expirationYear Expiration year of the credit card.
* @param cvc CVC of the credit card.
* @return PaymentMethodObject object.
* @throws EasyPostException when the request fails.
*/
public PaymentMethodObject addCreditCardToUser(final String referralApiKey, final String number,
final int expirationMonth, final int expirationYear,
final String cvc) throws EasyPostException {
return addCreditCardToUser(referralApiKey, number, expirationMonth, expirationYear, cvc,
PaymentMethod.Priority.PRIMARY);
}

/**
* Add credit card to a referral user. This function requires the Referral
* User's API key.
*
* @param referralApiKey API key of the referral user.
* @param number Credit card number.
* @param expirationMonth Expiration month of the credit card.
* @param expirationYear Expiration year of the credit card.
* @param cvc CVC of the credit card.
* @param priority Priority of this credit card.
* @return PaymentMethodObject object.
* @throws EasyPostException when the request fails.
*/
public PaymentMethodObject addCreditCardToUser(final String referralApiKey, final String number,
final int expirationMonth, final int expirationYear,
final String cvc, final PaymentMethod.Priority priority)
throws EasyPostException {
String easypostStripeApiKey = retrieveEasypostStripeApiKey();
String stripeToken;

try {
stripeToken = createStripeToken(number, expirationMonth, expirationYear, cvc, easypostStripeApiKey);
} catch (Exception e) {
throw new ExternalApiError(String.format(Constants.ErrorMessages.EXTERNAL_API_CALL_FAILED, "Stripe"));
}

return createEasypostCreditCard(referralApiKey, stripeToken, priority.toString().toLowerCase());
}

/**
* Add a credit card to EasyPost for a ReferralCustomer with a payment method ID from Stripe.
* This function requires the ReferralCustomer User's API key.
Expand All @@ -181,7 +122,7 @@
Map<String, Object> params = new HashMap<>();
Map<String, Object> creditCardParams = new HashMap<>();
creditCardParams.put("payment_method_id", paymentMethodId);
creditCardParams.put("priority", priority.toString().toLowerCase());

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / coverage

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (21)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (25)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (22)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (8)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (14)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (20)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (18)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (15)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (11)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (17)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (13)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (19)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (23)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (16)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (10)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (12)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (24)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 125 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (9)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)
params.put("credit_card", creditCardParams);

EasyPostClient referralClient = new EasyPostClient(referralApiKey);
Expand Down Expand Up @@ -210,7 +151,7 @@
Map<String, Object> params = new HashMap<>();
params.put("financial_connections_id", financialConnectionsId);
params.put("mandate_data", mandateData);
params.put("priority", priority.toString().toLowerCase());

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / coverage

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (21)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (25)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (22)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (8)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (14)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (20)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (18)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (15)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (11)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (17)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (13)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (19)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (23)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (16)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (10)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (12)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (24)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

Check warning on line 154 in src/main/java/com/easypost/service/ReferralCustomerService.java

View workflow job for this annotation

GitHub Actions / build (9)

[StringCaseLocaleUsage] Specify a `Locale` when calling `String#to{Lower,Upper}Case`. (Note: there are multiple suggested fixes; the third may be most appropriate if you're dealing with ASCII Strings.)

EasyPostClient referralClient = new EasyPostClient(referralApiKey);

Expand All @@ -225,100 +166,12 @@
* @return EasyPost Stripe API key.
* @throws EasyPostException when the request fails.
*/
private String retrieveEasypostStripeApiKey() throws EasyPostException {
public String retrieveEasypostStripeApiKey() throws EasyPostException {
String endpoint = "partners/stripe_public_key";

@SuppressWarnings ("unchecked") Map<String, String> response =
Requestor.request(RequestMethod.GET, endpoint, null, Map.class, client);

return response.getOrDefault("public_key", "");
}

/**
* Get credit card token from Stripe.
*
* @param number Credit card number.
* @param expirationMonth Expiration month of the credit card.
* @param expirationYear Expiration year of the credit card.
* @param cvc CVC of the credit card.
* @param easypostStripeApiKey EasyPost Stripe API key.
* @return Stripe token.
* @throws EncodingError when the request details could not be encoded.
* @throws IOException when the request fails.
*/
private static String createStripeToken(final String number, final int expirationMonth, final int expirationYear,
final String cvc, final String easypostStripeApiKey)
throws EncodingError, IOException {
String apiToken = String.format("%s %s", "Bearer", easypostStripeApiKey);

Map<String, String> params = new HashMap<>();
params.put("number", number);
params.put("exp_month", String.valueOf(expirationMonth));
params.put("exp_year", String.valueOf(expirationYear));
params.put("cvc", cvc);

String encodedURL = InternalUtilities.getEncodedURL(params, "card");
URL stripeUrl = new URL("https://api.stripe.com/v1/tokens?" + encodedURL);

HttpURLConnection conn;
if (EasyPost._vcrUrlFunction != null) {
conn = EasyPost._vcrUrlFunction.apply(stripeUrl.toString());
} else {
conn = (HttpURLConnection) stripeUrl.openConnection();
}

conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", apiToken);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoOutput(true);

StringBuilder response;

try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
String line;
response = new StringBuilder();

while ((line = br.readLine()) != null) {
response.append(line);
response.append(System.lineSeparator());
}
br.close();
} finally {
conn.disconnect();
}

String responseBody = response.toString();

@SuppressWarnings ("unchecked") Map<String, Object> responseMap =
Constants.Http.GSON.fromJson(responseBody, Map.class);

return responseMap.get("id").toString();
}

/**
* Submit Stripe credit card token to EasyPost.
*
* @param referralApiKey API key of the referral user.
* @param stripeObjectId Stripe token.
* @param priority Credit card priority.
* @return CreditCard object.
* @throws EasyPostException when the request fails.
*/
private PaymentMethodObject createEasypostCreditCard(final String referralApiKey, final String stripeObjectId,
final String priority) throws EasyPostException {
Map<String, Object> params = new HashMap<>();
params.put("stripe_object_id", stripeObjectId);
params.put("priority", priority);

Map<String, Object> wrappedParams = new HashMap<>();
wrappedParams.put("credit_card", params);

EasyPostClient referralClient = new EasyPostClient(referralApiKey);

String endpoint = "credit_cards";

return Requestor.request(RequestMethod.POST, endpoint, wrappedParams, PaymentMethodObject.class,
referralClient);
}
}
Loading
Loading