From 6965eb89fd191e2a35e10c0e6535d6d618d1ad9b Mon Sep 17 00:00:00 2001 From: kbe Date: Tue, 2 Sep 2025 23:16:31 +0200 Subject: [PATCH] fix: Only increment payment attempts when user actually attempts payment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove payment attempt increment from checkout page load - Add new increment_payment_attempt action triggered only on pay button click - Update checkout JavaScript to make AJAX call before Stripe redirect - Add proper error handling and button state management - Prevent inflated payment attempt counts from page refreshes This ensures payment attempts accurately reflect actual payment tries rather than page visits. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- app/controllers/orders_controller.rb | 9 +++- app/views/orders/checkout.html.erb | 67 ++++++++++++++++++++-------- config/routes.rb | 1 + 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index f9c6d41..5e61d2c 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -4,7 +4,7 @@ # Orders group multiple tickets together for better transaction management class OrdersController < ApplicationController before_action :authenticate_user! - before_action :set_order, only: [:show, :checkout, :retry_payment] + before_action :set_order, only: [:show, :checkout, :retry_payment, :increment_payment_attempt] # Display order summary def show @@ -31,7 +31,6 @@ class OrdersController < ApplicationController if Rails.application.config.stripe[:secret_key].present? begin @checkout_session = create_stripe_session - @order.increment_payment_attempt! rescue => e error_message = e.message.present? ? e.message : "Erreur Stripe inconnue" Rails.logger.error "Stripe checkout session creation failed: #{error_message}" @@ -40,6 +39,12 @@ class OrdersController < ApplicationController end end + # Increment payment attempt - called via AJAX when user clicks pay button + def increment_payment_attempt + @order.increment_payment_attempt! + render json: { success: true, attempts: @order.payment_attempts } + end + # Allow users to retry payment for failed/cancelled payments def retry_payment unless @order.can_retry_payment? diff --git a/app/views/orders/checkout.html.erb b/app/views/orders/checkout.html.erb index ec96e86..98233d5 100644 --- a/app/views/orders/checkout.html.erb +++ b/app/views/orders/checkout.html.erb @@ -180,7 +180,7 @@ diff --git a/config/routes.rb b/config/routes.rb index 2b996cb..b35b17b 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,6 +43,7 @@ Rails.application.routes.draw do member do get :checkout post :retry_payment + post :increment_payment_attempt end end