From e413999ba42cbb42ed63ba031f612748c68a4536 Mon Sep 17 00:00:00 2001 From: Takayuki Miyoshi Date: Mon, 30 Aug 2021 17:29:45 +0900 Subject: [PATCH] Activate irritating submission mode if stolen card is detected #465 #533 --- modules/stripe/index.js | 2 +- modules/stripe/src/index.js | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/modules/stripe/index.js b/modules/stripe/index.js index 6d206a32..1b2389c3 100644 --- a/modules/stripe/index.js +++ b/modules/stripe/index.js @@ -1 +1 @@ -!function(e){var t={};function r(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)r.d(n,i,function(t){return e[t]}.bind(null,i));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){document.addEventListener("DOMContentLoaded",e=>{if(void 0===window.wpcf7_stripe)return void console.error("window.wpcf7_stripe is not defined.");if("function"!=typeof window.Stripe)return void console.error("window.Stripe is not defined.");if("function"!=typeof wpcf7.submit)return void console.error("wpcf7.submit is not defined.");const t=Stripe(wpcf7_stripe.publishable_key),r=t.elements();document.addEventListener("wpcf7submit",e=>{const n=e.detail.unitTag,i=n+"-ve-stripe-card-element",o=document.querySelector(`#${n} form`),s=o.closest(".wpcf7").querySelector(".screen-reader-response"),c=o.querySelector(".wpcf7-stripe .wpcf7-form-control-wrap"),d=o.querySelector(".wpcf7-stripe button.first"),a=o.querySelector(".wpcf7-stripe button.second"),l=o.querySelector('[name="_wpcf7_stripe_payment_intent"]');if(!l)return;l.setAttribute("value","");const u=e=>{const t=s.querySelector("ul"),r=t.querySelector("li#"+i);r&&r.remove();const n=document.createElement("li");n.setAttribute("id",i),n.insertAdjacentText("beforeend",e.message),t.appendChild(n)},f=e=>{const t=c.querySelector(".wpcf7-form-control");t.classList.add("wpcf7-not-valid"),t.setAttribute("aria-describedby",i);const r=document.createElement("span");r.setAttribute("class","wpcf7-not-valid-tip"),r.setAttribute("aria-hidden","true"),r.insertAdjacentText("beforeend",e.message),c.appendChild(r),c.querySelectorAll("[aria-invalid]").forEach(e=>{e.setAttribute("aria-invalid","true")}),t.closest(".use-floating-validation-tip")&&(t.addEventListener("focus",e=>{r.setAttribute("style","display: none")}),r.addEventListener("mouseover",e=>{r.setAttribute("style","display: none")}))},p=()=>{s.querySelectorAll("ul li#"+i).forEach(e=>{e.remove()}),c.querySelectorAll(".wpcf7-not-valid-tip").forEach(e=>{e.remove()}),c.querySelectorAll("[aria-invalid]").forEach(e=>{e.setAttribute("aria-invalid","false")}),c.querySelectorAll(".wpcf7-form-control").forEach(e=>{e.removeAttribute("aria-describedby"),e.classList.remove("wpcf7-not-valid")})};if("payment_required"===e.detail.status){const i=e.detail.apiResponse.stripe.payment_intent;i.id&&l.setAttribute("value",i.id);const s=r.getElement("card")||r.create("card");s.mount(`#${n} .wpcf7-stripe .card-element`),s.clear(),c.classList.remove("hidden"),d.classList.add("hidden"),a.classList.remove("hidden"),a.disabled=!0,s.addEventListener("change",e=>{if(p(),e.error){const t={message:e.error.message};u(t),f(t),a.disabled=!0}else a.disabled=!1}),a.addEventListener("click",e=>{a.disabled=!0,o.classList.add("submitting"),t.confirmCardPayment(i.client_secret,{payment_method:{card:s}}).then(e=>{if(p(),e.error){o.classList.remove("submitting");const t={message:e.error.message};u(t),f(t)}else"succeeded"===e.paymentIntent.status&&wpcf7.submit(o)})})}else c.classList.add("hidden"),d.classList.remove("hidden"),a.classList.add("hidden")})})}]); \ No newline at end of file +!function(e){var t={};function r(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)r.d(n,i,function(t){return e[t]}.bind(null,i));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t){document.addEventListener("DOMContentLoaded",e=>{if(void 0===window.wpcf7_stripe)return void console.error("window.wpcf7_stripe is not defined.");if("function"!=typeof window.Stripe)return void console.error("window.Stripe is not defined.");if("function"!=typeof wpcf7.submit)return void console.error("wpcf7.submit is not defined.");const t=Stripe(wpcf7_stripe.publishable_key),r=t.elements();document.addEventListener("wpcf7submit",e=>{const n=e.detail.unitTag,i=n+"-ve-stripe-card-element",o=document.querySelector(`#${n} form`),s=o.closest(".wpcf7").querySelector(".screen-reader-response"),c=o.querySelector(".wpcf7-stripe .wpcf7-form-control-wrap"),d=o.querySelector(".wpcf7-stripe button.first"),a=o.querySelector(".wpcf7-stripe button.second"),l=o.querySelector('[name="_wpcf7_stripe_payment_intent"]');if(!l)return;l.setAttribute("value","");const u=e=>{const t=s.querySelector("ul"),r=t.querySelector("li#"+i);r&&r.remove();const n=document.createElement("li");n.setAttribute("id",i),n.insertAdjacentText("beforeend",e.message),t.appendChild(n)},f=e=>{const t=c.querySelector(".wpcf7-form-control");t.classList.add("wpcf7-not-valid"),t.setAttribute("aria-describedby",i);const r=document.createElement("span");r.setAttribute("class","wpcf7-not-valid-tip"),r.setAttribute("aria-hidden","true"),r.insertAdjacentText("beforeend",e.message),c.appendChild(r),c.querySelectorAll("[aria-invalid]").forEach(e=>{e.setAttribute("aria-invalid","true")}),t.closest(".use-floating-validation-tip")&&(t.addEventListener("focus",e=>{r.setAttribute("style","display: none")}),r.addEventListener("mouseover",e=>{r.setAttribute("style","display: none")}))},p=()=>{s.querySelectorAll("ul li#"+i).forEach(e=>{e.remove()}),c.querySelectorAll(".wpcf7-not-valid-tip").forEach(e=>{e.remove()}),c.querySelectorAll("[aria-invalid]").forEach(e=>{e.setAttribute("aria-invalid","false")}),c.querySelectorAll(".wpcf7-form-control").forEach(e=>{e.removeAttribute("aria-describedby"),e.classList.remove("wpcf7-not-valid")})};if("payment_required"===e.detail.status){const i=e.detail.apiResponse.stripe.payment_intent;i.id&&l.setAttribute("value",i.id);const s=r.getElement("card")||r.create("card");s.mount(`#${n} .wpcf7-stripe .card-element`),s.clear(),c.classList.remove("hidden"),d.classList.add("hidden"),a.classList.remove("hidden"),a.disabled=!0,s.addEventListener("change",e=>{if(p(),e.error){const t={message:e.error.message};u(t),f(t),a.disabled=!0}else a.disabled=!1}),a.addEventListener("click",e=>{p(),a.disabled=!0,o.classList.add("submitting"),wpcf7.blocked||t.confirmCardPayment(i.client_secret,{payment_method:{card:s}}).then(e=>{if(e.error){e.error.decline_code&&["fraudulent","lost_card","merchant_blacklist","pickup_card","restricted_card","security_violation","service_not_allowed","stolen_card","transaction_not_allowed"].includes(e.error.decline_code)&&(wpcf7.blocked=!0),o.classList.remove("submitting");const t={message:e.error.message};u(t),f(t)}else"succeeded"===e.paymentIntent.status&&wpcf7.submit(o)})})}else c.classList.add("hidden"),d.classList.remove("hidden"),a.classList.add("hidden")})})}]); \ No newline at end of file diff --git a/modules/stripe/src/index.js b/modules/stripe/src/index.js index a06461aa..ed0c778b 100644 --- a/modules/stripe/src/index.js +++ b/modules/stripe/src/index.js @@ -141,17 +141,40 @@ document.addEventListener( 'DOMContentLoaded', event => { } ); button2.addEventListener( 'click', event => { + clearValidationErrors(); button2.disabled = true; form.classList.add( 'submitting' ); + if ( wpcf7.blocked ) { + return; + } + stripe.confirmCardPayment( paymentIntent.client_secret, { payment_method: { card: card, } } ).then( result => { - clearValidationErrors(); - if ( result.error ) { + if ( result.error.decline_code ) { + + // See https://stripe.com/docs/declines/codes + const redcardReasons = [ + 'fraudulent', + 'lost_card', + 'merchant_blacklist', + 'pickup_card', + 'restricted_card', + 'security_violation', + 'service_not_allowed', + 'stolen_card', + 'transaction_not_allowed', + ]; + + if ( redcardReasons.includes( result.error.decline_code ) ) { + wpcf7.blocked = true; + } + } + form.classList.remove( 'submitting' ); const error = {