diff --git a/app/helpers/enterprises_helper.rb b/app/helpers/enterprises_helper.rb index 37df04b685..94c3901f8a 100644 --- a/app/helpers/enterprises_helper.rb +++ b/app/helpers/enterprises_helper.rb @@ -22,8 +22,7 @@ module EnterprisesHelper return [] unless current_distributor.present? payment_methods = current_distributor.payment_methods.available(:front_end).all - stripe_enabled = Spree::Config.stripe_connect_enabled && Stripe.publishable_key - payment_methods.reject!{ |p| p.type.ends_with? "StripeConnect" } unless stripe_enabled + apply_stripe_filters_to(payment_methods) applicator = OpenFoodNetwork::TagRuleApplicator.new(current_distributor, "FilterPaymentMethods", current_customer.andand.tag_list) applicator.filter!(payment_methods) @@ -99,4 +98,13 @@ module EnterprisesHelper def show_bought_items? order_changes_allowed? && current_order.finalised_line_items.present? end + + def apply_stripe_filters_to(payment_methods) + stripe_enabled = Spree::Config.stripe_connect_enabled && Stripe.publishable_key + if stripe_enabled + payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") && p.preferred_enterprise_id == 0 } + else + payment_methods.reject!{ |p| p.type.ends_with?("StripeConnect") } + end + end end diff --git a/spec/helpers/enterprises_helper_spec.rb b/spec/helpers/enterprises_helper_spec.rb index ac2307f94f..9ab6d1d17d 100644 --- a/spec/helpers/enterprises_helper_spec.rb +++ b/spec/helpers/enterprises_helper_spec.rb @@ -221,14 +221,19 @@ describe EnterprisesHelper do end context "when StripeConnect payment methods are present" do - let!(:pm3) { create(:payment_method, type: "Spree::Gateway::StripeConnect", distributors: [distributor])} - before { allow(helper).to receive(:current_distributor) { distributor } } + 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) } + + 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 the Stripe payment method" do - expect(helper.available_payment_methods.map(&:id)).to_not include pm3.id + it "ignores Stripe payment methods" do + expect(helper.available_payment_methods.map(&:id)).to_not include pm3.id, pm4.id end end @@ -238,8 +243,9 @@ describe EnterprisesHelper do allow(Stripe).to receive(:publishable_key) { "some_key" } end - it "includes the Stripe payment method" do + 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 end end end