1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-24 10:21:35 +02:00
invoiceninja/public/build/assets/square-credit-card-a20464a3.js
2023-09-26 08:24:51 +10:00

10 lines
3.5 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
*/class c{constructor(){this.appId=document.querySelector("meta[name=square-appId]").content,this.locationId=document.querySelector("meta[name=square-locationId]").content,this.isLoaded=!1}async init(){this.payments=Square.payments(this.appId,this.locationId),this.card=await this.payments.card(),await this.card.attach("#card-container"),this.isLoaded=!0;let e=document.querySelector(".sq-card-iframe-container");e&&e.setAttribute("style","150px !important"),document.querySelector(".toggle-payment-with-token")&&document.getElementById("card-container").classList.add("hidden")}async completePaymentWithoutToken(e){document.getElementById("errors").hidden=!0,e.target.parentElement.disabled=!0;let t=await this.card.tokenize(),o;try{const n={amount:document.querySelector("meta[name=amount]").content,billingContact:JSON.parse(document.querySelector("meta[name=square_contact]").content),currencyCode:document.querySelector("meta[name=currencyCode]").content,intent:"CHARGE"};o=(await this.payments.verifyBuyer(t.token,n)).token}catch{e.target.parentElement.disabled=!0}if(document.querySelector('input[name="verificationToken"]').value=o,t.status==="OK"){document.getElementById("sourceId").value=t.token;let n=document.querySelector('input[name="token-billing-checkbox"]:checked');return n&&(document.querySelector('input[name="store_card"]').value=n.value),document.getElementById("server_response").submit()}document.getElementById("errors").textContent=t.errors[0].message,document.getElementById("errors").hidden=!1,e.target.parentElement.disabled=!1}async completePaymentUsingToken(e){return e.target.parentElement.disabled=!0,document.getElementById("server_response").submit()}async verifyBuyer(e){const t={amount:document.querySelector("meta[name=amount]").content,billingContact:document.querySelector("meta[name=square_contact]").content,currencyCode:document.querySelector("meta[name=currencyCode]").content,intent:"CHARGE"};return(await this.payments.verifyBuyer(e,t)).token}async handle(){document.getElementById("payment-list").classList.add("hidden"),await this.init().then(()=>{var e,t,o,n;(e=document.getElementById("authorize-card"))==null||e.addEventListener("click",a=>this.completePaymentWithoutToken(a)),(t=document.getElementById("pay-now"))==null||t.addEventListener("click",a=>document.querySelector("input[name=token]").value?this.completePaymentUsingToken(a):this.completePaymentWithoutToken(a)),Array.from(document.getElementsByClassName("toggle-payment-with-token")).forEach(a=>a.addEventListener("click",async r=>{document.getElementById("card-container").classList.add("hidden"),document.getElementById("save-card--container").style.display="none",document.querySelector("input[name=token]").value=r.target.dataset.token})),(o=document.getElementById("toggle-payment-with-credit-card"))==null||o.addEventListener("click",async a=>{document.getElementById("card-container").classList.remove("hidden"),document.getElementById("save-card--container").style.display="grid",document.querySelector("input[name=token]").value=""}),document.getElementById("loader").classList.add("hidden"),document.getElementById("payment-list").classList.remove("hidden"),(n=document.getElementById("toggle-payment-with-credit-card"))==null||n.click()})}}new c().handle();