| 1 |
class StripeCheckout { |
| 2 |
constructor(form, response) { |
| 3 |
this.form = form; |
| 4 |
this.data = response.data; |
| 5 |
this.intent = response.data?.intent; |
| 6 |
} |
| 7 |
|
| 8 |
init() { |
| 9 |
const formItems = this.form.querySelectorAll('.fcal_form_item'); |
| 10 |
formItems.forEach(item => { |
| 11 |
item.style.display = 'none'; |
| 12 |
}); |
| 13 |
|
| 14 |
const paymentProcessor = this.form.querySelector('.fluent_booking_payment_processor'); |
| 15 |
paymentProcessor.style.display = 'block'; |
| 16 |
|
| 17 |
const submitButton = document.createElement('button'); |
| 18 |
submitButton.id = 'fluent_booking_stipe_pay'; |
| 19 |
submitButton.style.marginTop = '23px'; |
| 20 |
submitButton.type = 'submit'; |
| 21 |
submitButton.textContent = window.fcal_translate('Pay Now'); |
| 22 |
|
| 23 |
const stripe = Stripe(this.data?.data?.payment_args?.public_key); |
| 24 |
|
| 25 |
const elements = stripe.elements({ |
| 26 |
clientSecret: this.intent.client_secret, |
| 27 |
}); |
| 28 |
|
| 29 |
const paymentElement = elements.create('payment', {}); |
| 30 |
const paymentMethods = this.form.querySelector('.fluent_booking_payment_methods'); |
| 31 |
|
| 32 |
paymentElement.mount(paymentMethods); |
| 33 |
|
| 34 |
const loadingMessage = document.createElement('p'); |
| 35 |
loadingMessage.classList.add('fluent_booking_loading_payment_processor') ; |
| 36 |
loadingMessage.textContent = 'Loading Payment Processor...'; |
| 37 |
|
| 38 |
paymentProcessor.appendChild(loadingMessage); |
| 39 |
|
| 40 |
const submit = this.form.querySelector('.fcal_submit'); |
| 41 |
submit.style.display = 'none'; |
| 42 |
|
| 43 |
const that = this; |
| 44 |
|
| 45 |
paymentElement.on('ready', (event)=> { |
| 46 |
const loadingPaymentProcessor = this.form.querySelector('.fluent_booking_loading_payment_processor'); |
| 47 |
if (loadingPaymentProcessor) { |
| 48 |
loadingPaymentProcessor.remove(); |
| 49 |
} |
| 50 |
|
| 51 |
paymentMethods.appendChild(submitButton); |
| 52 |
|
| 53 |
const stripePayButton = this.form.querySelector('#fluent_booking_stipe_pay'); |
| 54 |
stripePayButton.addEventListener('click', function (e) { |
| 55 |
e.preventDefault(); |
| 56 |
|
| 57 |
elements.submit().then(result => { |
| 58 |
stripePayButton.textContent = 'Processing...'; |
| 59 |
stripePayButton.disabled = true; |
| 60 |
|
| 61 |
const confirmParams = { |
| 62 |
// return_url: that.data?.data?.payment_args?.success_url |
| 63 |
}; |
| 64 |
|
| 65 |
stripe.confirmPayment({ |
| 66 |
elements, |
| 67 |
confirmParams, |
| 68 |
redirect: 'if_required' |
| 69 |
}).then(result => { |
| 70 |
if (result?.paymentIntent?.id) { |
| 71 |
stripePayButton.disabled = true; |
| 72 |
fetch(window.fluentCalendarPublicVars.ajaxurl, { |
| 73 |
method: 'POST', |
| 74 |
headers: { |
| 75 |
'Content-Type': 'application/x-www-form-urlencoded', |
| 76 |
}, |
| 77 |
body: `action=fluent_cal_confirm_stripe_payment&intentId=${result?.paymentIntent?.id}`, |
| 78 |
}).then(response => { |
| 79 |
response.json().then(data => { |
| 80 |
window.location.href = that.data?.data?.payment_args?.success_url; |
| 81 |
}); |
| 82 |
}); |
| 83 |
} |
| 84 |
if (result?.error) { |
| 85 |
stripePayButton.textContent = window.fcal_translate('Pay Now'); |
| 86 |
stripePayButton.disabled = false; |
| 87 |
} |
| 88 |
}); |
| 89 |
}).catch(error => { |
| 90 |
stripePayButton.textContent = window.fcal_translate('Pay Now'); |
| 91 |
stripePayButton.disabled = false; |
| 92 |
}); |
| 93 |
}); |
| 94 |
}); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
window.addEventListener('fluent_booking_payment_next_action_stripe', function (e) { |
| 99 |
new StripeCheckout(e.detail.form, e.detail.response).init(); |
| 100 |
}); |
| 101 |
|