Simplify stripe selected check

This commit is contained in:
Matt-Yorkley
2021-12-20 15:21:13 +00:00
parent ad24351bb3
commit 24c051bad3

View File

@@ -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
}
}