mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
refactor ProcessPaymentIntent to service
This commit is contained in:
@@ -25,7 +25,7 @@ module Spree
|
||||
before_action :check_at_least_one_line_item, only: :update
|
||||
|
||||
def show
|
||||
process_payment_intent!(params["payment_intent"])
|
||||
ProcessPaymentIntent.new(params["payment_intent"], params[:id]).call!
|
||||
@order = Spree::Order.find_by!(number: params[:id])
|
||||
end
|
||||
|
||||
@@ -216,17 +216,5 @@ module Spree
|
||||
line_items_attributes: [:id, :quantity]
|
||||
)
|
||||
end
|
||||
|
||||
def process_payment_intent!(payment_intent)
|
||||
return unless payment_intent&.starts_with?("pi_")
|
||||
return unless order = Spree::Order.find_by!(number: params[:id])
|
||||
|
||||
last_payment = OrderPaymentFinder.new(order).last_payment
|
||||
return unless last_payment&.state == "pending" &&
|
||||
last_payment&.response_code == payment_intent
|
||||
|
||||
last_payment.update_attribute(:cvv_response_message, nil)
|
||||
last_payment.complete!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
30
app/services/process_payment_intent.rb
Normal file
30
app/services/process_payment_intent.rb
Normal file
@@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ProcessPaymentIntent
|
||||
def initialize(payment_intent, order_number)
|
||||
@payment_intent = payment_intent
|
||||
@order = Spree::Order.find_by!(number: order_number)
|
||||
@last_payment = OrderPaymentFinder.new(@order).last_payment
|
||||
end
|
||||
|
||||
def call!
|
||||
return unless valid?
|
||||
|
||||
@last_payment.update_attribute(:cvv_response_message, nil)
|
||||
@last_payment.complete!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def valid?
|
||||
@order.present? && valid_intent_string? && matches_last_payment?
|
||||
end
|
||||
|
||||
def valid_intent_string?
|
||||
@payment_intent&.starts_with?("pi_")
|
||||
end
|
||||
|
||||
def matches_last_payment?
|
||||
@last_payment&.state == "pending" && @last_payment&.response_code == @payment_intent
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user