Skip to content

Commit

Permalink
Merge pull request #1051 from amihaiemil/1049
Browse files Browse the repository at this point in the history
#1049 Better messages for WalletPaymentException
  • Loading branch information
amihaiemil authored Mar 10, 2021
2 parents 036b347 + b4710bf commit 998fd7f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
* Wallet decorator which perform all the required pre-checks before making
Expand Down Expand Up @@ -78,23 +77,18 @@ public Payment pay(final Invoice invoice) {
LOG.error("[Payment-PreCheck] Invoice already paid.");
throw new InvoiceException.AlreadyPaid(invoice);
}
if (invoice.totalAmount().longValueExact() < 108 * 100) {
final BigDecimal totalAmount = invoice.totalAmount();
if (totalAmount.longValueExact() < 108 * 100) {
LOG.error("[Payment-PreCheck] In order to be paid, Invoice amount"
+ " must be at least 108 €.");
throw new WalletPaymentException("In order to be paid, Invoice"
+ " amount must be at least 108 €.");
}
final BigDecimal newLimit = this.cash().subtract(
invoice.totalAmount()
);
final BigDecimal newLimit = this.cash().subtract(totalAmount);
if (newLimit.longValue() < 0L) {
LOG.error("[Payment-PreCheck] Not enough cash to pay Invoice.");
throw new WalletPaymentException(
"Invoice value exceeds wallet limit. "
+ "Please increase the limit of your wallet with "
+ "at least " + newLimit.abs().add(invoice.totalAmount())
.divide(BigDecimal.valueOf(100), RoundingMode.HALF_UP)
+ " €."
"Invoice value exceeds the limit of the project's wallet."
);
}
return this.original.pay(invoice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,8 @@ public Payment pay(final Invoice invoice) {
+ "PayoutMethod set up, cannot pay."
);
throw new WalletPaymentException(
"Contributor " + contributor.username()
+ " hasn't finished setting up their Stripe account yet. "
+ "We cannot make the payment yet."
"Contributor " + contributor.username() + " hasn't "
+ "finished setting up their Stripe PayoutMethod."
);
}
final PaymentMethod paymentMethod = this.storage
Expand All @@ -194,10 +193,7 @@ public Payment pay(final Invoice invoice) {
+ "Cannot make payment."
);
throw new WalletPaymentException(
"No active payment method for wallet #"
+ this.identifier + " of project "
+ this.project.repoFullName() + "/"
+ this.project.provider()
"The project's wallet has no active card."
);
}

Expand Down Expand Up @@ -273,8 +269,8 @@ public Payment pay(final Invoice invoice) {
paymentIntent.cancel();
LOG.error("[STRIPE] PaymentIntent successfully cancelled.");
throw new WalletPaymentException(
"Could not pay invoice #" + invoice.invoiceId() + " due to"
+ " Stripe payment intent status \"" + status + "\""
"Stripe payment intent status \"" + status + "\". "
+ "Payment intent cancelled. "
);
}
} catch (final StripeException ex) {
Expand All @@ -292,15 +288,14 @@ public Payment pay(final Invoice invoice) {
);
if("authentication_required".equalsIgnoreCase(code)) {
throw new WalletPaymentException(
"Payment cannot be made because the card "
+ "requires authentication."
"The card requires authentication."
);
}
if("card_declined".equalsIgnoreCase(code)) {
final String message = ex.getMessage();
throw new WalletPaymentException(
"Payment cannot be made: "
+ message.substring(0, message.indexOf(';'))
"Stripe message: "
+ message.substring(0, message.indexOf(';')) + ". "
);
}
throw new IllegalStateException(
Expand All @@ -317,7 +312,7 @@ public Payment pay(final Invoice invoice) {
private void ensureApiToken() {
if (this.stripeApiToken == null
|| this.stripeApiToken.trim().isEmpty()) {
throw new WalletPaymentException(
throw new IllegalStateException(
"Please specify the "
+ Env.STRIPE_API_TOKEN
+ " Environment Variable!"
Expand Down

0 comments on commit 998fd7f

Please sign in to comment.