diff --git a/imports/plugins/included/payments-stripe/client/checkout/stripe.js b/imports/plugins/included/payments-stripe/client/checkout/stripe.js index 6acb54b3913..b355c3f4b2e 100644 --- a/imports/plugins/included/payments-stripe/client/checkout/stripe.js +++ b/imports/plugins/included/payments-stripe/client/checkout/stripe.js @@ -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); } } diff --git a/imports/plugins/included/payments-stripe/lib/api/stripe.js b/imports/plugins/included/payments-stripe/lib/api/stripe.js index ee71b23c21a..b2529efe444 100644 --- a/imports/plugins/included/payments-stripe/lib/api/stripe.js +++ b/imports/plugins/included/payments-stripe/lib/api/stripe.js @@ -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); + }); } }; diff --git a/imports/plugins/included/payments-stripe/server/methods/stripe.js b/imports/plugins/included/payments-stripe/server/methods/stripe.js index 104c72b2077..170a637a220 100644 --- a/imports/plugins/included/payments-stripe/server/methods/stripe.js +++ b/imports/plugins/included/payments-stripe/server/methods/stripe.js @@ -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; diff --git a/imports/plugins/included/payments-stripe/server/methods/stripeapi-methods-charge.app-test.js b/imports/plugins/included/payments-stripe/server/methods/stripeapi-methods-charge.app-test.js index 17498ca295b..9f8d587d3f9 100644 --- a/imports/plugins/included/payments-stripe/server/methods/stripeapi-methods-charge.app-test.js +++ b/imports/plugins/included/payments-stripe/server/methods/stripeapi-methods-charge.app-test.js @@ -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, @@ -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, diff --git a/imports/plugins/included/payments-stripe/server/methods/stripeapi.js b/imports/plugins/included/payments-stripe/server/methods/stripeapi.js index 74a72a559d1..42e795a1aa9 100644 --- a/imports/plugins/included/payments-stripe/server/methods/stripeapi.js +++ b/imports/plugins/included/payments-stripe/server/methods/stripeapi.js @@ -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 }; } } });