refactor ProcessPaymentIntent to service

This commit is contained in:
Andy Brett
2021-02-10 11:22:29 -08:00
parent ef6d1a3afb
commit 891874995b
2 changed files with 31 additions and 13 deletions

View File

@@ -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

View 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