FileMaster
Search
Toggle Dark Mode
Home
/
.
/
wp-content
/
plugins
/
surecart
/
dist
/
components
/
components
Edit File: paypal-buttons.js
import{proxyCustomElement,HTMLElement,createEvent,h}from"@stencil/core/internal/client";import{l as loadScript,g as getScriptLoadParams}from"./functions.js";import{a as apiFetch}from"./fetch.js";import{g as fetchCheckout}from"./index4.js";import{c as createErrorNotice}from"./mutations3.js";import{d as defineCustomElement$1}from"./sc-skeleton2.js";const paypalButtonsCss=':host{display:block}.paypal-buttons{position:relative;line-height:0;text-align:center}.paypal-buttons:not(.paypal-buttons--busy):after{content:" ";border-bottom:1px solid var(--sc-input-border-color);width:100%;height:0;top:50%;left:0;right:0;position:absolute}',ScPaypalButtonsStyle0=paypalButtonsCss,ScPaypalButtons=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.scSetState=createEvent(this,"scSetState",7),this.scPaid=createEvent(this,"scPaid",7),this.clientId=void 0,this.busy=!1,this.merchantId=void 0,this.merchantInitiated=void 0,this.mode=void 0,this.order=void 0,this.buttons=["paypal","card"],this.label="paypal",this.color="gold",this.loaded=void 0}handleOrderChange(t,e){(null==t?void 0:t.updated_at)!==(null==e?void 0:e.updated_at)&&(this.cardContainer.innerHTML="",this.paypalContainer.innerHTML="",this.loadScript())}async loadScript(){var t,e;if(this.clientId&&this.merchantId)try{const a=await loadScript(getScriptLoadParams({clientId:this.clientId,merchantId:this.merchantId,merchantInitiated:this.merchantInitiated,reusable:null===(t=this.order)||void 0===t?void 0:t.reusable_payment_method_required,currency:null===(e=this.order)||void 0===e?void 0:e.currency}));this.renderButtons(a)}catch(t){console.error("failed to load the PayPal JS SDK script",t)}}componentDidLoad(){this.loadScript()}renderButtons(t){const e={onClick:async(t,e)=>{const a=this.el.closest("sc-checkout");return await a.validate()?e.resolve():e.reject()},onInit:()=>{this.loaded=!0},onCancel:()=>{this.scSetState.emit("REJECT")},onApprove:async()=>{var t,e,a,s;try{this.order=await fetchCheckout({id:null===(t=this.order)||void 0===t?void 0:t.id})}catch(t){console.error(t),createErrorNotice({code:"could_not_capture",message:wp.i18n.__("The payment did not process. Please try again.","surecart")}),this.scSetState.emit("REJECT")}try{this.scSetState.emit("PAYING");const t=await apiFetch({method:"PATCH",path:`surecart/v1/payment_intents/${(null===(a=null===(e=this.order)||void 0===e?void 0:e.payment_intent)||void 0===a?void 0:a.id)||(null===(s=this.order)||void 0===s?void 0:s.payment_intent)}/capture`});["succeeded","processing"].includes(null==t?void 0:t.status)?(this.scSetState.emit("PAID"),this.scPaid.emit()):(createErrorNotice({code:"could_not_capture",message:wp.i18n.__("Payment processing failed. Kindly attempt the transaction once more.","surecart")}),this.scSetState.emit("REJECT"))}catch(t){console.error(t),createErrorNotice({code:"could_not_capture",message:wp.i18n.__("Payment processing failed. Kindly attempt the transaction once more.","surecart")}),this.scSetState.emit("REJECT")}},onError:t=>{console.error(t),createErrorNotice(t),this.scSetState.emit("REJECT")}};if(e[this.order.reusable_payment_method_required?"createBillingAgreement":"createOrder"]=async()=>new Promise((async(t,e)=>{var a,s;const i=this.el.closest("sc-checkout"),n=await i.submit();return n instanceof Error?e(n):"finalized"!==(null==n?void 0:n.status)?e(new Error("Something went wrong. Please try again.")):(null===(a=null==n?void 0:n.payment_intent)||void 0===a?void 0:a.external_intent_id)?t(null===(s=null==n?void 0:n.payment_intent)||void 0===s?void 0:s.external_intent_id):(createErrorNotice({code:"missing_payment_intent",message:wp.i18n.__("Something went wrong. Please contact us for payment.","surecart")}),e())})),t.FUNDING.PAYPAL){const a=t.Buttons({fundingSource:t.FUNDING.PAYPAL,style:{label:this.label,color:this.color},...e});a.isEligible()&&a.render(this.paypalContainer)}if(t.FUNDING.CARD){const a=t.Buttons({fundingSource:t.FUNDING.CARD,style:{color:"black"},...e});a.isEligible()&&a.render(this.cardContainer)}}render(){return h("div",{key:"d6f23e66b7ac788fba410e854a6152e777a33064",part:`base ${this.busy||!this.loaded&&"base--busy"}`,class:{"paypal-buttons":!0,"paypal-buttons--busy":this.busy||!this.loaded}},(!this.loaded||this.busy)&&h("sc-skeleton",{key:"7c052193ddd7953245913a0f5405798b1e028bdd",style:{height:"55px","--border-radius":"4px",cursor:"wait"}}),h("div",{key:"752920f5b0c6e5179fbffbd6b6d85315da9cdd92",class:"sc-paypal-button-container",hidden:!this.loaded||this.busy},h("div",{key:"071162683cf1021ed82a51217ec7ebab3ef5f0c4",part:"paypal-card-button",hidden:!this.buttons.includes("card"),class:"sc-paypal-card-button",ref:t=>this.cardContainer=t}),h("div",{key:"3ff0ee50a82f9527ce82db3e0f0ec70dbbdba8b9",part:"paypal-button",hidden:!this.buttons.includes("paypal"),class:"sc-paypal-button",ref:t=>this.paypalContainer=t})))}get el(){return this}static get watchers(){return{order:["handleOrderChange"]}}static get style(){return ScPaypalButtonsStyle0}},[1,"sc-paypal-buttons",{clientId:[1,"client-id"],busy:[4],merchantId:[1,"merchant-id"],merchantInitiated:[4,"merchant-initiated"],mode:[1],order:[1040],buttons:[16],label:[1],color:[1],loaded:[32]},void 0,{order:["handleOrderChange"]}]);function defineCustomElement(){"undefined"!=typeof customElements&&["sc-paypal-buttons","sc-skeleton"].forEach((t=>{switch(t){case"sc-paypal-buttons":customElements.get(t)||customElements.define(t,ScPaypalButtons);break;case"sc-skeleton":customElements.get(t)||defineCustomElement$1()}}))}export{ScPaypalButtons as S,defineCustomElement as d};
Save
Back