FileMaster
Search
Toggle Dark Mode
Home
/
.
/
wp-content
/
plugins
/
surecart
/
dist
/
components
/
components
Edit File: sc-subscription-payment.js
import{proxyCustomElement,HTMLElement,h,Fragment}from"@stencil/core/internal/client";import{a as apiFetch}from"./fetch.js";import{d as defineCustomElement$m}from"./sc-alert2.js";import{d as defineCustomElement$l}from"./sc-block-ui2.js";import{d as defineCustomElement$k}from"./sc-button2.js";import{d as defineCustomElement$j}from"./sc-card2.js";import{d as defineCustomElement$i}from"./sc-cc-logo2.js";import{d as defineCustomElement$h}from"./sc-choice2.js";import{d as defineCustomElement$g}from"./sc-choices2.js";import{d as defineCustomElement$f}from"./sc-dashboard-module2.js";import{d as defineCustomElement$e}from"./sc-empty2.js";import{d as defineCustomElement$d}from"./sc-form2.js";import{d as defineCustomElement$c}from"./sc-form-control2.js";import{d as defineCustomElement$b}from"./sc-icon2.js";import{d as defineCustomElement$a}from"./sc-manual-payment-method2.js";import{d as defineCustomElement$9}from"./sc-payment-method2.js";import{d as defineCustomElement$8}from"./sc-prose2.js";import{d as defineCustomElement$7}from"./sc-skeleton2.js";import{d as defineCustomElement$6}from"./sc-spinner2.js";import{d as defineCustomElement$5}from"./sc-tag2.js";import{d as defineCustomElement$4}from"./sc-text2.js";import{d as defineCustomElement$3}from"./sc-tooltip2.js";import{d as defineCustomElement$2}from"./sc-visually-hidden2.js";import{a as addQueryArgs}from"./add-query-args.js";const scSubscriptionPaymentCss=":host{display:block;position:relative}.subscription-payment{display:grid;gap:0.5em}",ScSubscriptionPaymentStyle0=scSubscriptionPaymentCss,ScSubscriptionPayment$1=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow(),this.subscriptionId=void 0,this.backUrl=void 0,this.successUrl=void 0,this.subscription=void 0,this.paymentMethods=[],this.customerIds=[],this.manualPaymentMethods=void 0,this.loading=void 0,this.busy=void 0,this.error=void 0}componentWillLoad(){this.fetchItems()}async fetchItems(){try{this.loading=!0,await Promise.all([this.fetchSubscription(),this.fetchPaymentMethods()])}catch(e){console.error(e),this.error=(null==e?void 0:e.message)||wp.i18n.__("Something went wrong","surecart")}finally{this.loading=!1}}async fetchSubscription(){this.subscriptionId&&(this.subscription=await apiFetch({path:addQueryArgs(`/surecart/v1/subscriptions/${this.subscriptionId}`,{expand:["price","price.product","current_period","product"]})}))}async fetchPaymentMethods(){var e,t;this.paymentMethods=await apiFetch({path:addQueryArgs("/surecart/v1/payment_methods",{expand:["card","customer","billing_agreement","paypal_account","payment_instrument","bank_account"],customer_ids:this.customerIds,reusable:!0,...null!==(null===(e=this.subscription)||void 0===e?void 0:e.live_mode)?{live_mode:this.subscription.live_mode}:{}})}),this.manualPaymentMethods=await apiFetch({path:addQueryArgs("surecart/v1/manual_payment_methods",{customer_ids:this.customerIds,reusable:!0,live_mode:null===(t=this.subscription)||void 0===t?void 0:t.live_mode})}),this.manualPaymentMethods=this.manualPaymentMethods.filter((e=>!(null==e?void 0:e.archived)||(null==e?void 0:e.id)===this.currentPaymentMethodId()))}async handleSubmit(e){var t;const{payment_method:s}=await e.target.getFormJson(),i=(this.manualPaymentMethods||[]).some((e=>e.id===s));try{this.error="",this.busy=!0,await apiFetch({path:`/surecart/v1/subscriptions/${null===(t=this.subscription)||void 0===t?void 0:t.id}`,method:"PATCH",data:{...i?{manual_payment_method:s,manual_payment:!0}:{payment_method:s,manual_payment:!1}}}),this.successUrl?window.location.assign(this.successUrl):this.busy=!1}catch(e){this.error=(null==e?void 0:e.message)||wp.i18n.__("Something went wrong","surecart"),this.busy=!1}}renderLoading(){return h(Fragment,null,h("sc-choice",{name:"loading",disabled:!0},h("sc-skeleton",{style:{width:"60px",display:"inline-block"}}),h("sc-skeleton",{style:{width:"80px",display:"inline-block"},slot:"price"}),h("sc-skeleton",{style:{width:"120px",display:"inline-block"},slot:"description"})),h("sc-button",{type:"primary",full:!0,submit:!0,loading:!0,busy:!0}),!!this.backUrl&&h("sc-button",{href:this.backUrl,full:!0,loading:!0,busy:!0}))}currentPaymentMethodId(){var e,t,s,i,o;return(null===(e=this.subscription)||void 0===e?void 0:e.manual_payment)?null===(t=this.subscription)||void 0===t?void 0:t.manual_payment_method:(null===(i=null===(s=this.subscription)||void 0===s?void 0:s.payment_method)||void 0===i?void 0:i.id)||(null===(o=this.subscription)||void 0===o?void 0:o.payment_method)}renderContent(){var e,t,s;if(this.loading)return this.renderLoading();const i=this.paymentMethods.filter((e=>{var t;return(null==e?void 0:e.live_mode)===(null===(t=this.subscription)||void 0===t?void 0:t.live_mode)}));return!(null===(e=this.paymentMethods)||void 0===e?void 0:e.length)&&!(null===(t=this.manualPaymentMethods)||void 0===t?void 0:t.length)||(null===(s=this.paymentMethods)||void 0===s?void 0:s.length)&&!(null==i?void 0:i.length)?h(Fragment,null,h("sc-empty",{icon:"credit-card"},wp.i18n.__("You have no saved payment methods.","surecart")),!!this.backUrl&&h("sc-button",{href:this.backUrl,full:!0},wp.i18n.__("Go Back","surecart"))):h(Fragment,null,h("sc-choices",null,h("div",null,(this.paymentMethods||[]).map((e=>{var t;return(null==e?void 0:e.live_mode)!==(null===(t=null==this?void 0:this.subscription)||void 0===t?void 0:t.live_mode)?null:h("sc-choice",{checked:this.currentPaymentMethodId()===(null==e?void 0:e.id),name:"payment_method",value:null==e?void 0:e.id},h("sc-payment-method",{paymentMethod:e,full:!0}))})),(this.manualPaymentMethods||[]).map((e=>h("sc-choice",{checked:this.currentPaymentMethodId()===(null==e?void 0:e.id),name:"payment_method",value:null==e?void 0:e.id},h("sc-manual-payment-method",{paymentMethod:e,showDescription:!0})))))),h("sc-button",{type:"primary",full:!0,submit:!0,loading:this.loading||this.busy,disabled:this.loading||this.busy},wp.i18n.__("Update","surecart")),!!this.backUrl&&h("sc-button",{href:this.backUrl,full:!0,loading:this.loading||this.busy,disabled:this.loading||this.busy},wp.i18n.__("Go Back","surecart")))}render(){return h("sc-dashboard-module",{key:"59323c5cccfa543ce6639f9349f7ae92b660bf60",heading:wp.i18n.__("Select a payment method","surecart"),class:"subscription-payment",error:this.error},h("sc-form",{key:"290aa526c15ef014799f499e10b4f7ee60d6b590",onScFormSubmit:e=>this.handleSubmit(e)},h("sc-card",{key:"2b494b1217f7ef00d2e3ef462db1015a33e55bc6"},this.renderContent())),this.busy&&h("sc-block-ui",{key:"d708c065fcaf6a46ffbe2519febce4fb597942f5"}))}static get style(){return ScSubscriptionPaymentStyle0}},[1,"sc-subscription-payment",{subscriptionId:[1,"subscription-id"],backUrl:[1,"back-url"],successUrl:[1,"success-url"],subscription:[1040],paymentMethods:[16],customerIds:[16],manualPaymentMethods:[32],loading:[32],busy:[32],error:[32]}]);function defineCustomElement$1(){"undefined"!=typeof customElements&&["sc-subscription-payment","sc-alert","sc-block-ui","sc-button","sc-card","sc-cc-logo","sc-choice","sc-choices","sc-dashboard-module","sc-empty","sc-form","sc-form-control","sc-icon","sc-manual-payment-method","sc-payment-method","sc-prose","sc-skeleton","sc-spinner","sc-tag","sc-text","sc-tooltip","sc-visually-hidden"].forEach((e=>{switch(e){case"sc-subscription-payment":customElements.get(e)||customElements.define(e,ScSubscriptionPayment$1);break;case"sc-alert":customElements.get(e)||defineCustomElement$m();break;case"sc-block-ui":customElements.get(e)||defineCustomElement$l();break;case"sc-button":customElements.get(e)||defineCustomElement$k();break;case"sc-card":customElements.get(e)||defineCustomElement$j();break;case"sc-cc-logo":customElements.get(e)||defineCustomElement$i();break;case"sc-choice":customElements.get(e)||defineCustomElement$h();break;case"sc-choices":customElements.get(e)||defineCustomElement$g();break;case"sc-dashboard-module":customElements.get(e)||defineCustomElement$f();break;case"sc-empty":customElements.get(e)||defineCustomElement$e();break;case"sc-form":customElements.get(e)||defineCustomElement$d();break;case"sc-form-control":customElements.get(e)||defineCustomElement$c();break;case"sc-icon":customElements.get(e)||defineCustomElement$b();break;case"sc-manual-payment-method":customElements.get(e)||defineCustomElement$a();break;case"sc-payment-method":customElements.get(e)||defineCustomElement$9();break;case"sc-prose":customElements.get(e)||defineCustomElement$8();break;case"sc-skeleton":customElements.get(e)||defineCustomElement$7();break;case"sc-spinner":customElements.get(e)||defineCustomElement$6();break;case"sc-tag":customElements.get(e)||defineCustomElement$5();break;case"sc-text":customElements.get(e)||defineCustomElement$4();break;case"sc-tooltip":customElements.get(e)||defineCustomElement$3();break;case"sc-visually-hidden":customElements.get(e)||defineCustomElement$2()}}))}const ScSubscriptionPayment=ScSubscriptionPayment$1,defineCustomElement=defineCustomElement$1;export{ScSubscriptionPayment,defineCustomElement};
Save
Back