1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/public/build/assets/wepay-credit-card-dbe97b58.js
2023-09-26 08:24:51 +10:00

10 lines
5.6 KiB
JavaScript
Vendored

/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/var r;let n=(r=document.querySelector('meta[name="wepay-action"]'))==null?void 0:r.content;class d{constructor(e="payment"){this.action=e,this.errors=document.getElementById("errors")}initializeWePay(){var t;let e=(t=document.querySelector('meta[name="wepay-environment"]'))==null?void 0:t.content;return WePay.set_endpoint(e==="staging"?"stage":"production"),this}validateCreditCardFields(){if(this.myCard=$("#my-card"),document.getElementById("cardholder_name")===""){document.getElementById("cardholder_name").focus(),this.errors.textContent="Cardholder name required.",this.errors.hidden=!1;return}else if(this.myCard.CardJs("cardNumber").replace(/[^\d]/g,"")===""){document.getElementById("card_number").focus(),this.errors.textContent="Card number required.",this.errors.hidden=!1;return}else if(this.myCard.CardJs("cvc").replace(/[^\d]/g,"")===""){document.getElementById("cvv").focus(),this.errors.textContent="CVV number required.",this.errors.hidden=!1;return}else if(this.myCard.CardJs("expiryMonth").replace(/[^\d]/g,"")===""){this.errors.textContent="Expiry Month number required.",this.errors.hidden=!1;return}else if(this.myCard.CardJs("expiryYear").replace(/[^\d]/g,"")===""){this.errors.textContent="Expiry Year number required.",this.errors.hidden=!1;return}return!0}handleAuthorization(){if(!this.validateCreditCardFields())return;let e=document.getElementById("card_button");e.disabled=!0,e.querySelector("svg").classList.remove("hidden"),e.querySelector("span").classList.add("hidden"),WePay.credit_card.create({client_id:document.querySelector("meta[name=wepay-client-id]").content,user_name:document.getElementById("cardholder_name").value,email:document.querySelector("meta[name=contact-email]").content,cc_number:this.myCard.CardJs("cardNumber").replace(/[^\d]/g,""),cvv:this.myCard.CardJs("cvc").replace(/[^\d]/g,""),expiration_month:this.myCard.CardJs("expiryMonth").replace(/[^\d]/g,""),expiration_year:this.myCard.CardJs("expiryYear").replace(/[^\d]/g,""),address:{country:document.querySelector(["meta[name=country_code"]).content,postal_code:document.querySelector(["meta[name=client-postal-code"]).content}},t=>{t.error?(e=document.getElementById("card_button"),e.disabled=!1,e.querySelector("svg").classList.add("hidden"),e.querySelector("span").classList.remove("hidden"),this.errors.textContent="",this.errors.textContent=t.error_description,this.errors.hidden=!1):(document.querySelector('input[name="credit_card_id"]').value=t.credit_card_id,document.getElementById("server_response").submit())})}completePaymentUsingToken(e){document.querySelector('input[name="credit_card_id"]').value=null,document.querySelector('input[name="token"]').value=e,document.getElementById("server-response").submit()}completePaymentWithoutToken(){if(!this.validateCreditCardFields()){this.payNowButton=document.getElementById("pay-now"),this.payNowButton.disabled=!1,this.payNowButton.querySelector("svg").classList.add("hidden"),this.payNowButton.querySelector("span").classList.remove("hidden");return}WePay.credit_card.create({client_id:document.querySelector("meta[name=wepay-client-id]").content,user_name:document.getElementById("cardholder_name").value,email:document.querySelector("meta[name=contact-email]").content,cc_number:this.myCard.CardJs("cardNumber").replace(/[^\d]/g,""),cvv:this.myCard.CardJs("cvc").replace(/[^\d]/g,""),expiration_month:this.myCard.CardJs("expiryMonth").replace(/[^\d]/g,""),expiration_year:this.myCard.CardJs("expiryYear").replace(/[^\d]/g,""),address:{country:document.querySelector(["meta[name=country_code"]).content,postal_code:document.querySelector(["meta[name=client-postal-code"]).content}},e=>{e.error?(this.payNowButton.disabled=!1,this.payNowButton.querySelector("svg").classList.add("hidden"),this.payNowButton.querySelector("span").classList.remove("hidden"),this.errors.textContent="",this.errors.textContent=e.error_description,this.errors.hidden=!1):(document.querySelector('input[name="credit_card_id"]').value=e.credit_card_id,document.querySelector('input[name="token"]').value=null,document.getElementById("server-response").submit())})}handle(){this.initializeWePay(),this.action==="authorize"?document.getElementById("card_button").addEventListener("click",()=>this.handleAuthorization()):this.action==="payment"&&(Array.from(document.getElementsByClassName("toggle-payment-with-token")).forEach(e=>e.addEventListener("click",t=>{document.getElementById("save-card--container").style.display="none",document.getElementById("wepay--credit-card-container").style.display="none",document.getElementById("token").value=t.target.dataset.token})),document.getElementById("toggle-payment-with-credit-card").addEventListener("click",e=>{document.getElementById("save-card--container").style.display="grid",document.getElementById("wepay--credit-card-container").style.display="flex",document.getElementById("token").value=null}),document.getElementById("pay-now").addEventListener("click",()=>{this.payNowButton=document.getElementById("pay-now"),this.payNowButton.disabled=!0,this.payNowButton.querySelector("svg").classList.remove("hidden"),this.payNowButton.querySelector("span").classList.add("hidden");let e=document.querySelector("input[name=token]"),t=document.querySelector("input[name=token-billing-checkbox]:checked");return t&&(document.getElementById("store_card").value=t.value),e.value?this.completePaymentUsingToken(e.value):this.completePaymentWithoutToken()}))}}new d(n).handle();