diff --git a/lib/open_food_network/available_payment_method_filter.rb b/lib/open_food_network/available_payment_method_filter.rb index bb33fcd4bc..379beb22b5 100644 --- a/lib/open_food_network/available_payment_method_filter.rb +++ b/lib/open_food_network/available_payment_method_filter.rb @@ -2,7 +2,7 @@ module OpenFoodNetwork class AvailablePaymentMethodFilter def filter!(payment_methods) if stripe_enabled? - payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") && p.preferred_enterprise_id.zero? } + payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") && stripe_configuration_incomplete?(p) } else payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") } end @@ -13,5 +13,11 @@ module OpenFoodNetwork def stripe_enabled? Spree::Config.stripe_connect_enabled && Stripe.publishable_key end + + def stripe_configuration_incomplete?(payment_method) + return true if payment_method.preferred_enterprise_id.zero? + + payment_method.stripe_account_id.blank? + end end end diff --git a/spec/features/consumer/shopping/checkout_spec.rb b/spec/features/consumer/shopping/checkout_spec.rb index 5774b29d53..0a55324b37 100644 --- a/spec/features/consumer/shopping/checkout_spec.rb +++ b/spec/features/consumer/shopping/checkout_spec.rb @@ -159,6 +159,8 @@ feature "As a consumer I want to check out my cart", js: true, retry: 3 do gateway_customer_profile_id: "i_am_saved") end + let!(:stripe_account) { create(:stripe_account, enterprise_id: distributor.id, stripe_user_id: 'some_id') } + let(:response_mock) { { id: "ch_1234", object: "charge", amount: 2000} } before do diff --git a/spec/helpers/enterprises_helper_spec.rb b/spec/helpers/enterprises_helper_spec.rb index 9ab6d1d17d..f63cb49c0e 100644 --- a/spec/helpers/enterprises_helper_spec.rb +++ b/spec/helpers/enterprises_helper_spec.rb @@ -222,30 +222,32 @@ describe EnterprisesHelper do context "when StripeConnect payment methods are present" do let!(:pm3) { create(:stripe_payment_method, distributors: [distributor], preferred_enterprise_id: distributor.id) } - let!(:pm4) { create(:stripe_payment_method, distributors: [distributor], preferred_enterprise_id: distributor.id) } + let!(:pm4) { create(:stripe_payment_method, distributors: [distributor], preferred_enterprise_id: some_other_distributor.id) } + let(:available_payment_methods) { helper.available_payment_methods } before do allow(helper).to receive(:current_distributor) { distributor } - pm4.update_attribute(:preferred_enterprise_id, nil) end context "and Stripe Connect is disabled" do before { Spree::Config.set(stripe_connect_enabled: false) } it "ignores Stripe payment methods" do - expect(helper.available_payment_methods.map(&:id)).to_not include pm3.id, pm4.id + expect(available_payment_methods).to_not include pm3, pm4 end end context "and Stripe Connect is enabled" do + let!(:stripe_account) { create(:stripe_account, enterprise_id: distributor.id) } + before do Spree::Config.set(stripe_connect_enabled: true) allow(Stripe).to receive(:publishable_key) { "some_key" } end - it "includes Stripe payment methods with a preferred_enterprise_id" do - expect(helper.available_payment_methods.map(&:id)).to include pm3.id - expect(helper.available_payment_methods.map(&:id)).to_not include pm4.id + it "includes Stripe payment methods with a valid stripe accounts" do + expect(available_payment_methods).to include pm3 + expect(available_payment_methods).to_not include pm4 end end end