import{t as e}from"../../formatAmount-B0NmTpHt.js";import{t}from"../../coffeeLoader-eLx5ggrp.js";var n=class{constructor(e,n){this.form=e,this.data=n.data,this.intent=n.data?.intent,this.subscriptionId=n.data?.subscription_id||null,this.parentWrapper=this.form.parents(`.bmc-form-card, .buymecoffee_form_preview_wrapper`).first(),this.loader=new t(this.parentWrapper[0]),this.payButton=null}init(){this.startPaymentProcessing(),this.payButton=this.generatePayButton();var e=Stripe(this.data?.order_items?.payment_args?.public_key);let t=e.elements({clientSecret:this.intent.client_secret}),n=t.create(`payment`,{}),r=`#`+this.form.attr(`id`)+` .buymecoffee_payment_processor`;n.mount(r);let i=this;n.on(`ready`,()=>{this.afterPaymentProcessorReady(this.payButton),this.payButton.on(`click`,async function(n){n.preventDefault(),i.payButton.attr(`disabled`,!0),i.loader.show(`Processing your payment...`);try{await t.submit();let n=await e.confirmPayment({elements:t,confirmParams:{},redirect:`if_required`});if(n.error){i.showError(n.error.message||`Stripe could not confirm your payment. Please try again.`);return}let r=n?.paymentIntent?.id;if(!r){i.showError(`Stripe did not return a payment intent. Please try again.`);return}let a={action:`buymecoffee_payment_confirmation_stripe`,buymecoffee_nonce:window.buymecoffee_general?.buymecoffee_nonce||``,intentId:r};i.subscriptionId&&(a.subscriptionId=i.subscriptionId);let o=await jQuery.post(window.buymecoffee_general.ajax_url,a);i.afterPaymentSuccess(o?.data||{})}catch(e){let t=e?.responseJSON?.data?.message||e?.message||`Payment could not be confirmed. Please try again.`;i.showError(t)}finally{i.payButton&&i.payButton.removeAttr(`disabled`),i.loader.hide()}})})}generatePayButton(){let t=this.form.data(`wpm_currency`)||window.buymecoffee_general?.default_currency||`USD`,n=`Pay `+e(this.intent.amount,t)+` Now`;return jQuery(`<button type='submit' class='buymecoffee_pay_now'></button>`).text(n)}startPaymentProcessing(){this.form.find(`.buymecoffee_input_content, .buymecoffee_pay_methods, .buymecoffee_payment_item, .buymecoffee_form_submit_wrapper, .buymecoffee_no_signup, .buymecoffee_recurring_section`).hide(),this.loader.show(`Setting up payment...`)}afterPaymentSuccess(e={}){let t=!!this.data?.is_subscription,n=!!this.data?.is_membership,r=e.payment_status===`paid`,i=e.access_active===!0||e.access_active===`1`,a=r&&(!n||i),o=a?t?`Recurring donation set up successfully`:n?`Membership activated`:`Thanks for your contribution`:`Payment is processing`,s=a?t?`Your subscription is active. You can manage everything from your account.`:n?`Your membership access is active. You can manage everything from your account.`:`Your payment was successful. You can view your receipt below.`:`Stripe is still confirming this payment. Your access will be available as soon as the payment is completed.`,c=jQuery(`<div class='buymecoffee_form_receipt'></div>`),l=jQuery(`<div class='bmc-receipt-success'></div>`),u=jQuery(`<div class='bmc-receipt-success__header'></div>`),d=jQuery(`<span class='bmc-receipt-success__badge' aria-hidden='true'>✓</span>`),f=jQuery(`<div class='bmc-receipt-success__text'></div>`);f.append(jQuery(`<p class='bmc-receipt-success__title'></p>`).text(o)),f.append(jQuery(`<p class='bmc-receipt-success__subtitle'></p>`).text(s)),u.append(d,f),l.append(u);let p=jQuery(`<div class='bmc-receipt-success__actions'></div>`),m=this.getSafeReceiptUrl(this.data?.order_items?.payment_args?.success_url);if(m){let e=jQuery(`<a></a>`).addClass(`bmc-receipt-success__link`).attr(`href`,m).text(`View Receipt`);p.append(e)}if((t||n)&&a){let e=this.getSafeReceiptUrl(window.buymecoffee_general?.account_page_url);if(e){let t=jQuery(`<a></a>`).addClass(`bmc-receipt-success__link`).attr(`href`,e).text(`View subscriptions`);p.append(t)}}let h=this.form.find(`input[name="bmc_return_url"]`).val();if(h){let e=this.getSafeReceiptUrl(h);if(e){let t=jQuery(`<a></a>`).addClass(`bmc-receipt-success__link bmc-receipt-success__link--primary`).attr(`href`,e).text(`Return to article`);p.prepend(t),setTimeout(()=>{window.location.href=e},3e3)}}p.children().length&&l.append(p),c.append(l),this.parentWrapper.append(c),this.parentWrapper.find(`.buymecoffee_form`).hide(),this.parentWrapper.find(`.buymecoffee_form_to, .bmc-form-card__title`).hide()}afterPaymentProcessorReady(e){this.loader.hide(),this.form.prepend(`<p class='complete_payment_instruction'>Please complete your donation with Stripe 👇</p>`),this.form.find(`.buymecoffee_payment_processor`).append(e),this.form.find(`[data-bmc-level-info]`).hide()}getSafeReceiptUrl(e){if(!e)return null;try{let t=new URL(e,window.location.origin);return![`http:`,`https:`].includes(t.protocol)||t.origin!==window.location.origin?null:t.toString()}catch{return null}}showError(e){e&&alert(e)}};window.addEventListener(`buymecoffee_payment_next_action_stripe`,function(e){new n(e.detail.form,e.detail.response).init()});