recur-tw 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2619,7 +2619,7 @@ function toCamelCase(obj) {
2619
2619
 
2620
2620
  // package.json
2621
2621
  var package_default = {
2622
- version: "0.9.2"};
2622
+ version: "0.9.3"};
2623
2623
  var SDK_VERSION = package_default.version;
2624
2624
  var SDK_TYPE = "react";
2625
2625
  var RecurContext = React.createContext(null);
@@ -2952,6 +2952,15 @@ function RecurProvider({ children, config: initialConfig = {} }) {
2952
2952
  console.log("[Recur SDK] Payment executed:", paymentResult);
2953
2953
  if (paymentResult.requires3D && paymentResult.redirectUrl) {
2954
2954
  console.log("[Recur SDK] 3D verification required");
2955
+ const isMobileOrWebView = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || // Detect common WebView user agents
2956
+ /wv|WebView|FBAN|FBAV|Instagram|Line|MicroMessenger|QQ/i.test(navigator.userAgent) || // iOS WebView detection
2957
+ /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent) || // Check if we're in standalone mode (PWA) or WebView
2958
+ "standalone" in navigator && navigator.standalone;
2959
+ if (isMobileOrWebView) {
2960
+ console.log("[Recur SDK] Mobile/WebView detected, using redirect for 3D verification");
2961
+ window.location.href = paymentResult.redirectUrl;
2962
+ return;
2963
+ }
2955
2964
  console.log("[Recur SDK] Using popup for 3D verification");
2956
2965
  {
2957
2966
  const popup = window.open(
package/dist/index.js CHANGED
@@ -2613,7 +2613,7 @@ function toCamelCase(obj) {
2613
2613
 
2614
2614
  // package.json
2615
2615
  var package_default = {
2616
- version: "0.9.2"};
2616
+ version: "0.9.3"};
2617
2617
  var SDK_VERSION = package_default.version;
2618
2618
  var SDK_TYPE = "react";
2619
2619
  var RecurContext = createContext(null);
@@ -2946,6 +2946,15 @@ function RecurProvider({ children, config: initialConfig = {} }) {
2946
2946
  console.log("[Recur SDK] Payment executed:", paymentResult);
2947
2947
  if (paymentResult.requires3D && paymentResult.redirectUrl) {
2948
2948
  console.log("[Recur SDK] 3D verification required");
2949
+ const isMobileOrWebView = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) || // Detect common WebView user agents
2950
+ /wv|WebView|FBAN|FBAV|Instagram|Line|MicroMessenger|QQ/i.test(navigator.userAgent) || // iOS WebView detection
2951
+ /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent) || // Check if we're in standalone mode (PWA) or WebView
2952
+ "standalone" in navigator && navigator.standalone;
2953
+ if (isMobileOrWebView) {
2954
+ console.log("[Recur SDK] Mobile/WebView detected, using redirect for 3D verification");
2955
+ window.location.href = paymentResult.redirectUrl;
2956
+ return;
2957
+ }
2949
2958
  console.log("[Recur SDK] Using popup for 3D verification");
2950
2959
  {
2951
2960
  const popup = window.open(
package/dist/recur.umd.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";var RecurCheckout=(()=>{var A=Object.defineProperty;var Re=Object.getOwnPropertyDescriptor;var Pe=Object.getOwnPropertyNames;var De=Object.prototype.hasOwnProperty;var Le=(a,e,t)=>e in a?A(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var w=(a,e)=>()=>(a&&(e=a(a=0)),e);var k=(a,e)=>{for(var t in e)A(a,t,{get:e[t],enumerable:!0})},Ue=(a,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of Pe(e))!De.call(a,s)&&s!==t&&A(a,s,{get:()=>e[s],enumerable:!(r=Re(e,s))||r.enumerable});return a};var Me=a=>Ue(A({},"__esModule",{value:!0}),a);var c=(a,e,t)=>Le(a,typeof e!="symbol"?e+"":e,t);var re={};k(re,{RecurLoadingSpinner:()=>_});var _,se=w(()=>{"use strict";_=class extends HTMLElement{static get observedAttributes(){return["message","size"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}attributeChangedCallback(e,t,r){t!==r&&this.render()}get message(){return this.getAttribute("message")||"\u6B63\u5728\u8655\u7406\u8A02\u95B1..."}get size(){let e=this.getAttribute("size");return e==="small"||e==="large"?e:"medium"}getSizeValue(){return{small:24,medium:40,large:56}[this.size]}render(){let e=this.getSizeValue();this.shadowRoot.innerHTML=`
1
+ "use strict";var RecurCheckout=(()=>{var _=Object.defineProperty;var Pe=Object.getOwnPropertyDescriptor;var De=Object.getOwnPropertyNames;var Me=Object.prototype.hasOwnProperty;var Le=(a,e,t)=>e in a?_(a,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[e]=t;var v=(a,e)=>()=>(a&&(e=a(a=0)),e);var k=(a,e)=>{for(var t in e)_(a,t,{get:e[t],enumerable:!0})},Ue=(a,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of De(e))!Me.call(a,s)&&s!==t&&_(a,s,{get:()=>e[s],enumerable:!(r=Pe(e,s))||r.enumerable});return a};var Ae=a=>Ue(_({},"__esModule",{value:!0}),a);var c=(a,e,t)=>Le(a,typeof e!="symbol"?e+"":e,t);var se={};k(se,{RecurLoadingSpinner:()=>z});var z,ie=v(()=>{"use strict";z=class extends HTMLElement{static get observedAttributes(){return["message","size"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}attributeChangedCallback(e,t,r){t!==r&&this.render()}get message(){return this.getAttribute("message")||"\u6B63\u5728\u8655\u7406\u8A02\u95B1..."}get size(){let e=this.getAttribute("size");return e==="small"||e==="large"?e:"medium"}getSizeValue(){return{small:24,medium:40,large:56}[this.size]}render(){let e=this.getSizeValue();this.shadowRoot.innerHTML=`
2
2
  <style>
3
3
  :host {
4
4
  display: block;
@@ -40,7 +40,7 @@
40
40
 
41
41
  <div class="recur-sdk__spinner"></div>
42
42
  ${this.message?`<p class="recur-sdk__loading-text">${this.message}</p>`:""}
43
- `}};typeof window<"u"&&!customElements.get("recur-loading-spinner")&&customElements.define("recur-loading-spinner",_)});var ie={};k(ie,{RecurSuccessMessage:()=>z});var z,oe=w(()=>{"use strict";z=class extends HTMLElement{static get observedAttributes(){return["title","message","icon"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}attributeChangedCallback(e,t,r){t!==r&&this.render()}get successTitle(){return this.getAttribute("title")||"Subscription Complete!"}get successMessage(){return this.getAttribute("message")||"Thank you for subscribing. Your payment has been processed successfully."}get showIcon(){return this.getAttribute("icon")!=="false"}render(){this.shadowRoot.innerHTML=`
43
+ `}};typeof window<"u"&&!customElements.get("recur-loading-spinner")&&customElements.define("recur-loading-spinner",z)});var oe={};k(oe,{RecurSuccessMessage:()=>$});var $,ne=v(()=>{"use strict";$=class extends HTMLElement{static get observedAttributes(){return["title","message","icon"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}attributeChangedCallback(e,t,r){t!==r&&this.render()}get successTitle(){return this.getAttribute("title")||"Subscription Complete!"}get successMessage(){return this.getAttribute("message")||"Thank you for subscribing. Your payment has been processed successfully."}get showIcon(){return this.getAttribute("icon")!=="false"}render(){this.shadowRoot.innerHTML=`
44
44
  <style>
45
45
  :host {
46
46
  display: block;
@@ -121,7 +121,7 @@
121
121
  <p class="recur-sdk__success-message">${this.successMessage}</p>
122
122
  <slot></slot>
123
123
  </div>
124
- `}};typeof window<"u"&&!customElements.get("recur-success-message")&&customElements.define("recur-success-message",z)});var ne={};k(ne,{RecurErrorDisplay:()=>$});var $,ae=w(()=>{"use strict";$=class extends HTMLElement{static get observedAttributes(){return["error","dismissible"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}attributeChangedCallback(e,t,r){t!==r&&this.render()}get error(){return this.getAttribute("error")||""}get isDismissible(){return this.getAttribute("dismissible")==="true"}handleDismiss(){this.dispatchEvent(new CustomEvent("dismiss",{bubbles:!0,composed:!0})),this.remove()}render(){if(!this.error){this.shadowRoot.innerHTML="";return}this.shadowRoot.innerHTML=`
124
+ `}};typeof window<"u"&&!customElements.get("recur-success-message")&&customElements.define("recur-success-message",$)});var ae={};k(ae,{RecurErrorDisplay:()=>H});var H,ce=v(()=>{"use strict";H=class extends HTMLElement{static get observedAttributes(){return["error","dismissible"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}attributeChangedCallback(e,t,r){t!==r&&this.render()}get error(){return this.getAttribute("error")||""}get isDismissible(){return this.getAttribute("dismissible")==="true"}handleDismiss(){this.dispatchEvent(new CustomEvent("dismiss",{bubbles:!0,composed:!0})),this.remove()}render(){if(!this.error){this.shadowRoot.innerHTML="";return}this.shadowRoot.innerHTML=`
125
125
  <style>
126
126
  :host {
127
127
  display: block;
@@ -211,7 +211,7 @@
211
211
  </button>
212
212
  `:""}
213
213
  </div>
214
- `,this.isDismissible&&this.shadowRoot.querySelector(".recur-sdk__error-dismiss")?.addEventListener("click",()=>this.handleDismiss())}};typeof window<"u"&&!customElements.get("recur-error-display")&&customElements.define("recur-error-display",$)});var ce={};k(ce,{RecurSkeletonLoader:()=>H});var H,le=w(()=>{"use strict";H=class extends HTMLElement{static get observedAttributes(){return["type"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}attributeChangedCallback(e,t,r){t!==r&&this.render()}get type(){let e=this.getAttribute("type");return e==="list"||e==="card"?e:"payment-form"}renderPaymentFormSkeleton(){return`
214
+ `,this.isDismissible&&this.shadowRoot.querySelector(".recur-sdk__error-dismiss")?.addEventListener("click",()=>this.handleDismiss())}};typeof window<"u"&&!customElements.get("recur-error-display")&&customElements.define("recur-error-display",H)});var le={};k(le,{RecurSkeletonLoader:()=>K});var K,de=v(()=>{"use strict";K=class extends HTMLElement{static get observedAttributes(){return["type"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}attributeChangedCallback(e,t,r){t!==r&&this.render()}get type(){let e=this.getAttribute("type");return e==="list"||e==="card"?e:"payment-form"}renderPaymentFormSkeleton(){return`
215
215
  <div class="skeleton-field">
216
216
  <div class="skeleton-label"></div>
217
217
  <div class="skeleton-input"></div>
@@ -377,7 +377,7 @@
377
377
  </style>
378
378
 
379
379
  ${e}
380
- `}};typeof window<"u"&&!customElements.get("recur-skeleton-loader")&&customElements.define("recur-skeleton-loader",H)});var de={};k(de,{RecurPaymentFormSkeleton:()=>K});var K,ue=w(()=>{"use strict";K=class extends HTMLElement{constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}render(){this.shadowRoot.innerHTML=`
380
+ `}};typeof window<"u"&&!customElements.get("recur-skeleton-loader")&&customElements.define("recur-skeleton-loader",K)});var ue={};k(ue,{RecurPaymentFormSkeleton:()=>B});var B,me=v(()=>{"use strict";B=class extends HTMLElement{constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}render(){this.shadowRoot.innerHTML=`
381
381
  <style>
382
382
  :host {
383
383
  display: block;
@@ -644,7 +644,7 @@
644
644
  <p class="security-text">\u60A8\u7684\u4ED8\u6B3E\u8CC7\u8A0A\u7D93\u904E\u52A0\u5BC6\u4FDD\u8B77</p>
645
645
  </div>
646
646
  </div>
647
- `}};typeof window<"u"&&!customElements.get("recur-payment-form-skeleton")&&customElements.define("recur-payment-form-skeleton",K)});var me={};k(me,{RecurToast:()=>N,RecurToastContainer:()=>D});var N,x,D,pe=w(()=>{"use strict";N=class extends HTMLElement{static get observedAttributes(){return["message","type","duration"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render(),this.setupAutoDismiss()}get message(){return this.getAttribute("message")||"Notification"}get type(){let e=this.getAttribute("type");return e==="success"||e==="error"?e:"info"}get duration(){let e=this.getAttribute("duration");return e?parseInt(e,10):5e3}setupAutoDismiss(){let e=this.duration;e>0&&setTimeout(()=>this.dismiss(),e)}dismiss(){this.style.animation="recur-toast-slide-out 0.3s ease-in-out",setTimeout(()=>this.remove(),300)}getTypeIcon(){switch(this.type){case"success":return`<svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
647
+ `}};typeof window<"u"&&!customElements.get("recur-payment-form-skeleton")&&customElements.define("recur-payment-form-skeleton",B)});var pe={};k(pe,{RecurToast:()=>N,RecurToastContainer:()=>D});var N,w,D,he=v(()=>{"use strict";N=class extends HTMLElement{static get observedAttributes(){return["message","type","duration"]}constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render(),this.setupAutoDismiss()}get message(){return this.getAttribute("message")||"Notification"}get type(){let e=this.getAttribute("type");return e==="success"||e==="error"?e:"info"}get duration(){let e=this.getAttribute("duration");return e?parseInt(e,10):5e3}setupAutoDismiss(){let e=this.duration;e>0&&setTimeout(()=>this.dismiss(),e)}dismiss(){this.style.animation="recur-toast-slide-out 0.3s ease-in-out",setTimeout(()=>this.remove(),300)}getTypeIcon(){switch(this.type){case"success":return`<svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
648
648
  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
649
649
  </svg>`;case"error":return`<svg width="20" height="20" fill="none" stroke="currentColor" viewBox="0 0 24 24">
650
650
  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
@@ -764,7 +764,7 @@
764
764
  </svg>
765
765
  </button>
766
766
  </div>
767
- `,this.shadowRoot.querySelector(".toast-close")?.addEventListener("click",s=>{s.stopPropagation(),this.dismiss()}),this.shadowRoot.querySelector(".toast")?.addEventListener("click",()=>this.dismiss())}static show(e,t="info",r=5e3){let s=D.getInstance(),i=document.createElement("recur-toast");return i.setAttribute("message",e),i.setAttribute("type",t),i.setAttribute("duration",r.toString()),s.appendChild(i),i}},x=class x extends HTMLElement{constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}render(){this.shadowRoot.innerHTML=`
767
+ `,this.shadowRoot.querySelector(".toast-close")?.addEventListener("click",s=>{s.stopPropagation(),this.dismiss()}),this.shadowRoot.querySelector(".toast")?.addEventListener("click",()=>this.dismiss())}static show(e,t="info",r=5e3){let s=D.getInstance(),i=document.createElement("recur-toast");return i.setAttribute("message",e),i.setAttribute("type",t),i.setAttribute("duration",r.toString()),s.appendChild(i),i}},w=class w extends HTMLElement{constructor(){super(),this.attachShadow({mode:"open"})}connectedCallback(){this.render()}render(){this.shadowRoot.innerHTML=`
768
768
  <style>
769
769
  :host {
770
770
  position: fixed;
@@ -791,7 +791,7 @@
791
791
  </style>
792
792
 
793
793
  <slot></slot>
794
- `}static getInstance(){return x.instance||(x.instance=document.querySelector("recur-toast-container"),x.instance||(x.instance=document.createElement("recur-toast-container"),document.body.appendChild(x.instance))),x.instance}};c(x,"instance",null);D=x;typeof window<"u"&&!customElements.get("recur-toast")&&customElements.define("recur-toast",N);typeof window<"u"&&!customElements.get("recur-toast-container")&&customElements.define("recur-toast-container",D)});var he={};k(he,{RecurPaymentForm:()=>B});var B,fe=w(()=>{"use strict";B=class extends HTMLElement{constructor(){super();c(this,"containerId");c(this,"customStyles");c(this,"_isInitializing",!1);c(this,"_initializationAborted",!1);this.containerId=this.getAttribute("container-id")||`recur-${Date.now()}`,this.customStyles=this.getAttribute("custom-styles")||"",this.attachShadow({mode:"open"})}connectedCallback(){this.render()}disconnectedCallback(){console.log("[PaymentForm] Component disconnected, cleaning up..."),this._initializationAborted=!0,this._paymentSession=null;let t=document.getElementById(`${this.containerId}-submit-btn`);if(t){let r=t.cloneNode(!0);t.parentNode?.replaceChild(r,t)}}static get observedAttributes(){return["custom-styles","customer-name","customer-email","plan-name","amount","billing-period"]}attributeChangedCallback(t,r,s){t==="custom-styles"&&r!==s?(this.customStyles=s||"",this.updateCustomStyles()):r!==s&&this.updateCustomerInfoSection()}render(){this.shadowRoot.innerHTML=`
794
+ `}static getInstance(){return w.instance||(w.instance=document.querySelector("recur-toast-container"),w.instance||(w.instance=document.createElement("recur-toast-container"),document.body.appendChild(w.instance))),w.instance}};c(w,"instance",null);D=w;typeof window<"u"&&!customElements.get("recur-toast")&&customElements.define("recur-toast",N);typeof window<"u"&&!customElements.get("recur-toast-container")&&customElements.define("recur-toast-container",D)});var fe={};k(fe,{RecurPaymentForm:()=>O});var O,ge=v(()=>{"use strict";O=class extends HTMLElement{constructor(){super();c(this,"containerId");c(this,"customStyles");c(this,"_isInitializing",!1);c(this,"_initializationAborted",!1);this.containerId=this.getAttribute("container-id")||`recur-${Date.now()}`,this.customStyles=this.getAttribute("custom-styles")||"",this.attachShadow({mode:"open"})}connectedCallback(){this.render()}disconnectedCallback(){console.log("[PaymentForm] Component disconnected, cleaning up..."),this._initializationAborted=!0,this._paymentSession=null;let t=document.getElementById(`${this.containerId}-submit-btn`);if(t){let r=t.cloneNode(!0);t.parentNode?.replaceChild(r,t)}}static get observedAttributes(){return["custom-styles","customer-name","customer-email","plan-name","amount","billing-period"]}attributeChangedCallback(t,r,s){t==="custom-styles"&&r!==s?(this.customStyles=s||"",this.updateCustomStyles()):r!==s&&this.updateCustomerInfoSection()}render(){this.shadowRoot.innerHTML=`
795
795
  <style>
796
796
  :host {
797
797
  display: block;
@@ -1220,13 +1220,13 @@
1220
1220
  >
1221
1221
  <span id="${this.containerId}-submit-text">\u78BA\u8A8D\u4ED8\u6B3E</span>
1222
1222
  </button>
1223
- `,t}async initializePayment(t,r){if(this._initializationAborted)return console.log("[PaymentForm] Initialization aborted before start"),null;if(this._isInitializing=!0,await this.updateComplete(),this._initializationAborted)return console.log("[PaymentForm] Initialization aborted after updateComplete"),this._isInitializing=!1,null;if(!window.UniPayment)throw this._isInitializing=!1,new Error("PAYUNi SDK not loaded");try{let s=document.getElementById(`${this.containerId}-card-no`),i=document.getElementById(`${this.containerId}-card-exp`),n=document.getElementById(`${this.containerId}-card-cvc`);if(!s||!i||!n)return console.log("[PaymentForm] DOM elements not found, likely disconnected"),this._isInitializing=!1,null;if(this._initializationAborted)return console.log("[PaymentForm] Initialization aborted before createSession"),this._isInitializing=!1,null;let o=window.UniPayment.createSession(t,{env:r==="SANDBOX"?"S":"P",elements:{CardNo:`${this.containerId}-card-no`,CardExp:`${this.containerId}-card-exp`,CardCvc:`${this.containerId}-card-cvc`}});if(this._initializationAborted)return console.log("[PaymentForm] Initialization aborted before paymentSession.start()"),this._isInitializing=!1,null;try{await o.start()}catch(d){if((d?.message?.includes("1008")||d?.message?.includes("timeout")||d?.code===1008)&&this._initializationAborted)return console.log("[PaymentForm] Caught 1008 timeout error after component disconnect - ignoring"),this._isInitializing=!1,null;throw d}return this._initializationAborted?(console.log("[PaymentForm] Initialization aborted after paymentSession.start()"),this._isInitializing=!1,null):(this.hideCardSkeletons(),o.onUpdate?.(d=>{if(this._initializationAborted){console.log("[PaymentForm] Ignoring onUpdate event - component disconnected");return}console.log("[PaymentForm] PAYUNi update:",d);let m=d.status&&d.status.CardNo===!0&&d.status.CardExp===!0&&d.status.CardCvc===!0,E=document.getElementById(`${this.containerId}-submit-btn`);E&&(E.disabled=!m,console.log("[PaymentForm] Submit button disabled:",!m))}),this._initializationAborted?(console.log("[PaymentForm] Initialization aborted before saving session"),this._isInitializing=!1,null):(this._paymentSession=o,this.setupFormSubmission(),this._isInitializing=!1,o))}catch(s){throw this._isInitializing=!1,console.error("[PaymentForm] Failed to initialize payment:",s),s}}setupFormSubmission(){let t=document.getElementById(`${this.containerId}-submit-btn`);t&&t.addEventListener("click",async r=>{if(r.preventDefault(),t.classList.contains("loading"))return;let s,i,n=document.getElementById(`${this.containerId}-email`),o=document.getElementById(`${this.containerId}-name`);if(n&&o){if(s=n.value,i=o.value,!s||!i){this.showError("\u8ACB\u586B\u5BEB\u59D3\u540D\u548C\u96FB\u5B50\u90F5\u4EF6");return}}else if(s=this.getAttribute("customer-email")||void 0,i=this.getAttribute("customer-name")||void 0,!s||!i){this.showError("\u5BA2\u6236\u8CC7\u8A0A\u4E0D\u5B8C\u6574");return}this.clearError(),this.setButtonLoading(!0),this.dispatchEvent(new CustomEvent("submit",{detail:{customerEmail:s,customerName:i,paymentSession:this._paymentSession},bubbles:!0,composed:!0}))})}setButtonLoading(t){let r=document.getElementById(`${this.containerId}-submit-btn`);r&&(t?(r.classList.add("loading"),r.disabled=!0,r.innerHTML=`
1223
+ `,t}async initializePayment(t,r){if(this._initializationAborted)return console.log("[PaymentForm] Initialization aborted before start"),null;if(this._isInitializing=!0,await this.updateComplete(),this._initializationAborted)return console.log("[PaymentForm] Initialization aborted after updateComplete"),this._isInitializing=!1,null;if(!window.UniPayment)throw this._isInitializing=!1,new Error("PAYUNi SDK not loaded");try{let s=document.getElementById(`${this.containerId}-card-no`),i=document.getElementById(`${this.containerId}-card-exp`),n=document.getElementById(`${this.containerId}-card-cvc`);if(!s||!i||!n)return console.log("[PaymentForm] DOM elements not found, likely disconnected"),this._isInitializing=!1,null;if(this._initializationAborted)return console.log("[PaymentForm] Initialization aborted before createSession"),this._isInitializing=!1,null;let o=window.UniPayment.createSession(t,{env:r==="SANDBOX"?"S":"P",elements:{CardNo:`${this.containerId}-card-no`,CardExp:`${this.containerId}-card-exp`,CardCvc:`${this.containerId}-card-cvc`}});if(this._initializationAborted)return console.log("[PaymentForm] Initialization aborted before paymentSession.start()"),this._isInitializing=!1,null;try{await o.start()}catch(d){if((d?.message?.includes("1008")||d?.message?.includes("timeout")||d?.code===1008)&&this._initializationAborted)return console.log("[PaymentForm] Caught 1008 timeout error after component disconnect - ignoring"),this._isInitializing=!1,null;throw d}return this._initializationAborted?(console.log("[PaymentForm] Initialization aborted after paymentSession.start()"),this._isInitializing=!1,null):(this.hideCardSkeletons(),o.onUpdate?.(d=>{if(this._initializationAborted){console.log("[PaymentForm] Ignoring onUpdate event - component disconnected");return}console.log("[PaymentForm] PAYUNi update:",d);let m=d.status&&d.status.CardNo===!0&&d.status.CardExp===!0&&d.status.CardCvc===!0,x=document.getElementById(`${this.containerId}-submit-btn`);x&&(x.disabled=!m,console.log("[PaymentForm] Submit button disabled:",!m))}),this._initializationAborted?(console.log("[PaymentForm] Initialization aborted before saving session"),this._isInitializing=!1,null):(this._paymentSession=o,this.setupFormSubmission(),this._isInitializing=!1,o))}catch(s){throw this._isInitializing=!1,console.error("[PaymentForm] Failed to initialize payment:",s),s}}setupFormSubmission(){let t=document.getElementById(`${this.containerId}-submit-btn`);t&&t.addEventListener("click",async r=>{if(r.preventDefault(),t.classList.contains("loading"))return;let s,i,n=document.getElementById(`${this.containerId}-email`),o=document.getElementById(`${this.containerId}-name`);if(n&&o){if(s=n.value,i=o.value,!s||!i){this.showError("\u8ACB\u586B\u5BEB\u59D3\u540D\u548C\u96FB\u5B50\u90F5\u4EF6");return}}else if(s=this.getAttribute("customer-email")||void 0,i=this.getAttribute("customer-name")||void 0,!s||!i){this.showError("\u5BA2\u6236\u8CC7\u8A0A\u4E0D\u5B8C\u6574");return}this.clearError(),this.setButtonLoading(!0),this.dispatchEvent(new CustomEvent("submit",{detail:{customerEmail:s,customerName:i,paymentSession:this._paymentSession},bubbles:!0,composed:!0}))})}setButtonLoading(t){let r=document.getElementById(`${this.containerId}-submit-btn`);r&&(t?(r.classList.add("loading"),r.disabled=!0,r.innerHTML=`
1224
1224
  <span class="recur-loading-spinner"></span>
1225
1225
  <span>\u8655\u7406\u4E2D...</span>
1226
1226
  `):(r.classList.remove("loading"),r.disabled=!1,r.innerHTML='<span id="'+this.containerId+'-submit-text">\u78BA\u8A8D\u4ED8\u6B3E</span>'))}resetButton(){this.setButtonLoading(!1)}setVerifying(t){let r=document.getElementById(`${this.containerId}-submit-btn`);r&&(t?(r.classList.add("loading"),r.disabled=!0,r.innerHTML=`
1227
1227
  <span class="recur-loading-spinner"></span>
1228
1228
  <span>3D \u9A57\u8B49\u4E2D...</span>
1229
- `):this.setButtonLoading(!1))}showError(t){let r=this.querySelector(".recur-sdk__error-container");r||(r=document.createElement("div"),r.className="recur-sdk__error-container",r.style.cssText="margin-bottom: 16px;",this.insertBefore(r,this.firstChild));let s=document.createElement("recur-error-display");s.setAttribute("error",t),s.setAttribute("dismissible","true"),r.innerHTML="",r.appendChild(s),s.scrollIntoView({behavior:"smooth",block:"center"})}clearError(){let t=this.querySelector(".recur-sdk__error-container");t&&(t.innerHTML="")}updateComplete(){return new Promise(t=>{if(this.isConnected)setTimeout(t,0);else{let r=new MutationObserver(()=>{this.isConnected&&(r.disconnect(),setTimeout(t,0))});r.observe(document.body,{childList:!0,subtree:!0})}})}getFormData(){return{email:document.getElementById(`${this.containerId}-email`)?.value,name:document.getElementById(`${this.containerId}-name`)?.value}}setFormData(t){if(t.email){let r=document.getElementById(`${this.containerId}-email`);r&&(r.value=t.email)}if(t.name){let r=document.getElementById(`${this.containerId}-name`);r&&(r.value=t.name)}}};customElements.get("recur-payment-form")||customElements.define("recur-payment-form",B)});var ge={};k(ge,{RecurCheckoutButton:()=>O});var O,be=w(()=>{"use strict";O=class extends HTMLElement{constructor(){super();c(this,"_isLoading",!1);c(this,"handleClick",async t=>{if(t.preventDefault(),this._isLoading||this.hasAttribute("disabled"))return;let r=this.getAttribute("publishable-key"),s=this.getAttribute("product-id"),i=this.getAttribute("success-url"),n=this.getAttribute("cancel-url");if(!r){this.dispatchError("Missing required attribute: publishable-key");return}if(!s){this.dispatchError("Missing required attribute: product-id");return}if(!i){this.dispatchError("Missing required attribute: success-url");return}if(!n){this.dispatchError("Missing required attribute: cancel-url");return}this._isLoading=!0,this.render(),this.setupEventListeners();try{let o=await this.createCheckoutSession({publishableKey:r,productId:s,successUrl:this.resolveUrl(i),cancelUrl:this.resolveUrl(n),customerEmail:this.getAttribute("customer-email")||void 0,mode:this.getAttribute("mode")});this.dispatchEvent(new CustomEvent("checkout-started",{detail:{sessionId:o.id,url:o.url},bubbles:!0,composed:!0})),window.location.href=o.url}catch(o){this._isLoading=!1,this.render(),this.setupEventListeners(),this.dispatchError(o.message||"Failed to create checkout session")}});this.attachShadow({mode:"open"})}static get observedAttributes(){return["publishable-key","product-id","success-url","cancel-url","customer-email","mode","button-text","button-style","disabled"]}connectedCallback(){this.render(),this.setupEventListeners()}disconnectedCallback(){let t=this.shadowRoot?.querySelector("button");t&&t.removeEventListener("click",this.handleClick)}attributeChangedCallback(t,r,s){r!==s&&this.render()}render(){let t=this.getAttribute("button-text")||this.textContent?.trim()||"\u8A02\u95B1",r=this.getAttribute("button-style")||"primary",s=this.hasAttribute("disabled")||this._isLoading;this.shadowRoot.innerHTML=`
1229
+ `):this.setButtonLoading(!1))}showError(t){let r=this.querySelector(".recur-sdk__error-container");r||(r=document.createElement("div"),r.className="recur-sdk__error-container",r.style.cssText="margin-bottom: 16px;",this.insertBefore(r,this.firstChild));let s=document.createElement("recur-error-display");s.setAttribute("error",t),s.setAttribute("dismissible","true"),r.innerHTML="",r.appendChild(s),s.scrollIntoView({behavior:"smooth",block:"center"})}clearError(){let t=this.querySelector(".recur-sdk__error-container");t&&(t.innerHTML="")}updateComplete(){return new Promise(t=>{if(this.isConnected)setTimeout(t,0);else{let r=new MutationObserver(()=>{this.isConnected&&(r.disconnect(),setTimeout(t,0))});r.observe(document.body,{childList:!0,subtree:!0})}})}getFormData(){return{email:document.getElementById(`${this.containerId}-email`)?.value,name:document.getElementById(`${this.containerId}-name`)?.value}}setFormData(t){if(t.email){let r=document.getElementById(`${this.containerId}-email`);r&&(r.value=t.email)}if(t.name){let r=document.getElementById(`${this.containerId}-name`);r&&(r.value=t.name)}}};customElements.get("recur-payment-form")||customElements.define("recur-payment-form",O)});var be={};k(be,{RecurCheckoutButton:()=>j});var j,ye=v(()=>{"use strict";j=class extends HTMLElement{constructor(){super();c(this,"_isLoading",!1);c(this,"handleClick",async t=>{if(t.preventDefault(),this._isLoading||this.hasAttribute("disabled"))return;let r=this.getAttribute("publishable-key"),s=this.getAttribute("product-id"),i=this.getAttribute("success-url"),n=this.getAttribute("cancel-url");if(!r){this.dispatchError("Missing required attribute: publishable-key");return}if(!s){this.dispatchError("Missing required attribute: product-id");return}if(!i){this.dispatchError("Missing required attribute: success-url");return}if(!n){this.dispatchError("Missing required attribute: cancel-url");return}this._isLoading=!0,this.render(),this.setupEventListeners();try{let o=await this.createCheckoutSession({publishableKey:r,productId:s,successUrl:this.resolveUrl(i),cancelUrl:this.resolveUrl(n),customerEmail:this.getAttribute("customer-email")||void 0,mode:this.getAttribute("mode")});this.dispatchEvent(new CustomEvent("checkout-started",{detail:{sessionId:o.id,url:o.url},bubbles:!0,composed:!0})),window.location.href=o.url}catch(o){this._isLoading=!1,this.render(),this.setupEventListeners(),this.dispatchError(o.message||"Failed to create checkout session")}});this.attachShadow({mode:"open"})}static get observedAttributes(){return["publishable-key","product-id","success-url","cancel-url","customer-email","mode","button-text","button-style","disabled"]}connectedCallback(){this.render(),this.setupEventListeners()}disconnectedCallback(){let t=this.shadowRoot?.querySelector("button");t&&t.removeEventListener("click",this.handleClick)}attributeChangedCallback(t,r,s){r!==s&&this.render()}render(){let t=this.getAttribute("button-text")||this.textContent?.trim()||"\u8A02\u95B1",r=this.getAttribute("button-style")||"primary",s=this.hasAttribute("disabled")||this._isLoading;this.shadowRoot.innerHTML=`
1230
1230
  <style>
1231
1231
  :host {
1232
1232
  display: inline-block;
@@ -1325,7 +1325,7 @@
1325
1325
  ${this._isLoading?'<span class="spinner"></span>':""}
1326
1326
  <span>${this._isLoading?"\u8655\u7406\u4E2D...":t}</span>
1327
1327
  </button>
1328
- `}setupEventListeners(){let t=this.shadowRoot?.querySelector("button");t&&t.addEventListener("click",this.handleClick)}async createCheckoutSession(t){let r=this.getApiBaseUrl(),s={productId:t.productId,successUrl:t.successUrl,cancelUrl:t.cancelUrl};t.mode&&(s.mode=t.mode),t.customerEmail&&(s.customerEmail=t.customerEmail);let i=await fetch(`${r}/v1/checkout/sessions`,{method:"POST",headers:{"Content-Type":"application/json","X-Recur-Publishable-Key":t.publishableKey},body:JSON.stringify(s)});if(!i.ok){let n=await i.json().catch(()=>({}));throw new Error(n.error?.message||`HTTP ${i.status}: Failed to create checkout session`)}return i.json()}getApiBaseUrl(){let t=this.getAttribute("api-base-url");if(t)return t;if(typeof window<"u"){let r=window.location.hostname;if(r==="localhost"||r.includes(".test")||r.includes(".local")||r==="127.0.0.1")return`${window.location.protocol}//${window.location.host}/api`}return"https://api.recur.tw"}resolveUrl(t){return t.startsWith("/")?`${window.location.origin}${t}`:t}dispatchError(t){console.error("[recur-checkout]",t),this.dispatchEvent(new CustomEvent("checkout-error",{detail:{message:t},bubbles:!0,composed:!0}))}};typeof window<"u"&&!customElements.get("recur-checkout")&&customElements.define("recur-checkout",O)});var ye={};k(ye,{RecurPortalButton:()=>j});var j,ke=w(()=>{"use strict";j=class extends HTMLElement{constructor(){super();c(this,"_isLoading",!1);c(this,"handleClick",async t=>{if(t.preventDefault(),this._isLoading||this.hasAttribute("disabled"))return;let r=this.getAttribute("portal-url"),s=this.getAttribute("api-endpoint");if(r){this.redirectToPortal(r);return}if(s){await this.fetchAndRedirect(s);return}this.dispatchError("Missing required attribute: either portal-url or api-endpoint must be provided")});this.attachShadow({mode:"open"})}static get observedAttributes(){return["portal-url","api-endpoint","customer-id","return-url","button-text","button-style","disabled","target"]}connectedCallback(){this.render(),this.setupEventListeners()}disconnectedCallback(){let t=this.shadowRoot?.querySelector("button");t&&t.removeEventListener("click",this.handleClick)}attributeChangedCallback(t,r,s){r!==s&&this.render()}render(){let t=this.getAttribute("button-text")||this.textContent?.trim()||"\u7BA1\u7406\u8A02\u95B1",r=this.getAttribute("button-style")||"primary",s=this.hasAttribute("disabled")||this._isLoading;this.shadowRoot.innerHTML=`
1328
+ `}setupEventListeners(){let t=this.shadowRoot?.querySelector("button");t&&t.addEventListener("click",this.handleClick)}async createCheckoutSession(t){let r=this.getApiBaseUrl(),s={productId:t.productId,successUrl:t.successUrl,cancelUrl:t.cancelUrl};t.mode&&(s.mode=t.mode),t.customerEmail&&(s.customerEmail=t.customerEmail);let i=await fetch(`${r}/v1/checkout/sessions`,{method:"POST",headers:{"Content-Type":"application/json","X-Recur-Publishable-Key":t.publishableKey},body:JSON.stringify(s)});if(!i.ok){let n=await i.json().catch(()=>({}));throw new Error(n.error?.message||`HTTP ${i.status}: Failed to create checkout session`)}return i.json()}getApiBaseUrl(){let t=this.getAttribute("api-base-url");if(t)return t;if(typeof window<"u"){let r=window.location.hostname;if(r==="localhost"||r.includes(".test")||r.includes(".local")||r==="127.0.0.1")return`${window.location.protocol}//${window.location.host}/api`}return"https://api.recur.tw"}resolveUrl(t){return t.startsWith("/")?`${window.location.origin}${t}`:t}dispatchError(t){console.error("[recur-checkout]",t),this.dispatchEvent(new CustomEvent("checkout-error",{detail:{message:t},bubbles:!0,composed:!0}))}};typeof window<"u"&&!customElements.get("recur-checkout")&&customElements.define("recur-checkout",j)});var ke={};k(ke,{RecurPortalButton:()=>F});var F,ve=v(()=>{"use strict";F=class extends HTMLElement{constructor(){super();c(this,"_isLoading",!1);c(this,"handleClick",async t=>{if(t.preventDefault(),this._isLoading||this.hasAttribute("disabled"))return;let r=this.getAttribute("portal-url"),s=this.getAttribute("api-endpoint");if(r){this.redirectToPortal(r);return}if(s){await this.fetchAndRedirect(s);return}this.dispatchError("Missing required attribute: either portal-url or api-endpoint must be provided")});this.attachShadow({mode:"open"})}static get observedAttributes(){return["portal-url","api-endpoint","customer-id","return-url","button-text","button-style","disabled","target"]}connectedCallback(){this.render(),this.setupEventListeners()}disconnectedCallback(){let t=this.shadowRoot?.querySelector("button");t&&t.removeEventListener("click",this.handleClick)}attributeChangedCallback(t,r,s){r!==s&&this.render()}render(){let t=this.getAttribute("button-text")||this.textContent?.trim()||"\u7BA1\u7406\u8A02\u95B1",r=this.getAttribute("button-style")||"primary",s=this.hasAttribute("disabled")||this._isLoading;this.shadowRoot.innerHTML=`
1329
1329
  <style>
1330
1330
  :host {
1331
1331
  display: inline-block;
@@ -1452,7 +1452,7 @@
1452
1452
  <path d="M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"/>
1453
1453
  <circle cx="12" cy="7" r="4"/>
1454
1454
  </svg>
1455
- `}setupEventListeners(){let t=this.shadowRoot?.querySelector("button");t&&t.addEventListener("click",this.handleClick)}redirectToPortal(t){let r=this.getAttribute("target");this.dispatchEvent(new CustomEvent("portal-redirect",{detail:{url:t},bubbles:!0,composed:!0})),r==="_blank"?window.open(t,"_blank","noopener,noreferrer"):window.location.href=t}async fetchAndRedirect(t){let r=this.getAttribute("customer-id"),s=this.getAttribute("return-url");this._isLoading=!0,this.render(),this.setupEventListeners();try{let i={};r&&(i.customerId=r),s&&(i.returnUrl=s);let n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});if(!n.ok){let m=await n.json().catch(()=>({}));throw new Error(m.error?.message||m.message||`HTTP ${n.status}: Failed to create portal session`)}let o=await n.json(),d=o.url||o.portalUrl;if(!d)throw new Error("Portal URL not found in response");this._isLoading=!1,this.render(),this.setupEventListeners(),this.redirectToPortal(d)}catch(i){this._isLoading=!1,this.render(),this.setupEventListeners(),this.dispatchError(i.message||"Failed to create portal session")}}dispatchError(t){console.error("[recur-portal]",t),this.dispatchEvent(new CustomEvent("portal-error",{detail:{message:t},bubbles:!0,composed:!0}))}};typeof window<"u"&&!customElements.get("recur-portal")&&customElements.define("recur-portal",j)});var Oe={};k(Oe,{RecurCheckout:()=>U,RecurElements:()=>T,createElements:()=>G,default:()=>Be,init:()=>Ee});async function Ae(){if(typeof window>"u"){console.warn("[Recur SDK] Cannot register Web Components: window is not defined");return}await Promise.all([Promise.resolve().then(()=>(se(),re)),Promise.resolve().then(()=>(oe(),ie)),Promise.resolve().then(()=>(ae(),ne)),Promise.resolve().then(()=>(le(),ce)),Promise.resolve().then(()=>(ue(),de)),Promise.resolve().then(()=>(pe(),me)),Promise.resolve().then(()=>(fe(),he)),Promise.resolve().then(()=>(be(),ge)),Promise.resolve().then(()=>(ke(),ye))]);let e=["recur-loading-spinner","recur-success-message","recur-error-display","recur-skeleton-loader","recur-payment-form-skeleton","recur-toast","recur-toast-container","recur-payment-form","recur-checkout","recur-portal"].filter(t=>!customElements.get(t));e.length>0&&console.warn("[Recur SDK] The following components are not registered:",e)}typeof window<"u"&&Ae();function _e(a){return a.replace(/_([a-z])/g,(e,t)=>t.toUpperCase())}function b(a){if(a==null)return a;if(Array.isArray(a))return a.map(e=>b(e));if(a instanceof Date)return a;if(typeof a=="object"){let e={};for(let[t,r]of Object.entries(a)){let s=_e(t);e[s]=b(r)}return e}return a}var ve={name:"recur-tw",version:"0.9.2",description:"React & Vanilla JS SDK for embedding subscription checkout flows (Taiwan / PAYUNi)",type:"module",private:!1,author:"Recur",license:"Elastic-2.0",keywords:["react","vanilla-js","subscription","checkout","payment","payuni","recurring-billing","taiwan","\u7E41\u9AD4\u4E2D\u6587","sdk","embedded-checkout"],homepage:"https://recur.tw/",bugs:{email:"support@recur.tw"},scripts:{build:"tsup",dev:"tsup --watch",lint:"eslint .","lint:fix":"eslint . --fix","type-check":"tsc --noEmit",examples:"npx http-server examples -p 8080 -o"},main:"./dist/index.js",module:"./dist/index.js",types:"./dist/index.d.ts",exports:{".":{types:"./dist/index.d.ts",import:"./dist/index.js",require:"./dist/index.cjs"},"./server":{types:"./dist/server.d.ts",import:"./dist/server.js",require:"./dist/server.cjs"},"./vanilla":"./dist/recur.umd.js","./package.json":"./package.json"},unpkg:"./dist/recur.umd.js",jsdelivr:"./dist/recur.umd.js",files:["dist","README.md","AGENTS.md","LICENSE"],sideEffects:["src/components/**/*.ts","src/components/**/*.js"],peerDependencies:{react:">=18.0.0","react-dom":">=18.0.0"},peerDependenciesMeta:{react:{optional:!0},"react-dom":{optional:!0}},devDependencies:{"@eslint/js":"^9.39.1","@types/node":"^20.19.9","@types/react":"^19.1.9","@types/react-dom":"^19.1.7","@workspace/typescript-config":"workspace:*",autoprefixer:"^10.4.22",eslint:"^9","eslint-plugin-react":"^7.37.5","eslint-plugin-react-hooks":"^7.0.1",postcss:"^8.5.6",react:"^19.2.1","react-dom":"^19.2.1",tailwindcss:"^4.1.11",tsup:"^8.3.5",typescript:"^5.9.2","typescript-eslint":"^8.47.0"}};var $e=ve.version,He="vanilla",F=class{constructor(e){c(this,"config");if(!e.publishableKey)throw new Error("publishableKey is required");this.config={baseUrl:"https://api.recur.tw",...e}}getHeaders(){return{"Content-Type":"application/json","X-Recur-Publishable-Key":this.config.publishableKey,"X-Recur-SDK-Type":He,"X-Recur-SDK-Version":$e,"X-Recur-Source":typeof window<"u"?window.location.origin:"server"}}async createSubscription(e){let{customerName:t,customerEmail:r}=e,s=e.productId||e.planId,i=e.productSlug;if(!s&&!i)throw new Error("Either productId or productSlug is required");let n={customerName:t,customerEmail:r};s&&(n.productId=s),i&&(n.productSlug=i);let o=await fetch(`${this.config.baseUrl}/v1/subscriptions`,{method:"POST",headers:this.getHeaders(),body:JSON.stringify(n)});if(!o.ok){let m=await o.json().catch(()=>({}));throw{code:m.error||"CHECKOUT_FAILED",message:m.message||"Failed to initiate checkout",details:m}}let d=await o.json();return b(d)}async fetchProducts(e){let t=new URL(`${this.config.baseUrl}/v1/products`);e?.type&&t.searchParams.set("type",e.type);let r=await fetch(t.toString(),{method:"GET",headers:this.getHeaders()});if(!r.ok){let i=await r.json().catch(()=>({}));throw{code:i.error||"FETCH_PRODUCTS_FAILED",message:i.message||"Failed to fetch products",details:i}}let s=await r.json();return b(s)}async fetchPlans(){return{plans:(await this.fetchProducts({type:"SUBSCRIPTION"})).products}}getConfig(){return{...this.config}}};var q=class{constructor(e,t){c(this,"config");c(this,"options");c(this,"container");c(this,"checkoutId",null);c(this,"sdkToken",null);c(this,"sdkTimestamp",null);c(this,"creditToken",null);c(this,"sdkEnv","S");c(this,"payuniSDK",null);c(this,"isFormValid",!1);if(this.config=e,this.options=t,typeof t.container=="string"){let r=document.querySelector(t.container);if(!r)throw new Error(`Container not found: ${t.container}`);this.container=r}else this.container=t.container}async render(){try{await this.initCheckout(),this.renderHTML(),await this.loadPayUniSDK(),await this.initPayUniSDK(),this.setupFormSubmit()}catch(e){this.handleError(e)}}getBaseUrl(){if(this.config.baseUrl)return this.config.baseUrl;if(typeof window<"u"){let e=window.location.hostname;if(e==="localhost"||e.includes(".test")||e.includes(".local")||e==="127.0.0.1")return`${window.location.protocol}//${window.location.host}/api`}return"https://api.recur.tw"}async initCheckout(){let e=this.getBaseUrl(),t=await fetch(`${e}/v1/checkouts`,{method:"POST",headers:{"Content-Type":"application/json","X-Recur-Publishable-Key":this.config.publishableKey},body:JSON.stringify({productId:this.options.planId,customerEmail:this.options.customerEmail,customerName:this.options.customerName})});if(!t.ok){let i=await t.json().catch(()=>({}));throw new Error(i.error||"Failed to initialize checkout")}let r=await t.json(),s=b(r);this.checkoutId=s.checkout.id,this.sdkToken=s.sdkToken,this.sdkTimestamp=s.sdkTimestamp||null,this.creditToken=s.creditToken||null,this.sdkEnv=this.config.publishableKey.startsWith("pk_test_")?"S":"P"}renderHTML(){this.container.innerHTML=`
1455
+ `}setupEventListeners(){let t=this.shadowRoot?.querySelector("button");t&&t.addEventListener("click",this.handleClick)}redirectToPortal(t){let r=this.getAttribute("target");this.dispatchEvent(new CustomEvent("portal-redirect",{detail:{url:t},bubbles:!0,composed:!0})),r==="_blank"?window.open(t,"_blank","noopener,noreferrer"):window.location.href=t}async fetchAndRedirect(t){let r=this.getAttribute("customer-id"),s=this.getAttribute("return-url");this._isLoading=!0,this.render(),this.setupEventListeners();try{let i={};r&&(i.customerId=r),s&&(i.returnUrl=s);let n=await fetch(t,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)});if(!n.ok){let m=await n.json().catch(()=>({}));throw new Error(m.error?.message||m.message||`HTTP ${n.status}: Failed to create portal session`)}let o=await n.json(),d=o.url||o.portalUrl;if(!d)throw new Error("Portal URL not found in response");this._isLoading=!1,this.render(),this.setupEventListeners(),this.redirectToPortal(d)}catch(i){this._isLoading=!1,this.render(),this.setupEventListeners(),this.dispatchError(i.message||"Failed to create portal session")}}dispatchError(t){console.error("[recur-portal]",t),this.dispatchEvent(new CustomEvent("portal-error",{detail:{message:t},bubbles:!0,composed:!0}))}};typeof window<"u"&&!customElements.get("recur-portal")&&customElements.define("recur-portal",F)});var je={};k(je,{RecurCheckout:()=>L,RecurElements:()=>T,createElements:()=>Q,default:()=>Oe,init:()=>Se});async function _e(){if(typeof window>"u"){console.warn("[Recur SDK] Cannot register Web Components: window is not defined");return}await Promise.all([Promise.resolve().then(()=>(ie(),se)),Promise.resolve().then(()=>(ne(),oe)),Promise.resolve().then(()=>(ce(),ae)),Promise.resolve().then(()=>(de(),le)),Promise.resolve().then(()=>(me(),ue)),Promise.resolve().then(()=>(he(),pe)),Promise.resolve().then(()=>(ge(),fe)),Promise.resolve().then(()=>(ye(),be)),Promise.resolve().then(()=>(ve(),ke))]);let e=["recur-loading-spinner","recur-success-message","recur-error-display","recur-skeleton-loader","recur-payment-form-skeleton","recur-toast","recur-toast-container","recur-payment-form","recur-checkout","recur-portal"].filter(t=>!customElements.get(t));e.length>0&&console.warn("[Recur SDK] The following components are not registered:",e)}typeof window<"u"&&_e();function ze(a){return a.replace(/_([a-z])/g,(e,t)=>t.toUpperCase())}function g(a){if(a==null)return a;if(Array.isArray(a))return a.map(e=>g(e));if(a instanceof Date)return a;if(typeof a=="object"){let e={};for(let[t,r]of Object.entries(a)){let s=ze(t);e[s]=g(r)}return e}return a}var we={name:"recur-tw",version:"0.9.3",description:"React & Vanilla JS SDK for embedding subscription checkout flows (Taiwan / PAYUNi)",type:"module",private:!1,author:"Recur",license:"Elastic-2.0",keywords:["react","vanilla-js","subscription","checkout","payment","payuni","recurring-billing","taiwan","\u7E41\u9AD4\u4E2D\u6587","sdk","embedded-checkout"],homepage:"https://recur.tw/",bugs:{email:"support@recur.tw"},scripts:{build:"tsup",dev:"tsup --watch",lint:"eslint .","lint:fix":"eslint . --fix","type-check":"tsc --noEmit",examples:"npx http-server examples -p 8080 -o"},main:"./dist/index.js",module:"./dist/index.js",types:"./dist/index.d.ts",exports:{".":{types:"./dist/index.d.ts",import:"./dist/index.js",require:"./dist/index.cjs"},"./server":{types:"./dist/server.d.ts",import:"./dist/server.js",require:"./dist/server.cjs"},"./vanilla":"./dist/recur.umd.js","./package.json":"./package.json"},unpkg:"./dist/recur.umd.js",jsdelivr:"./dist/recur.umd.js",files:["dist","README.md","AGENTS.md","LICENSE"],sideEffects:["src/components/**/*.ts","src/components/**/*.js"],peerDependencies:{react:">=18.0.0","react-dom":">=18.0.0"},peerDependenciesMeta:{react:{optional:!0},"react-dom":{optional:!0}},devDependencies:{"@eslint/js":"^9.39.1","@types/node":"^20.19.9","@types/react":"^19.1.9","@types/react-dom":"^19.1.7","@workspace/typescript-config":"workspace:*",autoprefixer:"^10.4.22",eslint:"^9","eslint-plugin-react":"^7.37.5","eslint-plugin-react-hooks":"^7.0.1",postcss:"^8.5.6",react:"^19.2.1","react-dom":"^19.2.1",tailwindcss:"^4.1.11",tsup:"^8.3.5",typescript:"^5.9.2","typescript-eslint":"^8.47.0"}};var He=we.version,Ke="vanilla",q=class{constructor(e){c(this,"config");if(!e.publishableKey)throw new Error("publishableKey is required");this.config={baseUrl:"https://api.recur.tw",...e}}getHeaders(){return{"Content-Type":"application/json","X-Recur-Publishable-Key":this.config.publishableKey,"X-Recur-SDK-Type":Ke,"X-Recur-SDK-Version":He,"X-Recur-Source":typeof window<"u"?window.location.origin:"server"}}async createSubscription(e){let{customerName:t,customerEmail:r}=e,s=e.productId||e.planId,i=e.productSlug;if(!s&&!i)throw new Error("Either productId or productSlug is required");let n={customerName:t,customerEmail:r};s&&(n.productId=s),i&&(n.productSlug=i);let o=await fetch(`${this.config.baseUrl}/v1/subscriptions`,{method:"POST",headers:this.getHeaders(),body:JSON.stringify(n)});if(!o.ok){let m=await o.json().catch(()=>({}));throw{code:m.error||"CHECKOUT_FAILED",message:m.message||"Failed to initiate checkout",details:m}}let d=await o.json();return g(d)}async fetchProducts(e){let t=new URL(`${this.config.baseUrl}/v1/products`);e?.type&&t.searchParams.set("type",e.type);let r=await fetch(t.toString(),{method:"GET",headers:this.getHeaders()});if(!r.ok){let i=await r.json().catch(()=>({}));throw{code:i.error||"FETCH_PRODUCTS_FAILED",message:i.message||"Failed to fetch products",details:i}}let s=await r.json();return g(s)}async fetchPlans(){return{plans:(await this.fetchProducts({type:"SUBSCRIPTION"})).products}}getConfig(){return{...this.config}}};var V=class{constructor(e,t){c(this,"config");c(this,"options");c(this,"container");c(this,"checkoutId",null);c(this,"sdkToken",null);c(this,"sdkTimestamp",null);c(this,"creditToken",null);c(this,"sdkEnv","S");c(this,"payuniSDK",null);c(this,"isFormValid",!1);if(this.config=e,this.options=t,typeof t.container=="string"){let r=document.querySelector(t.container);if(!r)throw new Error(`Container not found: ${t.container}`);this.container=r}else this.container=t.container}async render(){try{await this.initCheckout(),this.renderHTML(),await this.loadPayUniSDK(),await this.initPayUniSDK(),this.setupFormSubmit()}catch(e){this.handleError(e)}}getBaseUrl(){if(this.config.baseUrl)return this.config.baseUrl;if(typeof window<"u"){let e=window.location.hostname;if(e==="localhost"||e.includes(".test")||e.includes(".local")||e==="127.0.0.1")return`${window.location.protocol}//${window.location.host}/api`}return"https://api.recur.tw"}async initCheckout(){let e=this.getBaseUrl(),t=await fetch(`${e}/v1/checkouts`,{method:"POST",headers:{"Content-Type":"application/json","X-Recur-Publishable-Key":this.config.publishableKey},body:JSON.stringify({productId:this.options.planId,customerEmail:this.options.customerEmail,customerName:this.options.customerName})});if(!t.ok){let i=await t.json().catch(()=>({}));throw new Error(i.error||"Failed to initialize checkout")}let r=await t.json(),s=g(r);this.checkoutId=s.checkout.id,this.sdkToken=s.sdkToken,this.sdkTimestamp=s.sdkTimestamp||null,this.creditToken=s.creditToken||null,this.sdkEnv=this.config.publishableKey.startsWith("pk_test_")?"S":"P"}renderHTML(){this.container.innerHTML=`
1456
1456
  <div class="recur-embedded-checkout" style="max-width: 400px; margin: 0 auto; padding: 20px; font-family: system-ui, -apple-system, sans-serif;">
1457
1457
  <div class="recur-checkout-header" style="margin-bottom: 24px;">
1458
1458
  <h2 style="margin: 0 0 8px 0; font-size: 20px; font-weight: 600; color: #111;">Subscribe</h2>
@@ -1529,14 +1529,14 @@
1529
1529
  display: block;
1530
1530
  user-select: none;
1531
1531
  transition: height 0.35s ease, opacity 0.4s ease 0.1s;
1532
- `.replace(/\s+/g," ").trim(),this.iframe.allow="payment *",this.iframe.setAttribute("title","Secure payment input frame"),this.iframe.setAttribute("role","presentation"),this.iframe.setAttribute("scrolling","no"),this.iframe.setAttribute("frameBorder","0"),this.container.innerHTML="",this.container.appendChild(this.iframe),new Promise((r,s)=>{let i=setTimeout(()=>{s(new Error("Elements initialization timeout"))},3e4),n=()=>{clearTimeout(i),this.off("ready",n),r()},o=d=>{clearTimeout(i),this.off("error",o),s(new Error(d.message||"Elements initialization failed"))};this.on("ready",n),this.on("error",o)})}on(e,t){this.eventHandlers.has(e)||this.eventHandlers.set(e,new Set),this.eventHandlers.get(e).add(t)}off(e,t){let r=this.eventHandlers.get(e);r&&r.delete(t)}emit(e,t){let r=this.eventHandlers.get(e);r&&r.forEach(s=>s(t))}handleMessage(e){if(!this.isValidOrigin(e.origin))return;let{type:t,data:r}=e.data||{};switch(t){case"RECUR_ELEMENTS_READY":this.sessionId=r.sessionId,this.timestamp=r.timestamp,this.creditToken=r.creditToken,this.sdkToken=r.sdkToken,this.emit("ready",r);break;case"RECUR_CARD_VALID":this.emit("change",{valid:r.valid});break;case"RECUR_CARD_TOKENIZED":this.cardToken=r.cardToken,this.cardTimestamp=r.timestamp,this.emit("tokenized",r);break;case"RECUR_PAYMENT_ERROR":this.emit("error",r);break;case"RECUR_RESIZE":this.iframe&&r?.height&&(this.iframe.style.height=`${r.height}px`);break}}isValidOrigin(e){try{let t=new URL(this.embedUrl).origin;return e===t}catch{return!1}}async tokenize(){if(!this.iframe||!this.iframe.contentWindow)throw new Error("Elements not mounted");let e=new URL(this.embedUrl).origin;return this.iframe.contentWindow.postMessage({type:"RECUR_TOKENIZE"},e),new Promise((t,r)=>{let s=setTimeout(()=>{r(new Error("Tokenization timeout"))},6e4),i=o=>{clearTimeout(s),this.off("tokenized",i),t(o)},n=o=>{clearTimeout(s),this.off("error",n),r(new Error(o.message||"Tokenization failed"))};this.on("tokenized",i),this.on("error",n)})}async confirmPayment(e){if(!this.sessionId||!this.cardToken)throw new Error("Card not tokenized. Call tokenize() first.");let t=await fetch(`${this.baseUrl}/api/v1/elements/confirm`,{method:"POST",headers:{"Content-Type":"application/json","X-Recur-Publishable-Key":this.publishableKey},body:JSON.stringify({sessionId:this.sessionId,productId:e.productId,email:e.email,name:e.name,phone:e.phone,externalCustomerId:e.externalCustomerId,metadata:e.metadata,successUrl:e.successUrl,cancelUrl:e.cancelUrl})}),r=await t.json();return t.ok?r:{status:"failed",message:r.error||r.message||"Payment failed",canRetry:r.canRetry??!0}}getDefaultBaseUrl(){if(typeof window>"u")return"https://app.recur.tw";let e=window.location.hostname;return e==="localhost"||e.includes(".test")||e.includes(".local")||e==="127.0.0.1"?`${window.location.protocol}//${e}:3000`:"https://app.recur.tw"}getDefaultEmbedUrl(){if(typeof window>"u")return"https://embed.recur.tw";let e=window.location.hostname;return e==="localhost"||e.includes(".test")||e.includes(".local")||e==="127.0.0.1"?`${window.location.protocol}//${e}:3002`:"https://embed.recur.tw"}destroy(){this.iframe&&this.iframe.parentNode&&this.iframe.parentNode.removeChild(this.iframe),this.iframe=null,this.container=null,this.sessionId=null,this.timestamp=null,this.creditToken=null,this.sdkToken=null,this.cardToken=null,this.cardTimestamp=null,this.eventHandlers.clear(),window.removeEventListener("message",this.handleMessage.bind(this))}};function G(a){return new T(a)}var Ke="https://vendor.payuni.com.tw/sdk/uni-payment.js",Ne="https://sandbox-vendor.payuni.com.tw/sdk/uni-payment.js",we=!1,V=!1,L=null;async function xe(a=!1){return we&&window.UniPayment?Promise.resolve():(V&&L||(V=!0,L=new Promise((e,t)=>{let r=document.createElement("script");r.src=a?Ne:Ke,r.async=!0,r.onload=()=>{we=!0,V=!1,e()},r.onerror=()=>{V=!1,L=null,t(new Error("Failed to load PAYUNi SDK"))},document.head.appendChild(r)})),L)}var U=class{constructor(e){c(this,"core");c(this,"currentModal",null);c(this,"currentIframe",null);c(this,"currentModalOverlay",null);this.core=new F(e)}async fetchProducts(e){return await this.core.fetchProducts(e)}async fetchPlans(){return await this.core.fetchPlans()}async createEmbeddedCheckout(e){let t=this.core.getConfig();await new q(t,e).render()}async redirectToCheckout(e){let t=await this.createCheckoutSession(e);window.location.href=t.url}async createCheckoutSession(e){let t=this.getBaseUrl();if(!e.productId&&!e.productSlug)throw new Error("Either productId or productSlug is required");if(!e.successUrl)throw new Error("successUrl is required for hosted checkout");if(!e.cancelUrl)throw new Error("cancelUrl is required for hosted checkout");let r={successUrl:e.successUrl,cancelUrl:e.cancelUrl};e.productId&&(r.productId=e.productId),e.productSlug&&(r.productSlug=e.productSlug),e.customerEmail&&(r.customerEmail=e.customerEmail),e.customerName&&(r.customerName=e.customerName),e.externalCustomerId&&(r.externalCustomerId=e.externalCustomerId);let s=this.core.getConfig(),i=await fetch(`${t}/v1/checkout/sessions`,{method:"POST",headers:{"Content-Type":"application/json","X-Recur-Publishable-Key":s.publishableKey},body:JSON.stringify(r)});if(!i.ok){let d=await i.json().catch(()=>({}));throw new Error(d.error?.message||d.error||"Failed to create checkout session")}let n=await i.json(),o=b(n);return{id:o.id,url:o.url,expiresAt:o.expiresAt,clientSecret:o.clientSecret}}async getCheckoutStatus(e,t){let r=this.getBaseUrl(),s=this.core.getConfig(),i=await fetch(`${r}/v1/checkouts/${e}?client_secret=${encodeURIComponent(t)}`,{method:"GET",headers:{"X-Recur-Publishable-Key":s.publishableKey}});if(!i.ok){let d=await i.json().catch(()=>({}));throw new Error(d.error||"Failed to get checkout status")}let n=await i.json(),o=b(n);return{id:o.checkout.id,status:o.checkout.status,amount:o.checkout.amount,currency:o.checkout.currency,lastCharge:o.lastCharge}}async checkout(e){let t=this.core.getConfig(),r=null,s=e.productId||e.planId,i=e.productSlug;try{if(console.log("[Recur SDK] Starting checkout flow...",{productId:s,productSlug:i,mode:e.mode}),!s&&!i)throw new Error("Either productId or productSlug is required");if(!t.publishableKey)throw new Error("publishableKey is required");let n=this.getBaseUrl();console.log("[Recur SDK] Base URL:",n);let o={"Content-Type":"application/json","X-Recur-Publishable-Key":t.publishableKey},d=e.mode||"modal";if(d==="redirect"){if(!e.successUrl)throw new Error("successUrl is required for redirect mode");if(!e.cancelUrl)throw new Error("cancelUrl is required for redirect mode");console.log("[Recur SDK] Creating hosted checkout session...");let h={successUrl:e.successUrl,cancelUrl:e.cancelUrl};s&&(h.productId=s),i&&(h.productSlug=i),e.customerEmail&&(h.customerEmail=e.customerEmail),e.customerName&&(h.customerName=e.customerName),e.externalCustomerId&&(h.externalCustomerId=e.externalCustomerId);let S=await fetch(`${n}/v1/checkout/sessions`,{method:"POST",headers:o,body:JSON.stringify(h)});if(!S.ok){let C=await S.json().catch(()=>({}));throw console.error("[Recur SDK] Failed to create checkout session:",C),new Error(C.error?.message||C.error||"Failed to create checkout session")}let J=await S.json(),y=b(J);console.log("[Recur SDK] Checkout session created:",y),console.log("[Recur SDK] Redirecting to hosted checkout:",y.url),window.location.href=y.url;return}let m=null;if(d==="modal"){let h=this.createModalWithSkeleton(e.onClose);r=h.overlay,m=h.container}else if(d==="iframe"){if(m=this.getEmbeddedContainer(e.container),!m)throw new Error("Container is required for iframe mode");m.innerHTML="";let h=document.createElement("recur-payment-form-skeleton");m.appendChild(h)}console.log("[Recur SDK] Step 1: Creating embedded checkout...");let E={customerName:e.customerName,customerEmail:e.customerEmail};s&&(E.productId=s),i&&(E.productSlug=i),e.externalCustomerId&&(E.externalCustomerId=e.externalCustomerId);let Y=await fetch(`${n}/v1/checkouts`,{method:"POST",headers:o,body:JSON.stringify(E)});if(!Y.ok){let h=await Y.json().catch(()=>({}));console.error("[Recur SDK] Failed to create checkout:",h);let S=h.details||h.error||"Failed to create checkout";throw new Error(S)}let Se=await Y.json(),l=b(Se);if(console.log("[Recur SDK] Checkout created successfully:",l),e.onSuccess?.(l),console.log("[Recur SDK] Step 2: Extracting SDK token..."),!l.sdkToken)throw new Error("SDK token missing from checkout response");console.log("[Recur SDK] Step 3: Loading PAYUNi SDK...");let X=!l.livemode;if(console.log("[Recur SDK] Environment:",X?"SANDBOX":"PRODUCTION"),await xe(X),console.log("[Recur SDK] PAYUNi SDK loaded successfully"),!window.UniPayment)throw new Error("PAYUNi SDK failed to load");if(console.log("[Recur SDK] Step 4: Rendering payment form..."),!m)throw new Error("Payment container not available");m.innerHTML="";let p=document.createElement("recur-payment-form");if(p.setAttribute("container-id",m.id||"recur-payment-container"),e.customerName&&p.setAttribute("customer-name",e.customerName),e.customerEmail&&p.setAttribute("customer-email",e.customerEmail),l.plan?.name&&p.setAttribute("plan-name",l.plan.name),l.checkout?.amount&&p.setAttribute("amount",l.checkout.amount.toString()),l.plan?.billingPeriod&&p.setAttribute("billing-period",l.plan.billingPeriod),p.setAttribute("custom-styles",`
1532
+ `.replace(/\s+/g," ").trim(),this.iframe.allow="payment *",this.iframe.setAttribute("title","Secure payment input frame"),this.iframe.setAttribute("role","presentation"),this.iframe.setAttribute("scrolling","no"),this.iframe.setAttribute("frameBorder","0"),this.container.innerHTML="",this.container.appendChild(this.iframe),new Promise((r,s)=>{let i=setTimeout(()=>{s(new Error("Elements initialization timeout"))},3e4),n=()=>{clearTimeout(i),this.off("ready",n),r()},o=d=>{clearTimeout(i),this.off("error",o),s(new Error(d.message||"Elements initialization failed"))};this.on("ready",n),this.on("error",o)})}on(e,t){this.eventHandlers.has(e)||this.eventHandlers.set(e,new Set),this.eventHandlers.get(e).add(t)}off(e,t){let r=this.eventHandlers.get(e);r&&r.delete(t)}emit(e,t){let r=this.eventHandlers.get(e);r&&r.forEach(s=>s(t))}handleMessage(e){if(!this.isValidOrigin(e.origin))return;let{type:t,data:r}=e.data||{};switch(t){case"RECUR_ELEMENTS_READY":this.sessionId=r.sessionId,this.timestamp=r.timestamp,this.creditToken=r.creditToken,this.sdkToken=r.sdkToken,this.emit("ready",r);break;case"RECUR_CARD_VALID":this.emit("change",{valid:r.valid});break;case"RECUR_CARD_TOKENIZED":this.cardToken=r.cardToken,this.cardTimestamp=r.timestamp,this.emit("tokenized",r);break;case"RECUR_PAYMENT_ERROR":this.emit("error",r);break;case"RECUR_RESIZE":this.iframe&&r?.height&&(this.iframe.style.height=`${r.height}px`);break}}isValidOrigin(e){try{let t=new URL(this.embedUrl).origin;return e===t}catch{return!1}}async tokenize(){if(!this.iframe||!this.iframe.contentWindow)throw new Error("Elements not mounted");let e=new URL(this.embedUrl).origin;return this.iframe.contentWindow.postMessage({type:"RECUR_TOKENIZE"},e),new Promise((t,r)=>{let s=setTimeout(()=>{r(new Error("Tokenization timeout"))},6e4),i=o=>{clearTimeout(s),this.off("tokenized",i),t(o)},n=o=>{clearTimeout(s),this.off("error",n),r(new Error(o.message||"Tokenization failed"))};this.on("tokenized",i),this.on("error",n)})}async confirmPayment(e){if(!this.sessionId||!this.cardToken)throw new Error("Card not tokenized. Call tokenize() first.");let t=await fetch(`${this.baseUrl}/api/v1/elements/confirm`,{method:"POST",headers:{"Content-Type":"application/json","X-Recur-Publishable-Key":this.publishableKey},body:JSON.stringify({sessionId:this.sessionId,productId:e.productId,email:e.email,name:e.name,phone:e.phone,externalCustomerId:e.externalCustomerId,metadata:e.metadata,successUrl:e.successUrl,cancelUrl:e.cancelUrl})}),r=await t.json();return t.ok?r:{status:"failed",message:r.error||r.message||"Payment failed",canRetry:r.canRetry??!0}}getDefaultBaseUrl(){if(typeof window>"u")return"https://app.recur.tw";let e=window.location.hostname;return e==="localhost"||e.includes(".test")||e.includes(".local")||e==="127.0.0.1"?`${window.location.protocol}//${e}:3000`:"https://app.recur.tw"}getDefaultEmbedUrl(){if(typeof window>"u")return"https://embed.recur.tw";let e=window.location.hostname;return e==="localhost"||e.includes(".test")||e.includes(".local")||e==="127.0.0.1"?`${window.location.protocol}//${e}:3002`:"https://embed.recur.tw"}destroy(){this.iframe&&this.iframe.parentNode&&this.iframe.parentNode.removeChild(this.iframe),this.iframe=null,this.container=null,this.sessionId=null,this.timestamp=null,this.creditToken=null,this.sdkToken=null,this.cardToken=null,this.cardTimestamp=null,this.eventHandlers.clear(),window.removeEventListener("message",this.handleMessage.bind(this))}};function Q(a){return new T(a)}var Be="https://vendor.payuni.com.tw/sdk/uni-payment.js",Ne="https://sandbox-vendor.payuni.com.tw/sdk/uni-payment.js",xe=!1,Y=!1,M=null;async function Ee(a=!1){return xe&&window.UniPayment?Promise.resolve():(Y&&M||(Y=!0,M=new Promise((e,t)=>{let r=document.createElement("script");r.src=a?Ne:Be,r.async=!0,r.onload=()=>{xe=!0,Y=!1,e()},r.onerror=()=>{Y=!1,M=null,t(new Error("Failed to load PAYUNi SDK"))},document.head.appendChild(r)})),M)}var L=class{constructor(e){c(this,"core");c(this,"currentModal",null);c(this,"currentIframe",null);c(this,"currentModalOverlay",null);this.core=new q(e)}async fetchProducts(e){return await this.core.fetchProducts(e)}async fetchPlans(){return await this.core.fetchPlans()}async createEmbeddedCheckout(e){let t=this.core.getConfig();await new V(t,e).render()}async redirectToCheckout(e){let t=await this.createCheckoutSession(e);window.location.href=t.url}async createCheckoutSession(e){let t=this.getBaseUrl();if(!e.productId&&!e.productSlug)throw new Error("Either productId or productSlug is required");if(!e.successUrl)throw new Error("successUrl is required for hosted checkout");if(!e.cancelUrl)throw new Error("cancelUrl is required for hosted checkout");let r={successUrl:e.successUrl,cancelUrl:e.cancelUrl};e.productId&&(r.productId=e.productId),e.productSlug&&(r.productSlug=e.productSlug),e.customerEmail&&(r.customerEmail=e.customerEmail),e.customerName&&(r.customerName=e.customerName),e.externalCustomerId&&(r.externalCustomerId=e.externalCustomerId);let s=this.core.getConfig(),i=await fetch(`${t}/v1/checkout/sessions`,{method:"POST",headers:{"Content-Type":"application/json","X-Recur-Publishable-Key":s.publishableKey},body:JSON.stringify(r)});if(!i.ok){let d=await i.json().catch(()=>({}));throw new Error(d.error?.message||d.error||"Failed to create checkout session")}let n=await i.json(),o=g(n);return{id:o.id,url:o.url,expiresAt:o.expiresAt,clientSecret:o.clientSecret}}async getCheckoutStatus(e,t){let r=this.getBaseUrl(),s=this.core.getConfig(),i=await fetch(`${r}/v1/checkouts/${e}?client_secret=${encodeURIComponent(t)}`,{method:"GET",headers:{"X-Recur-Publishable-Key":s.publishableKey}});if(!i.ok){let d=await i.json().catch(()=>({}));throw new Error(d.error||"Failed to get checkout status")}let n=await i.json(),o=g(n);return{id:o.checkout.id,status:o.checkout.status,amount:o.checkout.amount,currency:o.checkout.currency,lastCharge:o.lastCharge}}async checkout(e){let t=this.core.getConfig(),r=null,s=e.productId||e.planId,i=e.productSlug;try{if(console.log("[Recur SDK] Starting checkout flow...",{productId:s,productSlug:i,mode:e.mode}),!s&&!i)throw new Error("Either productId or productSlug is required");if(!t.publishableKey)throw new Error("publishableKey is required");let n=this.getBaseUrl();console.log("[Recur SDK] Base URL:",n);let o={"Content-Type":"application/json","X-Recur-Publishable-Key":t.publishableKey},d=e.mode||"modal";if(d==="redirect"){if(!e.successUrl)throw new Error("successUrl is required for redirect mode");if(!e.cancelUrl)throw new Error("cancelUrl is required for redirect mode");console.log("[Recur SDK] Creating hosted checkout session...");let h={successUrl:e.successUrl,cancelUrl:e.cancelUrl};s&&(h.productId=s),i&&(h.productSlug=i),e.customerEmail&&(h.customerEmail=e.customerEmail),e.customerName&&(h.customerName=e.customerName),e.externalCustomerId&&(h.externalCustomerId=e.externalCustomerId);let S=await fetch(`${n}/v1/checkout/sessions`,{method:"POST",headers:o,body:JSON.stringify(h)});if(!S.ok){let C=await S.json().catch(()=>({}));throw console.error("[Recur SDK] Failed to create checkout session:",C),new Error(C.error?.message||C.error||"Failed to create checkout session")}let J=await S.json(),y=g(J);console.log("[Recur SDK] Checkout session created:",y),console.log("[Recur SDK] Redirecting to hosted checkout:",y.url),window.location.href=y.url;return}let m=null;if(d==="modal"){let h=this.createModalWithSkeleton(e.onClose);r=h.overlay,m=h.container}else if(d==="iframe"){if(m=this.getEmbeddedContainer(e.container),!m)throw new Error("Container is required for iframe mode");m.innerHTML="";let h=document.createElement("recur-payment-form-skeleton");m.appendChild(h)}console.log("[Recur SDK] Step 1: Creating embedded checkout...");let x={customerName:e.customerName,customerEmail:e.customerEmail};s&&(x.productId=s),i&&(x.productSlug=i),e.externalCustomerId&&(x.externalCustomerId=e.externalCustomerId);let X=await fetch(`${n}/v1/checkouts`,{method:"POST",headers:o,body:JSON.stringify(x)});if(!X.ok){let h=await X.json().catch(()=>({}));console.error("[Recur SDK] Failed to create checkout:",h);let S=h.details||h.error||"Failed to create checkout";throw new Error(S)}let Ce=await X.json(),l=g(Ce);if(console.log("[Recur SDK] Checkout created successfully:",l),e.onSuccess?.(l),console.log("[Recur SDK] Step 2: Extracting SDK token..."),!l.sdkToken)throw new Error("SDK token missing from checkout response");console.log("[Recur SDK] Step 3: Loading PAYUNi SDK...");let W=!l.livemode;if(console.log("[Recur SDK] Environment:",W?"SANDBOX":"PRODUCTION"),await Ee(W),console.log("[Recur SDK] PAYUNi SDK loaded successfully"),!window.UniPayment)throw new Error("PAYUNi SDK failed to load");if(console.log("[Recur SDK] Step 4: Rendering payment form..."),!m)throw new Error("Payment container not available");m.innerHTML="";let p=document.createElement("recur-payment-form");if(p.setAttribute("container-id",m.id||"recur-payment-container"),e.customerName&&p.setAttribute("customer-name",e.customerName),e.customerEmail&&p.setAttribute("customer-email",e.customerEmail),l.plan?.name&&p.setAttribute("plan-name",l.plan.name),l.checkout?.amount&&p.setAttribute("amount",l.checkout.amount.toString()),l.plan?.billingPeriod&&p.setAttribute("billing-period",l.plan.billingPeriod),p.setAttribute("custom-styles",`
1533
1533
  .form-input-focus {
1534
1534
  border-color: var(--ring, hsl(215 16% 47%)) !important;
1535
1535
  outline: 0 !important;
1536
1536
  box-shadow: 0 0 0 3px color-mix(in oklch, var(--ring, hsl(215 16% 47%)) 50%, transparent) !important;
1537
1537
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out !important;
1538
1538
  }
1539
- `),m.appendChild(p),await new Promise(h=>setTimeout(h,100)),console.log("[Recur SDK] Step 5: Initializing PAYUNi SDK..."),await p.initializePayment(l.sdkToken,X?"SANDBOX":"PRODUCTION")===null){console.log("[Recur SDK] Initialization aborted");return}console.log("[Recur SDK] PAYUNi SDK initialized successfully"),console.log("[Recur SDK] Step 6: Setting up submit handler..."),p.addEventListener("submit",(async h=>{console.log("[Recur SDK] Form submitted");let S=h,{paymentSession:J}=S.detail;try{let y=await J.getTradeResult();console.log("[Recur SDK] Trade result received"),console.log("[Recur SDK] Executing payment...");let C=y.HashTimestamp||y.timestamp,Z={};if(l.checkout.productType==="SUBSCRIPTION"){let f=l.creditToken,v=l.sdkTimestamp;if(!f)throw console.error("[Recur SDK] Missing creditToken from checkout for subscription"),new Error("Missing creditToken for subscription payment");Z={creditToken:f,timestamp:v||C},console.log("[Recur SDK] Using creditToken from checkout:",f.substring(0,30)+"..."),console.log("[Recur SDK] Using timestamp:",v?"from checkout (sdkTimestamp)":"from tradeResult")}let W=await fetch(`${n}/v1/checkouts/${l.checkout.id}/pay`,{method:"POST",headers:o,body:JSON.stringify(Z)});if(!W.ok){let f=await W.json().catch(()=>({}));throw new Error(f.error||"Failed to execute payment")}let Ce=await W.json(),u=b(Ce);if(console.log("[Recur SDK] Payment executed:",u),u.requires3D&&u.redirectUrl){console.log("[Recur SDK] 3D verification required"),console.log("[Recur SDK] Using popup for 3D verification");{let f=window.open(u.redirectUrl,"recur_3d_verification","width=500,height=700,scrollbars=yes,resizable=yes");if(!f){console.log("[Recur SDK] Popup was blocked (null), falling back to redirect"),window.location.href=u.redirectUrl;return}if(await new Promise(R=>setTimeout(R,100)),f.closed){console.log("[Recur SDK] Popup was closed immediately, falling back to redirect"),window.location.href=u.redirectUrl;return}p.setVerifying?.(!0);let v=l.checkout.id,Q=l.checkout.clientSecret,Te=90,Ie=2e3,I=!0,ee=!1,te;console.log("[Recur SDK] Starting 3D verification polling...");for(let R=0;R<Te&&I;R++){if(f.closed){console.log("[Recur SDK] Popup was closed");try{let g=await fetch(`${n}/v1/checkouts/${v}/status?client_secret=${encodeURIComponent(Q)}`,{headers:o});if(g.ok){let P=await g.json();if(P.checkout?.status==="SUCCEEDED"){console.log("[Recur SDK] Payment succeeded after popup close"),I=!1,ee=!0,te=P.checkout?.orderId;break}}}catch(g){console.error("[Recur SDK] Final status check failed:",g)}throw console.log("[Recur SDK] Payment not confirmed, user closed popup"),p.setVerifying?.(!1),p.resetButton?.(),new Error("3D \u9A57\u8B49\u5DF2\u53D6\u6D88")}try{let g=await fetch(`${n}/v1/checkouts/${v}/status?client_secret=${encodeURIComponent(Q)}`,{headers:o});if(g.ok){let P=await g.json(),M=P.checkout?.status;if(console.log(`[Recur SDK] Poll ${R+1}: status = ${M}`),M==="SUCCEEDED"){console.log("[Recur SDK] Payment succeeded"),I=!1;try{f.close()}catch{}p.setVerifying?.(!1),e.onPaymentComplete&&(u.subscription?e.onPaymentComplete({id:u.subscription.id,status:"ACTIVE",planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:u.subscription.billingPeriod,currentPeriodStart:u.subscription.currentPeriodStart,currentPeriodEnd:u.subscription.currentPeriodEnd}):e.onPaymentComplete({id:P.checkout?.orderId||v,status:"SUCCEEDED",planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:l.checkout.productType})),r&&r.remove();return}if(M==="CANCELED"||M==="FAILED"){console.log("[Recur SDK] Payment failed or canceled"),I=!1;try{f.close()}catch{}throw p.setVerifying?.(!1),p.resetButton?.(),new Error("\u4ED8\u6B3E\u5931\u6557\u6216\u5DF2\u53D6\u6D88")}}}catch(g){if(g.message==="3D \u9A57\u8B49\u5DF2\u53D6\u6D88"||g.message==="\u4ED8\u6B3E\u5931\u6557\u6216\u5DF2\u53D6\u6D88")throw g;console.error("[Recur SDK] Poll error:",g)}await new Promise(g=>setTimeout(g,Ie))}if(I){console.log("[Recur SDK] 3D verification polling timeout");try{f.close()}catch{}throw p.setVerifying?.(!1),p.resetButton?.(),new Error("3D \u9A57\u8B49\u903E\u6642\uFF0C\u8ACB\u91CD\u8A66")}ee&&(console.log("[Recur SDK] Handling success after popup close"),p.setVerifying?.(!1),e.onPaymentComplete&&(u.subscription?e.onPaymentComplete({id:u.subscription.id,status:"ACTIVE",planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:u.subscription.billingPeriod,currentPeriodStart:u.subscription.currentPeriodStart,currentPeriodEnd:u.subscription.currentPeriodEnd}):e.onPaymentComplete({id:te||v,status:"SUCCEEDED",planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:l.checkout.productType})),r&&r.remove());return}}if(e.onPaymentComplete)if(u.subscription)e.onPaymentComplete({id:u.subscription.id,status:u.subscription.status,planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:u.subscription.billingPeriod,currentPeriodStart:u.subscription.currentPeriodStart,currentPeriodEnd:u.subscription.currentPeriodEnd});else{let f=u.charge?.id||u.paymentIntent?.id,v=u.charge?.status||"SUCCEEDED";e.onPaymentComplete({id:f,status:v,planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:l.checkout.productType})}console.log("[Recur SDK] Checkout flow completed successfully!"),p.resetButton?.(),r&&r.remove()}catch(y){console.error("[Recur SDK] Payment error:",y);let C={code:"PAYMENT_FAILED",message:y instanceof Error?y.message:"Payment failed"};e.onError?.(C),p.resetButton?.()}})),console.log("[Recur SDK] Checkout flow initialized, waiting for user input...")}catch(n){console.error("[Recur SDK] Checkout error:",n),r&&r.remove();let o={code:"CHECKOUT_ERROR",message:n instanceof Error?n.message:"An unknown error occurred"};throw e.onError?.(o),n}}getBaseUrl(){let e=this.core.getConfig();if(e.baseUrl)return e.baseUrl;if(typeof window<"u"){let t=window.location.hostname;if(t==="localhost"||t.includes(".test")||t.includes(".local")||t==="127.0.0.1")return`${window.location.protocol}//${window.location.host}/api`}return"https://api.recur.tw"}createModalWithSkeleton(e){this.closeModal();let t=document.createElement("div");t.id="recur-modal-overlay",t.style.cssText=`
1539
+ `),m.appendChild(p),await new Promise(h=>setTimeout(h,100)),console.log("[Recur SDK] Step 5: Initializing PAYUNi SDK..."),await p.initializePayment(l.sdkToken,W?"SANDBOX":"PRODUCTION")===null){console.log("[Recur SDK] Initialization aborted");return}console.log("[Recur SDK] PAYUNi SDK initialized successfully"),console.log("[Recur SDK] Step 6: Setting up submit handler..."),p.addEventListener("submit",(async h=>{console.log("[Recur SDK] Form submitted");let S=h,{paymentSession:J}=S.detail;try{let y=await J.getTradeResult();console.log("[Recur SDK] Trade result received"),console.log("[Recur SDK] Executing payment...");let C=y.HashTimestamp||y.timestamp,Z={};if(l.checkout.productType==="SUBSCRIPTION"){let E=l.creditToken,b=l.sdkTimestamp;if(!E)throw console.error("[Recur SDK] Missing creditToken from checkout for subscription"),new Error("Missing creditToken for subscription payment");Z={creditToken:E,timestamp:b||C},console.log("[Recur SDK] Using creditToken from checkout:",E.substring(0,30)+"..."),console.log("[Recur SDK] Using timestamp:",b?"from checkout (sdkTimestamp)":"from tradeResult")}let G=await fetch(`${n}/v1/checkouts/${l.checkout.id}/pay`,{method:"POST",headers:o,body:JSON.stringify(Z)});if(!G.ok){let E=await G.json().catch(()=>({}));throw new Error(E.error||"Failed to execute payment")}let Te=await G.json(),u=g(Te);if(console.log("[Recur SDK] Payment executed:",u),u.requires3D&&u.redirectUrl){if(console.log("[Recur SDK] 3D verification required"),/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)||/wv|WebView|FBAN|FBAV|Instagram|Line|MicroMessenger|QQ/i.test(navigator.userAgent)||/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent)||"standalone"in navigator&&navigator.standalone){console.log("[Recur SDK] Mobile/WebView detected, using redirect for 3D verification"),window.location.href=u.redirectUrl;return}console.log("[Recur SDK] Using popup for 3D verification");{let b=window.open(u.redirectUrl,"recur_3d_verification","width=500,height=700,scrollbars=yes,resizable=yes");if(!b){console.log("[Recur SDK] Popup was blocked (null), falling back to redirect"),window.location.href=u.redirectUrl;return}if(await new Promise(R=>setTimeout(R,100)),b.closed){console.log("[Recur SDK] Popup was closed immediately, falling back to redirect"),window.location.href=u.redirectUrl;return}p.setVerifying?.(!0);let U=l.checkout.id,ee=l.checkout.clientSecret,Ie=90,Re=2e3,I=!0,te=!1,re;console.log("[Recur SDK] Starting 3D verification polling...");for(let R=0;R<Ie&&I;R++){if(b.closed){console.log("[Recur SDK] Popup was closed");try{let f=await fetch(`${n}/v1/checkouts/${U}/status?client_secret=${encodeURIComponent(ee)}`,{headers:o});if(f.ok){let P=await f.json();if(P.checkout?.status==="SUCCEEDED"){console.log("[Recur SDK] Payment succeeded after popup close"),I=!1,te=!0,re=P.checkout?.orderId;break}}}catch(f){console.error("[Recur SDK] Final status check failed:",f)}throw console.log("[Recur SDK] Payment not confirmed, user closed popup"),p.setVerifying?.(!1),p.resetButton?.(),new Error("3D \u9A57\u8B49\u5DF2\u53D6\u6D88")}try{let f=await fetch(`${n}/v1/checkouts/${U}/status?client_secret=${encodeURIComponent(ee)}`,{headers:o});if(f.ok){let P=await f.json(),A=P.checkout?.status;if(console.log(`[Recur SDK] Poll ${R+1}: status = ${A}`),A==="SUCCEEDED"){console.log("[Recur SDK] Payment succeeded"),I=!1;try{b.close()}catch{}p.setVerifying?.(!1),e.onPaymentComplete&&(u.subscription?e.onPaymentComplete({id:u.subscription.id,status:"ACTIVE",planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:u.subscription.billingPeriod,currentPeriodStart:u.subscription.currentPeriodStart,currentPeriodEnd:u.subscription.currentPeriodEnd}):e.onPaymentComplete({id:P.checkout?.orderId||U,status:"SUCCEEDED",planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:l.checkout.productType})),r&&r.remove();return}if(A==="CANCELED"||A==="FAILED"){console.log("[Recur SDK] Payment failed or canceled"),I=!1;try{b.close()}catch{}throw p.setVerifying?.(!1),p.resetButton?.(),new Error("\u4ED8\u6B3E\u5931\u6557\u6216\u5DF2\u53D6\u6D88")}}}catch(f){if(f.message==="3D \u9A57\u8B49\u5DF2\u53D6\u6D88"||f.message==="\u4ED8\u6B3E\u5931\u6557\u6216\u5DF2\u53D6\u6D88")throw f;console.error("[Recur SDK] Poll error:",f)}await new Promise(f=>setTimeout(f,Re))}if(I){console.log("[Recur SDK] 3D verification polling timeout");try{b.close()}catch{}throw p.setVerifying?.(!1),p.resetButton?.(),new Error("3D \u9A57\u8B49\u903E\u6642\uFF0C\u8ACB\u91CD\u8A66")}te&&(console.log("[Recur SDK] Handling success after popup close"),p.setVerifying?.(!1),e.onPaymentComplete&&(u.subscription?e.onPaymentComplete({id:u.subscription.id,status:"ACTIVE",planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:u.subscription.billingPeriod,currentPeriodStart:u.subscription.currentPeriodStart,currentPeriodEnd:u.subscription.currentPeriodEnd}):e.onPaymentComplete({id:re||U,status:"SUCCEEDED",planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:l.checkout.productType})),r&&r.remove());return}}if(e.onPaymentComplete)if(u.subscription)e.onPaymentComplete({id:u.subscription.id,status:u.subscription.status,planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:u.subscription.billingPeriod,currentPeriodStart:u.subscription.currentPeriodStart,currentPeriodEnd:u.subscription.currentPeriodEnd});else{let E=u.charge?.id||u.paymentIntent?.id,b=u.charge?.status||"SUCCEEDED";e.onPaymentComplete({id:E,status:b,planId:l.checkout.productId,amount:l.checkout.amount,billingPeriod:l.checkout.productType})}console.log("[Recur SDK] Checkout flow completed successfully!"),p.resetButton?.(),r&&r.remove()}catch(y){console.error("[Recur SDK] Payment error:",y);let C={code:"PAYMENT_FAILED",message:y instanceof Error?y.message:"Payment failed"};e.onError?.(C),p.resetButton?.()}})),console.log("[Recur SDK] Checkout flow initialized, waiting for user input...")}catch(n){console.error("[Recur SDK] Checkout error:",n),r&&r.remove();let o={code:"CHECKOUT_ERROR",message:n instanceof Error?n.message:"An unknown error occurred"};throw e.onError?.(o),n}}getBaseUrl(){let e=this.core.getConfig();if(e.baseUrl)return e.baseUrl;if(typeof window<"u"){let t=window.location.hostname;if(t==="localhost"||t.includes(".test")||t.includes(".local")||t==="127.0.0.1")return`${window.location.protocol}//${window.location.host}/api`}return"https://api.recur.tw"}createModalWithSkeleton(e){this.closeModal();let t=document.createElement("div");t.id="recur-modal-overlay",t.style.cssText=`
1540
1540
  position: fixed;
1541
1541
  top: 0;
1542
1542
  left: 0;
@@ -1575,7 +1575,7 @@
1575
1575
  border-radius: 50%;
1576
1576
  z-index: 10;
1577
1577
  transition: background 0.2s;
1578
- `,s.onmouseover=()=>{s.style.background="rgba(0, 0, 0, 0.1)"},s.onmouseout=()=>{s.style.background="rgba(0, 0, 0, 0.05)"},s.onclick=()=>{t.remove(),e?.()};let i=document.createElement("div");i.id="recur-modal-payment-container";let n=document.createElement("recur-payment-form-skeleton");return i.appendChild(n),r.appendChild(s),r.appendChild(i),t.appendChild(r),document.body.appendChild(t),this.currentModalOverlay=t,{overlay:t,container:i}}getEmbeddedContainer(e){return e?typeof e=="string"?document.getElementById(e)||document.querySelector(e):e:null}closeModal(){this.currentModal&&(this.currentModal.close(),this.currentModal=null),this.currentModalOverlay&&(this.currentModalOverlay.remove(),this.currentModalOverlay=null)}removeIframe(){this.currentIframe&&(this.currentIframe.remove(),this.currentIframe=null)}close(){this.closeModal(),this.removeIframe()}async createPortalSession(e,t){let r=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({customerId:t.customerId,returnUrl:t.returnUrl})});if(!r.ok){let i=await r.json().catch(()=>({}));throw new Error(i.error?.message||i.message||"Failed to create portal session")}let s=await r.json();return{id:s.id,url:s.url||s.portalUrl,expiresAt:s.expiresAt}}async redirectToPortal(e,t){let r=await this.createPortalSession(e,t);window.location.href=r.url}};function Ee(a){return new U(a)}var Be={init:Ee,RecurCheckout:U,RecurElements:T,createElements:G};return Me(Oe);})();
1578
+ `,s.onmouseover=()=>{s.style.background="rgba(0, 0, 0, 0.1)"},s.onmouseout=()=>{s.style.background="rgba(0, 0, 0, 0.05)"},s.onclick=()=>{t.remove(),e?.()};let i=document.createElement("div");i.id="recur-modal-payment-container";let n=document.createElement("recur-payment-form-skeleton");return i.appendChild(n),r.appendChild(s),r.appendChild(i),t.appendChild(r),document.body.appendChild(t),this.currentModalOverlay=t,{overlay:t,container:i}}getEmbeddedContainer(e){return e?typeof e=="string"?document.getElementById(e)||document.querySelector(e):e:null}closeModal(){this.currentModal&&(this.currentModal.close(),this.currentModal=null),this.currentModalOverlay&&(this.currentModalOverlay.remove(),this.currentModalOverlay=null)}removeIframe(){this.currentIframe&&(this.currentIframe.remove(),this.currentIframe=null)}close(){this.closeModal(),this.removeIframe()}async createPortalSession(e,t){let r=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({customerId:t.customerId,returnUrl:t.returnUrl})});if(!r.ok){let i=await r.json().catch(()=>({}));throw new Error(i.error?.message||i.message||"Failed to create portal session")}let s=await r.json();return{id:s.id,url:s.url||s.portalUrl,expiresAt:s.expiresAt}}async redirectToPortal(e,t){let r=await this.createPortalSession(e,t);window.location.href=r.url}};function Se(a){return new L(a)}var Oe={init:Se,RecurCheckout:L,RecurElements:T,createElements:Q};return Ae(je);})();
1579
1579
  if (typeof window !== "undefined") {
1580
1580
  window.RecurCheckout = RecurCheckout.default;
1581
1581
  window.RecurElements = RecurCheckout.RecurElements;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "recur-tw",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "React & Vanilla JS SDK for embedding subscription checkout flows (Taiwan / PAYUNi)",
5
5
  "type": "module",
6
6
  "private": false,
package/dist/server.cjs DELETED
@@ -1,152 +0,0 @@
1
- 'use strict';
2
-
3
- var __defProp = Object.defineProperty;
4
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
-
7
- // src/server/types.ts
8
- var RecurAPIError = class extends Error {
9
- constructor(error, statusCode) {
10
- super(error.message);
11
- __publicField(this, "type");
12
- __publicField(this, "code");
13
- __publicField(this, "statusCode");
14
- this.name = "RecurAPIError";
15
- this.type = error.type;
16
- this.code = error.code;
17
- this.statusCode = statusCode;
18
- }
19
- };
20
-
21
- // src/server/resources/portal.ts
22
- var SDK_VERSION = "0.8.1";
23
- var SDK_TYPE = "server";
24
- var PortalSessions = class {
25
- constructor(config) {
26
- __publicField(this, "config");
27
- this.config = config;
28
- }
29
- /**
30
- * Create a portal session for a customer
31
- *
32
- * Customer can be identified using one of:
33
- * - `customer`: Internal customer ID (highest priority)
34
- * - `externalId`: External customer ID from your system
35
- * - `email`: Customer's email address (lowest priority)
36
- *
37
- * @param params - Portal session creation parameters
38
- * @returns The created portal session with URL
39
- *
40
- * @example
41
- * ```typescript
42
- * // By customer ID
43
- * const session = await recur.portal.sessions.create({
44
- * customer: 'cus_xxx',
45
- * returnUrl: 'https://myapp.com/account',
46
- * });
47
- *
48
- * // By email
49
- * const session = await recur.portal.sessions.create({
50
- * email: 'customer@example.com',
51
- * returnUrl: 'https://myapp.com/account',
52
- * });
53
- *
54
- * // By external ID
55
- * const session = await recur.portal.sessions.create({
56
- * externalId: 'user_123',
57
- * returnUrl: 'https://myapp.com/account',
58
- * });
59
- *
60
- * // Redirect the customer to the portal
61
- * redirect(session.url);
62
- * ```
63
- */
64
- async create(params) {
65
- if (!params.customer && !params.email && !params.externalId) {
66
- throw new RecurAPIError(
67
- {
68
- type: "invalid_request_error",
69
- code: "missing_customer_identifier",
70
- message: "At least one of customer, email, or externalId is required"
71
- },
72
- 400
73
- );
74
- }
75
- const baseUrl = this.config.baseUrl || "https://api.recur.tw";
76
- const response = await fetch(`${baseUrl}/v1/portal/sessions`, {
77
- method: "POST",
78
- headers: {
79
- "Authorization": `Bearer ${this.config.secretKey}`,
80
- "Content-Type": "application/json",
81
- "X-Recur-SDK-Type": SDK_TYPE,
82
- "X-Recur-SDK-Version": SDK_VERSION,
83
- "X-Recur-Source": "server"
84
- },
85
- body: JSON.stringify({
86
- customerId: params.customer,
87
- email: params.email,
88
- externalId: params.externalId,
89
- returnUrl: params.returnUrl,
90
- configurationId: params.configuration,
91
- locale: params.locale
92
- })
93
- });
94
- const data = await response.json();
95
- if (!response.ok) {
96
- const error = data.error;
97
- throw new RecurAPIError(error, response.status);
98
- }
99
- return {
100
- id: data.id,
101
- object: "portal.session",
102
- url: data.url,
103
- customer: data.customerId,
104
- returnUrl: data.returnUrl,
105
- status: data.status,
106
- expiresAt: data.expiresAt,
107
- accessedAt: data.accessedAt,
108
- createdAt: data.createdAt
109
- };
110
- }
111
- };
112
- var Portal = class {
113
- constructor(config) {
114
- __publicField(this, "sessions");
115
- this.sessions = new PortalSessions(config);
116
- }
117
- };
118
-
119
- // src/server/recur.ts
120
- var Recur = class {
121
- /**
122
- * Create a new Recur client
123
- *
124
- * @param secretKey - Your Recur secret API key (sk_test_xxx or sk_live_xxx)
125
- * @param options - Additional configuration options
126
- */
127
- constructor(secretKey, options) {
128
- __publicField(this, "config");
129
- /**
130
- * Portal resource for managing customer portal sessions
131
- */
132
- __publicField(this, "portal");
133
- if (!secretKey) {
134
- throw new Error(
135
- "Recur: secretKey is required. Get your API key from your organization settings."
136
- );
137
- }
138
- if (!secretKey.startsWith("sk_")) {
139
- throw new Error(
140
- 'Recur: Invalid API key format. Secret keys should start with "sk_". Use your secret key (sk_xxx), not your publishable key (pk_xxx).'
141
- );
142
- }
143
- this.config = {
144
- secretKey,
145
- baseUrl: options?.baseUrl || "https://api.recur.tw"
146
- };
147
- this.portal = new Portal(this.config);
148
- }
149
- };
150
-
151
- exports.Recur = Recur;
152
- exports.RecurAPIError = RecurAPIError;
package/dist/server.js DELETED
@@ -1,149 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
-
5
- // src/server/types.ts
6
- var RecurAPIError = class extends Error {
7
- constructor(error, statusCode) {
8
- super(error.message);
9
- __publicField(this, "type");
10
- __publicField(this, "code");
11
- __publicField(this, "statusCode");
12
- this.name = "RecurAPIError";
13
- this.type = error.type;
14
- this.code = error.code;
15
- this.statusCode = statusCode;
16
- }
17
- };
18
-
19
- // src/server/resources/portal.ts
20
- var SDK_VERSION = "0.8.1";
21
- var SDK_TYPE = "server";
22
- var PortalSessions = class {
23
- constructor(config) {
24
- __publicField(this, "config");
25
- this.config = config;
26
- }
27
- /**
28
- * Create a portal session for a customer
29
- *
30
- * Customer can be identified using one of:
31
- * - `customer`: Internal customer ID (highest priority)
32
- * - `externalId`: External customer ID from your system
33
- * - `email`: Customer's email address (lowest priority)
34
- *
35
- * @param params - Portal session creation parameters
36
- * @returns The created portal session with URL
37
- *
38
- * @example
39
- * ```typescript
40
- * // By customer ID
41
- * const session = await recur.portal.sessions.create({
42
- * customer: 'cus_xxx',
43
- * returnUrl: 'https://myapp.com/account',
44
- * });
45
- *
46
- * // By email
47
- * const session = await recur.portal.sessions.create({
48
- * email: 'customer@example.com',
49
- * returnUrl: 'https://myapp.com/account',
50
- * });
51
- *
52
- * // By external ID
53
- * const session = await recur.portal.sessions.create({
54
- * externalId: 'user_123',
55
- * returnUrl: 'https://myapp.com/account',
56
- * });
57
- *
58
- * // Redirect the customer to the portal
59
- * redirect(session.url);
60
- * ```
61
- */
62
- async create(params) {
63
- if (!params.customer && !params.email && !params.externalId) {
64
- throw new RecurAPIError(
65
- {
66
- type: "invalid_request_error",
67
- code: "missing_customer_identifier",
68
- message: "At least one of customer, email, or externalId is required"
69
- },
70
- 400
71
- );
72
- }
73
- const baseUrl = this.config.baseUrl || "https://api.recur.tw";
74
- const response = await fetch(`${baseUrl}/v1/portal/sessions`, {
75
- method: "POST",
76
- headers: {
77
- "Authorization": `Bearer ${this.config.secretKey}`,
78
- "Content-Type": "application/json",
79
- "X-Recur-SDK-Type": SDK_TYPE,
80
- "X-Recur-SDK-Version": SDK_VERSION,
81
- "X-Recur-Source": "server"
82
- },
83
- body: JSON.stringify({
84
- customerId: params.customer,
85
- email: params.email,
86
- externalId: params.externalId,
87
- returnUrl: params.returnUrl,
88
- configurationId: params.configuration,
89
- locale: params.locale
90
- })
91
- });
92
- const data = await response.json();
93
- if (!response.ok) {
94
- const error = data.error;
95
- throw new RecurAPIError(error, response.status);
96
- }
97
- return {
98
- id: data.id,
99
- object: "portal.session",
100
- url: data.url,
101
- customer: data.customerId,
102
- returnUrl: data.returnUrl,
103
- status: data.status,
104
- expiresAt: data.expiresAt,
105
- accessedAt: data.accessedAt,
106
- createdAt: data.createdAt
107
- };
108
- }
109
- };
110
- var Portal = class {
111
- constructor(config) {
112
- __publicField(this, "sessions");
113
- this.sessions = new PortalSessions(config);
114
- }
115
- };
116
-
117
- // src/server/recur.ts
118
- var Recur = class {
119
- /**
120
- * Create a new Recur client
121
- *
122
- * @param secretKey - Your Recur secret API key (sk_test_xxx or sk_live_xxx)
123
- * @param options - Additional configuration options
124
- */
125
- constructor(secretKey, options) {
126
- __publicField(this, "config");
127
- /**
128
- * Portal resource for managing customer portal sessions
129
- */
130
- __publicField(this, "portal");
131
- if (!secretKey) {
132
- throw new Error(
133
- "Recur: secretKey is required. Get your API key from your organization settings."
134
- );
135
- }
136
- if (!secretKey.startsWith("sk_")) {
137
- throw new Error(
138
- 'Recur: Invalid API key format. Secret keys should start with "sk_". Use your secret key (sk_xxx), not your publishable key (pk_xxx).'
139
- );
140
- }
141
- this.config = {
142
- secretKey,
143
- baseUrl: options?.baseUrl || "https://api.recur.tw"
144
- };
145
- this.portal = new Portal(this.config);
146
- }
147
- };
148
-
149
- export { Recur, RecurAPIError };