1 line
4.3 KiB
JavaScript
1 line
4.3 KiB
JavaScript
let _stripe,stripeElement,card;const stripeElements=function(e){_stripe=Stripe(e);const t=_stripe.elements();card=t.create("card",{hidePostalCode:!0}),wp.hooks.addFilter("wu_before_form_submitted","nextpress/wp-ultimo",(function(e,t,r){const o=document.getElementById("card-element");return"stripe"===r&&t.order.totals.total>0&&o&&o.offsetParent&&e.push(new Promise(async(e,t)=>{try{const e=await _stripe.createPaymentMethod({type:"card",card:card});e.error&&t(e.error)}catch(e){}e()})),e})),wp.hooks.addAction("wu_on_form_success","nextpress/wp-ultimo",(function(e,t){"stripe"===e.gateway&&(e.order.totals.total>0||e.order.totals.recurring.total>0)&&(e.set_prevent_submission(!1),handlePayment(e,t,card))})),wp.hooks.addAction("wu_on_form_updated","nextpress/wp-ultimo",(function(e){if("stripe"===e.gateway)try{card.mount("#card-element"),wu_stripe_update_styles(card,"#field-payment_template"),e.set_prevent_submission(e.order&&e.order.should_collect_payment&&"add-new"===e.payment_method)}catch(e){}else{e.set_prevent_submission(!1);try{card.unmount("#card-element")}catch(e){}}})),card.on("focus",(function(){document.getElementById("card-element").classList.add("focused")})),card.on("blur",(function(){document.getElementById("card-element").classList.remove("focused")}))};function wu_stripe_update_styles(e,t){void 0===typeof t&&(t="#field-payment_template");const r=document.querySelector(t);if(null===r)return;if(document.getElementById("wu-stripe-styles"))return;const o=window.getComputedStyle(r),d=document.createElement("style");d.innerHTML=".StripeElement {background-color:"+o.getPropertyValue("background-color")+";border-top-color:"+o.getPropertyValue("border-top-color")+";border-right-color:"+o.getPropertyValue("border-right-color")+";border-bottom-color:"+o.getPropertyValue("border-bottom-color")+";border-left-color:"+o.getPropertyValue("border-left-color")+";border-top-width:"+o.getPropertyValue("border-top-width")+";border-right-width:"+o.getPropertyValue("border-right-width")+";border-bottom-width:"+o.getPropertyValue("border-bottom-width")+";border-left-width:"+o.getPropertyValue("border-left-width")+";border-top-style:"+o.getPropertyValue("border-top-style")+";border-right-style:"+o.getPropertyValue("border-right-style")+";border-bottom-style:"+o.getPropertyValue("border-bottom-style")+";border-left-style:"+o.getPropertyValue("border-left-style")+";border-top-left-radius:"+o.getPropertyValue("border-top-left-radius")+";border-top-right-radius:"+o.getPropertyValue("border-top-right-radius")+";border-bottom-left-radius:"+o.getPropertyValue("border-bottom-left-radius")+";border-bottom-right-radius:"+o.getPropertyValue("border-bottom-right-radius")+";padding-top:"+o.getPropertyValue("padding-top")+";padding-right:"+o.getPropertyValue("padding-right")+";padding-bottom:"+o.getPropertyValue("padding-bottom")+";padding-left:"+o.getPropertyValue("padding-left")+";line-height:"+o.getPropertyValue("height")+";height:"+o.getPropertyValue("height")+";display: flex;\n flex-direction: column;\n justify-content: center;}",d.id="wu-stripe-styles",document.body.appendChild(d),e.update({style:{base:{color:o.getPropertyValue("color"),fontFamily:o.getPropertyValue("font-family"),fontSize:o.getPropertyValue("font-size"),fontWeight:o.getPropertyValue("font-weight"),fontSmoothing:o.getPropertyValue("-webkit-font-smoothing")}}})}function wu_stripe_handle_intent(e,t,r){const o=function(e){wu_checkout_form.unblock(),e.error&&wu_checkout_form.errors.push(e.error)};try{_stripe[e](t,r).then((function(e){e.error?o(e):wu_checkout_form.resubmit()}),o)}catch(e){}}function handlePayment(e,t,r){if(!t.gateway.data.stripe_client_secret)return;const o="payment_intent"===t.gateway.data.stripe_intent_type?"confirmCardPayment":"confirmCardSetup",d={payment_method:"add-new"!==e.payment_method?e.payment_method:{card:r,billing_details:{name:t.customer.display_name,email:t.customer.user_email,address:{country:t.customer.billing_address_data.billing_country,postal_code:t.customer.billing_address_data.billing_zip_code}}}};wu_stripe_handle_intent(o,t.gateway.data.stripe_client_secret,d)}wp.hooks.addFilter("wu_before_form_init","nextpress/wp-ultimo",(function(e){return e.add_new_card=wu_stripe.add_new_card,e.payment_method=wu_stripe.payment_method,e})),wp.hooks.addAction("wu_checkout_loaded","nextpress/wp-ultimo",(function(){stripeElement=stripeElements(wu_stripe.pk_key)})); |