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

Show proper error messages when Stripe payments fail #2499

Merged
merged 10 commits into from
Jun 28, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ function hidePaymentAlert() {
}

function handleStripeSubmitError(error) {
const singleError = error;
const serverError = error ? error.message : null;
if (serverError) {
return paymentAlert("Oops! Credit card is invalid. Please check your information and try again.");
} else if (singleError) {
return paymentAlert("Oops! " + singleError);
// Match eror on card number. Not submitted to stripe
if (error && error.reason && error.reason === "Match failed") {
const message = "Your card number is invalid. Please check the number and try again";
return paymentAlert(message);
}

// this is a server message with a client-sanitized message
if (error && error.details) {
return paymentAlert(error.details);
}
}

Expand Down
5 changes: 3 additions & 2 deletions imports/plugins/included/payments-stripe/lib/api/stripe.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint camelcase: 0 */
import { Meteor } from "meteor/meteor";

export const Stripe = {
authorize: function (cardData, paymentInfo, callback) {
Meteor.call("stripeSubmit", "authorize", cardData, paymentInfo, callback);
Meteor.call("stripeSubmit", "authorize", cardData, paymentInfo, (error, result) => {
callback(error, result);
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ Meteor.methods({

try {
chargeResult = StripeApi.methods.createCharge.call({ chargeObj });
if (chargeResult && chargeResult.status === "succeeded") {
if (chargeResult && chargeResult.status && chargeResult.status === "succeeded") {
result = {
saved: true,
response: chargeResult
};
} else {
Logger.debug("Stripe Call succeeded but charge failed");
Logger.error("Stripe Call succeeded but charge failed");
result = {
saved: false,
error: chargeResult.error.message
error: chargeResult.error
};
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe("Stripe.authorize", function () {

expect(chargeResult).to.not.be.undefined;
expect(chargeResult.saved).to.be.false;
expect(chargeResult.error).to.equal("Your card was declined.");
expect(chargeResult.error.message).to.equal("Your card was declined.");
expect(StripeApi.methods.createCharge.call).to.have.been.calledWith({
chargeObj: {
amount: 2298,
Expand Down Expand Up @@ -260,7 +260,7 @@ describe("Stripe.authorize", function () {
chargeResult = result;
expect(chargeResult).to.not.be.undefined;
expect(chargeResult.saved).to.be.false;
expect(chargeResult.error).to.equal("Your card has expired.");
expect(chargeResult.error.message).to.equal("Your card has expired.");
expect(StripeApi.methods.createCharge.call).to.have.been.calledWith({
chargeObj: {
amount: 2298,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,19 @@ StripeApi.methods.createCharge = new ValidatedMethod({
// Handle "expected" errors differently
if (e.rawType === "card_error" && _.includes(expectedErrors, e.code)) {
Logger.debug("Error from Stripe is expected, not throwing");
return { error: e, result: null };
const normalizedError = {
details: e.message
};
return { error: normalizedError, result: null };
}
Logger.error("Received unexpected error code: " + e.code);
Logger.error("Received unexpected error type: " + e.rawType);
Logger.error(e);
return { error: e, result: null };

// send raw error to server log, but sanitized version to client
const sanitisedError = {
details: "An unexpected error has occurred"
};
return { error: sanitisedError, result: null };
}
}
});
Expand Down