Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StripeException#getUserMessage on v1 API errors #1911

Merged
merged 2 commits into from
Oct 25, 2024

Conversation

jar-stripe
Copy link
Contributor

@jar-stripe jar-stripe commented Oct 25, 2024

Why?

This issue #1899 reports that CardException#getUserMessage() returns null after upgrading to SDK v27.1. The root cause is that the underlying StripeError#getUserMessage is only valid for v2 errors. Because it is not present on v1 errors, it will be null in the StripeError object. This PR fixes the issue by ensuring we only access the user message in StripeError for v2 errors.

What

  • Adds stripeErrorApiMode internal field to StripeException to store which API version returned the error
  • Adds setStripeV2Error method to set the error and set the stripeErrorApiMode to V2; modified setStripeError to set the stripeErrorApiMode to V1
  • Modifies StripeException getUserMessage to return error.getMessage() if we've wrapped a v1 error
  • Modifies StripeException getMessage to not include user message if we've wrapped a v1 error
  • Adds unit tests for setting v1 and v2 errors in StripeException

…he error correct according to the version of the API that returned it

added setStripeV2Error to StripeException, to let us set v2 errors wrapped in a StripeError
modified setStripeError to set the api mode to v1
updated getUserMessage to return stripeError.getMessage for v1 errors
added tests
@jar-stripe jar-stripe marked this pull request as ready for review October 25, 2024 21:58
@jar-stripe
Copy link
Contributor Author

I was able to reproduce the issue and then validate the fix with:

try {
  PaymentIntentCreateParams params =
      PaymentIntentCreateParams.builder()
          .setCurrency("usd")
          .setAmount(2000L)
          .setConfirm(true)
          .setAutomaticPaymentMethods(
              PaymentIntentCreateParams.AutomaticPaymentMethods.builder()
                  .setAllowRedirects(
                      PaymentIntentCreateParams.AutomaticPaymentMethods.AllowRedirects.NEVER)
                  .setEnabled(true)
                  .build())
          .setPaymentMethod("pm_card_chargeDeclinedProcessingError")
          .build();
  StripeClient client = new StripeClient(this.apiKey);
  PaymentIntent paymentIntent = client.paymentIntents().create(params);
  System.out.println("PaymentIntent created: " + paymentIntent);
} catch (StripeException e) {
  e.printStackTrace();
  System.out.println(e.getUserMessage());
}

@jar-stripe jar-stripe enabled auto-merge (squash) October 25, 2024 22:11
@jar-stripe jar-stripe merged commit 1f7295b into master Oct 25, 2024
12 checks passed
@jar-stripe jar-stripe deleted the jar/fix_error_user_message branch October 25, 2024 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants