From 4a8a2a70b51eb91884fafec8e21e1ed1aee82ac6 Mon Sep 17 00:00:00 2001 From: Ehren Kret Date: Fri, 3 Dec 2021 12:22:48 -0600 Subject: [PATCH] Return 400 instead of 500 when amount is too small --- .../textsecuregcm/stripe/StripeManager.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/stripe/StripeManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/stripe/StripeManager.java index 36293d062..7d1bfe6be 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/stripe/StripeManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/stripe/StripeManager.java @@ -44,6 +44,7 @@ import java.util.Collection; import java.util.Comparator; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; @@ -53,6 +54,9 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; import org.apache.commons.codec.binary.Hex; import org.whispersystems.textsecuregcm.util.Conversions; @@ -147,6 +151,9 @@ public class StripeManager { }, executor); } + /** + * Creates a payment intent. May throw a 400 WebApplicationException if the amount is too small. + */ public CompletableFuture createPaymentIntent(String currency, long amount) { return CompletableFuture.supplyAsync(() -> { PaymentIntentCreateParams params = PaymentIntentCreateParams.builder() @@ -157,7 +164,14 @@ public class StripeManager { try { return PaymentIntent.create(params, commonOptions()); } catch (StripeException e) { - throw new CompletionException(e); + if ("amount_too_small".equalsIgnoreCase(e.getCode())) { + throw new WebApplicationException(Response + .status(Status.BAD_REQUEST) + .entity(Map.of("error", "amount_too_small")) + .build()); + } else { + throw new CompletionException(e); + } } }, executor); }