Hide Stripe payment methods without a preferred_enterprise_id from the front-end

This commit is contained in:
Rob Harrington
2017-09-08 10:45:30 +10:00
parent ca1987fc87
commit 0cd43987de
2 changed files with 21 additions and 7 deletions

View File

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

View File

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