From ddf6e7eadd91afea3861387e68f356ce3c7c514b Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 27 Dec 2021 21:36:32 +0000 Subject: [PATCH] Use proper module/namespace --- .../payment_gateways/stripe_controller.rb | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/app/controllers/payment_gateways/stripe_controller.rb b/app/controllers/payment_gateways/stripe_controller.rb index cb2e94ef5c..980d03d57e 100644 --- a/app/controllers/payment_gateways/stripe_controller.rb +++ b/app/controllers/payment_gateways/stripe_controller.rb @@ -1,40 +1,42 @@ # frozen_string_literal: true -class StripeController < BaseController - include OrderStockCheck - include OrderCompletion +module PaymentGateways + class StripeController < BaseController + include OrderStockCheck + include OrderCompletion - before_action :load_checkout_order, only: :confirm + before_action :load_checkout_order, only: :confirm - def confirm - return processing_failed unless valid_payment_intent? + def confirm + return processing_failed unless valid_payment_intent? - cancel_incomplete_payments && handle_insufficient_stock unless sufficient_stock? + cancel_incomplete_payments && handle_insufficient_stock unless sufficient_stock? - process_payment_completion! - end - - private - - def valid_payment_intent? - @valid_payment_intent ||= begin - return false unless params["payment_intent"]&.starts_with?("pi_") - - last_payment = OrderPaymentFinder.new(@order).last_payment - - @order.state == "payment" && - last_payment&.state == "requires_authorization" && - last_payment&.response_code == params["payment_intent"] + process_payment_completion! end - end - def cancel_incomplete_payments - # The checkout could not complete due to stock running out. We void any pending (incomplete) - # Stripe payments here as the order will need to be changed and resubmitted (or abandoned). - @order.payments.incomplete.each do |payment| - payment.void_transaction! - payment.adjustment&.update_columns(eligible: false, state: "finalized") + private + + def valid_payment_intent? + @valid_payment_intent ||= begin + return false unless params["payment_intent"]&.starts_with?("pi_") + + last_payment = OrderPaymentFinder.new(@order).last_payment + + @order.state == "payment" && + last_payment&.state == "requires_authorization" && + last_payment&.response_code == params["payment_intent"] + end + end + + def cancel_incomplete_payments + # The checkout could not complete due to stock running out. We void any pending (incomplete) + # Stripe payments here as the order will need to be changed and resubmitted (or abandoned). + @order.payments.incomplete.each do |payment| + payment.void_transaction! + payment.adjustment&.update_columns(eligible: false, state: "finalized") + end + flash[:notice] = I18n.t("checkout.payment_cancelled_due_to_stock") end - flash[:notice] = I18n.t("checkout.payment_cancelled_due_to_stock") end end