Map stripe idempotency errors to 400
This commit is contained in:
parent
a99ac14c6a
commit
cc7bb8b549
|
@ -343,9 +343,14 @@ public class SubscriptionController {
|
||||||
public record Error(SetSubscriptionLevelErrorResponse.Error.Type type, String message) {
|
public record Error(SetSubscriptionLevelErrorResponse.Error.Type type, String message) {
|
||||||
|
|
||||||
public enum Type {
|
public enum Type {
|
||||||
|
// The requested level was invalid
|
||||||
UNSUPPORTED_LEVEL,
|
UNSUPPORTED_LEVEL,
|
||||||
|
// The requested currency was invalid
|
||||||
UNSUPPORTED_CURRENCY,
|
UNSUPPORTED_CURRENCY,
|
||||||
|
// The card could not be charged
|
||||||
PAYMENT_REQUIRES_ACTION,
|
PAYMENT_REQUIRES_ACTION,
|
||||||
|
// The request arguments were invalid representing a programmer error
|
||||||
|
INVALID_ARGUMENTS
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -391,6 +396,12 @@ public class SubscriptionController {
|
||||||
SetSubscriptionLevelErrorResponse.Error.Type.PAYMENT_REQUIRES_ACTION, null))))
|
SetSubscriptionLevelErrorResponse.Error.Type.PAYMENT_REQUIRES_ACTION, null))))
|
||||||
.build());
|
.build());
|
||||||
}))
|
}))
|
||||||
|
.exceptionally(ExceptionUtils.exceptionallyHandler(SubscriptionException.InvalidArguments.class, e -> {
|
||||||
|
throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST)
|
||||||
|
.entity(new SetSubscriptionLevelErrorResponse(List.of(new SetSubscriptionLevelErrorResponse.Error(
|
||||||
|
SetSubscriptionLevelErrorResponse.Error.Type.INVALID_ARGUMENTS, e.getMessage()))))
|
||||||
|
.build());
|
||||||
|
}))
|
||||||
.thenApply(unused -> Response.ok(new SetSubscriptionLevelSuccessResponse(level)).build());
|
.thenApply(unused -> Response.ok(new SetSubscriptionLevelSuccessResponse(level)).build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import com.google.common.base.Strings;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.stripe.StripeClient;
|
import com.stripe.StripeClient;
|
||||||
import com.stripe.exception.CardException;
|
import com.stripe.exception.CardException;
|
||||||
|
import com.stripe.exception.IdempotencyException;
|
||||||
import com.stripe.exception.StripeException;
|
import com.stripe.exception.StripeException;
|
||||||
import com.stripe.model.Charge;
|
import com.stripe.model.Charge;
|
||||||
import com.stripe.model.Customer;
|
import com.stripe.model.Customer;
|
||||||
|
@ -292,14 +293,13 @@ public class StripeManager implements CustomerAwareSubscriptionPaymentProcessor
|
||||||
// create a subscription, we want to ensure only one gets created.
|
// create a subscription, we want to ensure only one gets created.
|
||||||
return stripeClient.subscriptions()
|
return stripeClient.subscriptions()
|
||||||
.create(params, commonOptions(generateIdempotencyKeyForCreateSubscription(
|
.create(params, commonOptions(generateIdempotencyKeyForCreateSubscription(
|
||||||
customerId, lastSubscriptionCreatedAt)));
|
customerId, lastSubscriptionCreatedAt)));
|
||||||
|
} catch (IdempotencyException e) {
|
||||||
|
throw ExceptionUtils.wrap(new SubscriptionException.InvalidArguments(e.getStripeError().getMessage()));
|
||||||
|
} catch (CardException e) {
|
||||||
|
throw new CompletionException(
|
||||||
|
new SubscriptionException.ProcessorException(getProvider(), createChargeFailureFromCardException(e)));
|
||||||
} catch (StripeException e) {
|
} catch (StripeException e) {
|
||||||
|
|
||||||
if (e instanceof CardException ce) {
|
|
||||||
throw new CompletionException(
|
|
||||||
new SubscriptionException.ProcessorException(getProvider(), createChargeFailureFromCardException(e, ce)));
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new CompletionException(e);
|
throw new CompletionException(e);
|
||||||
}
|
}
|
||||||
}, executor)
|
}, executor)
|
||||||
|
@ -347,15 +347,14 @@ public class StripeManager implements CustomerAwareSubscriptionPaymentProcessor
|
||||||
return stripeClient.subscriptions().update(subscription.getId(), params,
|
return stripeClient.subscriptions().update(subscription.getId(), params,
|
||||||
commonOptions(
|
commonOptions(
|
||||||
generateIdempotencyKeyForSubscriptionUpdate(subscription.getCustomer(), idempotencyKey)));
|
generateIdempotencyKeyForSubscriptionUpdate(subscription.getCustomer(), idempotencyKey)));
|
||||||
|
} catch (IdempotencyException e) {
|
||||||
|
throw ExceptionUtils.wrap(new SubscriptionException.InvalidArguments(e.getStripeError().getMessage()));
|
||||||
|
} catch (CardException e) {
|
||||||
|
throw ExceptionUtils.wrap(
|
||||||
|
new SubscriptionException.ProcessorException(getProvider(), createChargeFailureFromCardException(e)));
|
||||||
} catch (StripeException e) {
|
} catch (StripeException e) {
|
||||||
|
|
||||||
if (e instanceof CardException ce) {
|
|
||||||
throw ExceptionUtils.wrap(
|
|
||||||
new SubscriptionException.ProcessorException(getProvider(), createChargeFailureFromCardException(e, ce)));
|
|
||||||
}
|
|
||||||
throw ExceptionUtils.wrap(e);
|
throw ExceptionUtils.wrap(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}, executor)
|
}, executor)
|
||||||
.thenApply(subscription1 -> new SubscriptionId(subscription1.getId()));
|
.thenApply(subscription1 -> new SubscriptionId(subscription1.getId()));
|
||||||
}
|
}
|
||||||
|
@ -534,9 +533,9 @@ public class StripeManager implements CustomerAwareSubscriptionPaymentProcessor
|
||||||
outcome != null ? outcome.getType() : null);
|
outcome != null ? outcome.getType() : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ChargeFailure createChargeFailureFromCardException(StripeException e, CardException ce) {
|
private static ChargeFailure createChargeFailureFromCardException(CardException e) {
|
||||||
return new ChargeFailure(
|
return new ChargeFailure(
|
||||||
StringUtils.defaultIfBlank(ce.getDeclineCode(), ce.getCode()),
|
StringUtils.defaultIfBlank(e.getDeclineCode(), e.getCode()),
|
||||||
e.getStripeError().getMessage(),
|
e.getStripeError().getMessage(),
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
|
|
Loading…
Reference in New Issue