diff --git a/app/controllers/spree/admin/payments_controller.rb b/app/controllers/spree/admin/payments_controller.rb index b95070ccf2..7649a5af41 100644 --- a/app/controllers/spree/admin/payments_controller.rb +++ b/app/controllers/spree/admin/payments_controller.rb @@ -52,7 +52,7 @@ module Spree # (we can't use respond_override because Spree no longer uses respond_with) def fire event = params[:e] - return unless event && @payment.payment_source + return unless event # capture_and_complete_order will complete the order, so we want to try to redeem VINE # voucher first and exit if it fails diff --git a/app/models/spree/credit_card.rb b/app/models/spree/credit_card.rb index 19fc47bff3..5a6d8dfddc 100644 --- a/app/models/spree/credit_card.rb +++ b/app/models/spree/credit_card.rb @@ -63,31 +63,6 @@ module Spree "XXXX-XXXX-XXXX-#{last_digits}" end - def can_resend_authorization_email?(payment) - payment.requires_authorization? - end - - # Indicates whether its possible to capture the payment - def can_capture_and_complete_order?(payment) - return false if payment.requires_authorization? - - payment.pending? || payment.checkout? - end - - # Indicates whether its possible to void the payment. - def can_void?(payment) - !payment.void? - end - - # Indicates whether its possible to credit the payment. Note that most gateways require that the - # payment be settled first which generally happens within 12-24 hours of the transaction. - def can_credit?(payment) - return false unless payment.completed? - return false unless payment.order.payment_state == 'credit_owed' - - payment.credit_allowed.positive? - end - # Allows us to use a gateway_payment_profile_id to store Stripe Tokens def has_payment_profile? gateway_customer_profile_id.present? || gateway_payment_profile_id.present? diff --git a/app/models/spree/gateway.rb b/app/models/spree/gateway.rb index 1ab4e8808f..e08a4e0ca0 100644 --- a/app/models/spree/gateway.rb +++ b/app/models/spree/gateway.rb @@ -17,6 +17,31 @@ module Spree %w{capture_and_complete_order void credit resend_authorization_email} end + # Indicates whether its possible to capture the payment + def can_capture_and_complete_order?(payment) + return false if payment.requires_authorization? + + payment.pending? || payment.checkout? + end + + # Indicates whether its possible to void the payment. + def can_void?(payment) + !payment.void? + end + + # Indicates whether its possible to credit the payment. Note that most gateways require that the + # payment be settled first which generally happens within 12-24 hours of the transaction. + def can_credit?(payment) + return false unless payment.completed? + return false unless payment.order.payment_state == 'credit_owed' + + payment.credit_allowed.positive? + end + + def can_resend_authorization_email?(payment) + payment.requires_authorization? + end + def payment_source_class CreditCard end diff --git a/app/models/spree/payment.rb b/app/models/spree/payment.rb index ef5c4d67a5..f397c43106 100644 --- a/app/models/spree/payment.rb +++ b/app/models/spree/payment.rb @@ -155,8 +155,8 @@ module Spree return [] unless payment_method.respond_to?(:actions) payment_method.actions.select do |action| - !payment_source.respond_to?("can_#{action}?") || - payment_source.__send__("can_#{action}?", self) + !payment_method.respond_to?("can_#{action}?") || + payment_method.__send__("can_#{action}?", self) end end @@ -166,11 +166,6 @@ module Spree PaymentMailer.authorize_payment(self).deliver_later end - def payment_source - res = source.is_a?(Payment) ? source.source : source - res || payment_method - end - def ensure_correct_adjustment revoke_adjustment_eligibility if ['failed', 'invalid', 'void'].include?(state) return if adjustment.try(:finalized?) diff --git a/spec/models/spree/credit_card_spec.rb b/spec/models/spree/credit_card_spec.rb index 29f1fdf753..7a7db93886 100644 --- a/spec/models/spree/credit_card_spec.rb +++ b/spec/models/spree/credit_card_spec.rb @@ -21,53 +21,6 @@ RSpec.describe Spree::CreditCard do let(:credit_card) { described_class.new } - context "#can_capture?" do - it "should be true if payment is pending" do - payment = build_stubbed(:payment, created_at: Time.zone.now) - allow(payment).to receive(:pending?) { true } - expect(credit_card.can_capture_and_complete_order?(payment)).to be_truthy - end - - it "should be true if payment is checkout" do - payment = build_stubbed(:payment, created_at: Time.zone.now) - allow(payment).to receive_messages pending?: false, - checkout?: true - expect(credit_card.can_capture_and_complete_order?(payment)).to be_truthy - end - end - - context "#can_void?" do - it "should be true if payment is not void" do - payment = build_stubbed(:payment) - allow(payment).to receive(:void?) { false } - expect(credit_card.can_void?(payment)).to be_truthy - end - end - - context "#can_credit?" do - it "should be false if payment is not completed" do - payment = build_stubbed(:payment) - allow(payment).to receive(:completed?) { false } - expect(credit_card.can_credit?(payment)).to be_falsy - end - - it "should be false when order payment_state is not 'credit_owed'" do - payment = build_stubbed(:payment, - order: create(:order, payment_state: 'paid')) - allow(payment).to receive(:completed?) { true } - expect(credit_card.can_credit?(payment)).to be_falsy - end - - it "should be false when credit_allowed is zero" do - payment = build_stubbed(:payment, - order: create(:order, payment_state: 'credit_owed')) - allow(payment).to receive_messages completed?: true, - credit_allowed: 0 - - expect(credit_card.can_credit?(payment)).to be_falsy - end - end - context "#valid?" do it "should validate presence of number" do credit_card.attributes = valid_credit_card_attributes.except(:number) diff --git a/spec/models/spree/gateway_spec.rb b/spec/models/spree/gateway_spec.rb index f6e975ba38..3ff9dc0741 100644 --- a/spec/models/spree/gateway_spec.rb +++ b/spec/models/spree/gateway_spec.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true RSpec.describe Spree::Gateway do + subject(:gateway) { test_gateway.new } let(:test_gateway) do Class.new(Spree::Gateway) do def provider_class @@ -15,13 +16,58 @@ RSpec.describe Spree::Gateway do it "passes through all arguments on a method_missing call" do expect(Rails.env).to receive(:local?).and_return(false) - gateway = test_gateway.new expect(gateway.provider).to receive(:imaginary_method).with('foo') gateway.imaginary_method('foo') end it "raises an error in test env" do - gateway = test_gateway.new expect { gateway.imaginary_method('foo') }.to raise_error StandardError end + + describe "#can_capture?" do + it "should be true if payment is pending" do + payment = build_stubbed(:payment, created_at: Time.zone.now) + allow(payment).to receive(:pending?) { true } + expect(gateway.can_capture_and_complete_order?(payment)).to be_truthy + end + + it "should be true if payment is checkout" do + payment = build_stubbed(:payment, created_at: Time.zone.now) + allow(payment).to receive_messages pending?: false, + checkout?: true + expect(gateway.can_capture_and_complete_order?(payment)).to be_truthy + end + end + + describe "#can_void?" do + it "should be true if payment is not void" do + payment = build_stubbed(:payment) + allow(payment).to receive(:void?) { false } + expect(gateway.can_void?(payment)).to be_truthy + end + end + + describe "#can_credit?" do + it "should be false if payment is not completed" do + payment = build_stubbed(:payment) + allow(payment).to receive(:completed?) { false } + expect(gateway.can_credit?(payment)).to be_falsy + end + + it "should be false when order payment_state is not 'credit_owed'" do + payment = build_stubbed(:payment, + order: create(:order, payment_state: 'paid')) + allow(payment).to receive(:completed?) { true } + expect(gateway.can_credit?(payment)).to be_falsy + end + + it "should be false when credit_allowed is zero" do + payment = build_stubbed(:payment, + order: create(:order, payment_state: 'credit_owed')) + allow(payment).to receive_messages completed?: true, + credit_allowed: 0 + + expect(gateway.can_credit?(payment)).to be_falsy + end + end end diff --git a/spec/requests/spree/admin/payments_spec.rb b/spec/requests/spree/admin/payments_spec.rb index fc4abdaf0f..283daf16e1 100644 --- a/spec/requests/spree/admin/payments_spec.rb +++ b/spec/requests/spree/admin/payments_spec.rb @@ -157,8 +157,6 @@ RSpec.describe Spree::Admin::PaymentsController do context "with no payment source" do it "redirect to payments page" do - allow(payment).to receive(:payment_source).and_return(nil) - put( "/admin/orders/#{order.number}/payments/#{order.payments.first.id}/fire?e=void", params: {},