From 32a2610e7d1daa6abc1a38250f877788b3acca95 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 17 Jun 2021 11:35:35 +0100 Subject: [PATCH] Refactor arguments for PaymentIntentValidator --- app/models/spree/gateway/stripe_sca.rb | 2 +- app/services/process_payment_intent.rb | 2 +- app/services/stripe_payment_status.rb | 2 +- lib/stripe/payment_intent_validator.rb | 16 +++++++++++----- spec/controllers/spree/orders_controller_spec.rb | 15 ++++++--------- spec/lib/stripe/payment_intent_validator_spec.rb | 6 +++--- spec/services/process_payment_intent_spec.rb | 10 +++++----- 7 files changed, 28 insertions(+), 25 deletions(-) diff --git a/app/models/spree/gateway/stripe_sca.rb b/app/models/spree/gateway/stripe_sca.rb index e23a4ea479..28f9e6db7b 100644 --- a/app/models/spree/gateway/stripe_sca.rb +++ b/app/models/spree/gateway/stripe_sca.rb @@ -129,7 +129,7 @@ module Spree payment = fetch_payment(creditcard, gateway_options) raise Stripe::StripeError, I18n.t(:no_pending_payments) unless payment&.response_code - payment_intent_response = Stripe::PaymentIntentValidator.new.call(payment) + payment_intent_response = Stripe::PaymentIntentValidator.new(payment).call raise_if_not_in_capture_state(payment_intent_response) diff --git a/app/services/process_payment_intent.rb b/app/services/process_payment_intent.rb index e9456cfb0b..b18319d83d 100644 --- a/app/services/process_payment_intent.rb +++ b/app/services/process_payment_intent.rb @@ -69,6 +69,6 @@ class ProcessPaymentIntent end def payment_intent_status - @payment_intent_status ||= Stripe::PaymentIntentValidator.new.call(payment).status + @payment_intent_status ||= Stripe::PaymentIntentValidator.new(payment).call.status end end diff --git a/app/services/stripe_payment_status.rb b/app/services/stripe_payment_status.rb index 49f5c998ac..a5ed2759d8 100644 --- a/app/services/stripe_payment_status.rb +++ b/app/services/stripe_payment_status.rb @@ -13,7 +13,7 @@ class StripePaymentStatus def stripe_status return if payment.response_code.blank? - Stripe::PaymentIntentValidator.new.call(self).status + Stripe::PaymentIntentValidator.new(self).call.status rescue Stripe::StripeError # Stripe::PaymentIntentValidator will raise an error if the response from the Stripe API # call indicates the last attempted action on the payment intent failed. diff --git a/lib/stripe/payment_intent_validator.rb b/lib/stripe/payment_intent_validator.rb index 2deeb74584..10f176af61 100644 --- a/lib/stripe/payment_intent_validator.rb +++ b/lib/stripe/payment_intent_validator.rb @@ -3,10 +3,14 @@ # This class validates if a given payment intent ID is valid in Stripe module Stripe class PaymentIntentValidator - def call(payment) + def initialize(payment) + @payment = payment + end + + def call payment_intent_response = Stripe::PaymentIntent.retrieve( - payment_intent_id(payment), - stripe_account: stripe_account_id(payment) + payment_intent_id, + stripe_account: stripe_account_id ) raise_if_last_payment_error_present(payment_intent_response) @@ -16,11 +20,13 @@ module Stripe private - def payment_intent_id(payment) + attr_accessor :payment + + def payment_intent_id payment.response_code end - def stripe_account_id(payment) + def stripe_account_id enterprise_id = payment.payment_method&.preferred_enterprise_id StripeAccount.find_by(enterprise_id: enterprise_id)&.stripe_user_id diff --git a/spec/controllers/spree/orders_controller_spec.rb b/spec/controllers/spree/orders_controller_spec.rb index 909d7d3609..7776d02903 100644 --- a/spec/controllers/spree/orders_controller_spec.rb +++ b/spec/controllers/spree/orders_controller_spec.rb @@ -113,9 +113,8 @@ describe Spree::OrdersController, type: :controller do let(:payment_intent_response) { double(id: "pi_123", status: "requires_capture") } before do - allow_any_instance_of(Stripe::PaymentIntentValidator) - .to receive(:call) - .with(payment) + allow(Stripe::PaymentIntentValidator) + .to receive_message_chain(:new, :call) .and_return(payment_intent_response) allow(Spree::Order).to receive(:find_by!) { order } @@ -159,9 +158,8 @@ describe Spree::OrdersController, type: :controller do let(:payment_intent) { "pi_123" } before do - allow_any_instance_of(Stripe::PaymentIntentValidator) - .to receive(:call) - .with(payment) + allow(Stripe::PaymentIntentValidator) + .to receive_message_chain(:new, :call) .and_raise(Stripe::StripeError, "error message") end @@ -183,9 +181,8 @@ describe Spree::OrdersController, type: :controller do before do allow(payment).to receive(:response_code).and_return("invalid") allow(OrderPaymentFinder).to receive(:new).with(order).and_return(finder) - allow_any_instance_of(Stripe::PaymentIntentValidator) - .to receive(:call) - .with(payment_intent, kind_of(String)) + allow(Stripe::PaymentIntentValidator) + .to receive_message_chain(:new, :call) .and_return(payment_intent) stub_payment_intent_get_request(payment_intent_id: "valid") end diff --git a/spec/lib/stripe/payment_intent_validator_spec.rb b/spec/lib/stripe/payment_intent_validator_spec.rb index c4ff925b08..12287bafe6 100644 --- a/spec/lib/stripe/payment_intent_validator_spec.rb +++ b/spec/lib/stripe/payment_intent_validator_spec.rb @@ -6,7 +6,7 @@ require 'stripe/payment_intent_validator' module Stripe describe PaymentIntentValidator do describe "#call" do - let(:validator) { Stripe::PaymentIntentValidator.new } + let(:validator) { Stripe::PaymentIntentValidator.new(payment) } let(:payment) { build(:payment, response_code: payment_intent_id) } let(:payment_intent_id) { "pi_123" } let(:stripe_account_id) { "abc123" } @@ -31,7 +31,7 @@ module Stripe it "returns payment intent id and does not raise" do expect { - result = validator.call(payment) + result = validator.call expect(result).to eq payment_intent_response_body }.to_not raise_error Stripe::StripeError end @@ -44,7 +44,7 @@ module Stripe it "raises Stripe error with payment intent last_payment_error as message" do expect { - validator.call(payment) + validator.call }.to raise_error Stripe::StripeError, "No money" end end diff --git a/spec/services/process_payment_intent_spec.rb b/spec/services/process_payment_intent_spec.rb index 01424570af..515ee16065 100644 --- a/spec/services/process_payment_intent_spec.rb +++ b/spec/services/process_payment_intent_spec.rb @@ -51,8 +51,8 @@ describe ProcessPaymentIntent do context "where the stripe payment intent validation responds with errors" do before do - allow(validator) - .to receive(:call).with(payment).and_raise(Stripe::StripeError, "error message") + allow(validator).to receive(:call). + and_raise(Stripe::StripeError, "error message") end it "returns returns the error message" do @@ -76,7 +76,7 @@ describe ProcessPaymentIntent do before do allow(order).to receive(:deliver_order_confirmation_email) - allow(validator).to receive(:call).with(payment).and_return(intent_response) + allow(validator).to receive(:call).and_return(intent_response) end it "validates the intent" do @@ -142,7 +142,7 @@ describe ProcessPaymentIntent do before do payment.update_attribute(:state, "failed") - allow(validator).to receive(:call).with(payment).and_return(intent) + allow(validator).to receive(:call).and_return(intent) end it "does not return any error message" do @@ -165,7 +165,7 @@ describe ProcessPaymentIntent do before do allow(order).to receive(:process_payments!) { nil } - allow(validator).to receive(:call).with(payment).and_return(intent_response) + allow(validator).to receive(:call).and_return(intent_response) end it "returns a failed result" do