fix: Resolve Stripe checkout button loading issues
- Add proper Stripe library loading checks to prevent ReferenceError - Implement retry logic for Stripe library initialization - Add comprehensive debugging console logs for troubleshooting - Ensure DOM ready state handling for Turbo compatibility - Fix async loading race conditions between Stripe CDN and local script - Add proper error handling for checkout button initialization 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -178,9 +178,25 @@
|
|||||||
|
|
||||||
<script src="https://js.stripe.com/v3/"></script>
|
<script src="https://js.stripe.com/v3/"></script>
|
||||||
<script>
|
<script>
|
||||||
const stripe = Stripe('<%= Rails.application.config.stripe[:publishable_key] %>');
|
// Wait for Stripe library to load and DOM to be ready
|
||||||
|
function initializeStripeCheckout() {
|
||||||
document.getElementById('checkout-button').addEventListener('click', async function() {
|
if (typeof Stripe === 'undefined') {
|
||||||
|
console.log('Waiting for Stripe library to load...');
|
||||||
|
setTimeout(initializeStripeCheckout, 100);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Initializing Stripe with publishable key:', '<%= Rails.application.config.stripe[:publishable_key] %>');
|
||||||
|
const stripe = Stripe('<%= Rails.application.config.stripe[:publishable_key] %>');
|
||||||
|
|
||||||
|
const checkoutButton = document.getElementById('checkout-button');
|
||||||
|
if (!checkoutButton) {
|
||||||
|
console.error('Checkout button not found');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
checkoutButton.addEventListener('click', async function() {
|
||||||
|
console.log('Checkout button clicked');
|
||||||
const button = this;
|
const button = this;
|
||||||
button.disabled = true;
|
button.disabled = true;
|
||||||
button.innerHTML = `
|
button.innerHTML = `
|
||||||
@@ -195,6 +211,7 @@
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Increment payment attempt counter
|
// Increment payment attempt counter
|
||||||
|
console.log('Incrementing payment attempt for order:', '<%= @order.id %>');
|
||||||
const response = await fetch('<%= increment_payment_attempt_order_path(@order) %>', {
|
const response = await fetch('<%= increment_payment_attempt_order_path(@order) %>', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -204,9 +221,12 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
console.error('Payment attempt increment failed:', response.status, response.statusText);
|
||||||
throw new Error('Failed to increment payment attempt');
|
throw new Error('Failed to increment payment attempt');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('Payment attempt incremented successfully');
|
||||||
|
|
||||||
// Update button text for redirect
|
// Update button text for redirect
|
||||||
button.innerHTML = `
|
button.innerHTML = `
|
||||||
<div class="flex items-center justify-center">
|
<div class="flex items-center justify-center">
|
||||||
@@ -219,6 +239,7 @@
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
// Redirect to Stripe
|
// Redirect to Stripe
|
||||||
|
console.log('Redirecting to Stripe with session ID:', '<%= @checkout_session&.id %>');
|
||||||
const stripeResult = await stripe.redirectToCheckout({
|
const stripeResult = await stripe.redirectToCheckout({
|
||||||
sessionId: '<%= @checkout_session.id %>'
|
sessionId: '<%= @checkout_session.id %>'
|
||||||
});
|
});
|
||||||
@@ -228,6 +249,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Checkout error:', error);
|
||||||
// Reset button on error
|
// Reset button on error
|
||||||
button.disabled = false;
|
button.disabled = false;
|
||||||
button.innerHTML = `
|
button.innerHTML = `
|
||||||
@@ -240,7 +262,15 @@
|
|||||||
`;
|
`;
|
||||||
alert('Erreur: ' + error.message);
|
alert('Erreur: ' + error.message);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialize when DOM is ready
|
||||||
|
if (document.readyState === 'loading') {
|
||||||
|
document.addEventListener('DOMContentLoaded', initializeStripeCheckout);
|
||||||
|
} else {
|
||||||
|
initializeStripeCheckout();
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</div>
|
</div>
|
||||||
<% else %>
|
<% else %>
|
||||||
|
|||||||
Reference in New Issue
Block a user