From 24c051bad339aae40043f69ce386ea0ddca80d99 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 20 Dec 2021 15:21:13 +0000 Subject: [PATCH] Simplify stripe selected check --- .../controllers/stripe_controller.js | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/app/webpacker/controllers/stripe_controller.js b/app/webpacker/controllers/stripe_controller.js index bd601a55cc..35e1d7b29a 100644 --- a/app/webpacker/controllers/stripe_controller.js +++ b/app/webpacker/controllers/stripe_controller.js @@ -43,35 +43,29 @@ export default class extends Controller { // Before the form is submitted we send the card details directly to Stripe (via StripeJS), // and receive a token which represents the card object, and add that token into the form. form.addEventListener("submit", event => { - if (this.isVisible(stripeElementsForm)) { - event.preventDefault(); - event.stopPropagation(); - - stripe.createPaymentMethod({type: "card", card: stripe_element}).then(response => { - if (response.error) { - error_container.textContent = response.error.message; - } else { - pm_id_field.setAttribute("value", response.paymentMethod.id); - exp_month_field.setAttribute("value", response.paymentMethod.card.exp_month); - exp_year_field.setAttribute("value", response.paymentMethod.card.exp_year); - brand_field.setAttribute("value", response.paymentMethod.card.brand); - last4_field.setAttribute("value", response.paymentMethod.card.last4); + if(!this.stripeSelected()) { return } - form.submit(); - } - }); - } + event.preventDefault(); + event.stopPropagation(); + + stripe.createPaymentMethod({type: "card", card: stripe_element}).then(response => { + if (response.error) { + error_container.textContent = response.error.message; + } else { + pm_id_field.setAttribute("value", response.paymentMethod.id); + exp_month_field.setAttribute("value", response.paymentMethod.card.exp_month); + exp_year_field.setAttribute("value", response.paymentMethod.card.exp_year); + brand_field.setAttribute("value", response.paymentMethod.card.brand); + last4_field.setAttribute("value", response.paymentMethod.card.last4); + + form.submit(); + } + }); }); } - isVisible(element) { - while (element) { - if (window.getComputedStyle(element).display === "none") { - return false; - } - element = element.parentElement; - } - return true; + // Boolean; true if Stripe is shown / currently selected + stripeSelected() { + return !!this.cardElementTarget.offsetParent } - }