Refactor arguments for PaymentIntentValidator

This commit is contained in:
Matt-Yorkley
2021-06-17 11:35:35 +01:00
parent e1846b1030
commit 32a2610e7d
7 changed files with 28 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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