mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-14 04:04:23 +00:00
Let people choose which payment methods are available to customers on order cycles
This commit is contained in:
217
spec/services/order_available_payment_methods_spec.rb
Normal file
217
spec/services/order_available_payment_methods_spec.rb
Normal file
@@ -0,0 +1,217 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe OrderAvailablePaymentMethods do
|
||||
context "when the order has no current_distributor" do
|
||||
it "returns an empty array" do
|
||||
order_cycle = create(:sells_own_order_cycle)
|
||||
order = build(:order, distributor: nil, order_cycle: order_cycle)
|
||||
|
||||
expect(OrderAvailablePaymentMethods.new(order).to_a).to eq []
|
||||
end
|
||||
end
|
||||
|
||||
it "does not return 'back office only' payment method" do
|
||||
distributor = create(:distributor_enterprise)
|
||||
frontend_payment_method = create(:payment_method, distributors: [distributor])
|
||||
backoffice_only_payment_method = create(:payment_method,
|
||||
distributors: [distributor],
|
||||
display_on: 'back_end')
|
||||
order_cycle = create(:sells_own_order_cycle)
|
||||
order = build(:order, distributor: distributor, order_cycle: order_cycle)
|
||||
|
||||
available_payment_methods = OrderAvailablePaymentMethods.new(order).to_a
|
||||
|
||||
expect(available_payment_methods).to eq [frontend_payment_method]
|
||||
end
|
||||
|
||||
it "does not return payment methods which are not configured correctly" do
|
||||
distributor = create(:distributor_enterprise)
|
||||
frontend_payment_method = create(:payment_method, distributors: [distributor])
|
||||
unconfigured_payment_method = create(:stripe_sca_payment_method,
|
||||
distributors: [distributor],
|
||||
display_on: 'back_end')
|
||||
order_cycle = create(:sells_own_order_cycle)
|
||||
order = build(:order, distributor: distributor, order_cycle: order_cycle)
|
||||
|
||||
available_payment_methods = OrderAvailablePaymentMethods.new(order).to_a
|
||||
|
||||
expect(available_payment_methods).to eq [frontend_payment_method]
|
||||
end
|
||||
|
||||
context "when no tag rules are in effect" do
|
||||
context "sells own order cycle i.e. simple" do
|
||||
it "only returns the payment methods which are available on the order cycle
|
||||
and belong to the order distributor" do
|
||||
distributor_i = create(:distributor_enterprise)
|
||||
distributor_ii = create(:distributor_enterprise)
|
||||
distributor_iii = create(:distributor_enterprise)
|
||||
payment_method_i = create(:payment_method, distributors: [distributor_i])
|
||||
payment_method_ii = create(:payment_method, distributors: [distributor_ii])
|
||||
payment_method_iii = create(:payment_method, distributors: [distributor_iii])
|
||||
order_cycle = create(:sells_own_order_cycle, distributors: [distributor_i, distributor_ii])
|
||||
order = build(:order, distributor: distributor_i, order_cycle: order_cycle)
|
||||
|
||||
available_payment_methods = OrderAvailablePaymentMethods.new(order).to_a
|
||||
|
||||
expect(available_payment_methods).to eq [payment_method_i]
|
||||
end
|
||||
end
|
||||
|
||||
context "distributor order cycle i.e. not simple" do
|
||||
it "only returns the payment methods which are available on the order cycle
|
||||
and belong to the order distributor" do
|
||||
distributor_i = create(:distributor_enterprise, payment_methods: [])
|
||||
distributor_ii = create(:distributor_enterprise, payment_methods: [])
|
||||
payment_method_i = create(:payment_method, distributors: [distributor_i])
|
||||
payment_method_ii = create(:payment_method, distributors: [distributor_i])
|
||||
payment_method_iii = create(:payment_method, distributors: [distributor_ii])
|
||||
payment_method_iv = create(:payment_method, distributors: [distributor_ii])
|
||||
order_cycle = create(:distributor_order_cycle,
|
||||
distributors: [distributor_i, distributor_ii])
|
||||
order_cycle.selected_distributor_payment_methods << [
|
||||
distributor_i.distributor_payment_methods.first,
|
||||
distributor_ii.distributor_payment_methods.first,
|
||||
]
|
||||
order = build(:order, distributor: distributor_i, order_cycle: order_cycle)
|
||||
|
||||
available_payment_methods = OrderAvailablePaymentMethods.new(order).to_a
|
||||
|
||||
expect(available_payment_methods).to eq [payment_method_i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when FilterPaymentMethods tag rules are in effect" do
|
||||
let(:user) { create(:user) }
|
||||
let(:distributor) { create(:distributor_enterprise) }
|
||||
let(:other_distributor) { create(:distributor_enterprise) }
|
||||
let!(:distributor_payment_method) { create(:payment_method, distributors: [distributor]) }
|
||||
let!(:other_distributor_payment_method) do
|
||||
create(:payment_method, distributors: [other_distributor])
|
||||
end
|
||||
let(:customer) { create(:customer, user: user, enterprise: distributor) }
|
||||
let!(:tag_rule) {
|
||||
create(:filter_payment_methods_tag_rule,
|
||||
enterprise: distributor,
|
||||
preferred_customer_tags: "local",
|
||||
preferred_payment_method_tags: "local-delivery")
|
||||
}
|
||||
let!(:default_tag_rule) {
|
||||
create(:filter_payment_methods_tag_rule,
|
||||
enterprise: distributor,
|
||||
is_default: true,
|
||||
preferred_payment_method_tags: "local-delivery")
|
||||
}
|
||||
let!(:tagged_payment_method) { distributor_payment_method }
|
||||
let!(:untagged_payment_method) { other_distributor_payment_method }
|
||||
|
||||
before do
|
||||
tagged_payment_method.update_attribute(:tag_list, 'local-delivery')
|
||||
distributor.payment_methods = [tagged_payment_method, untagged_payment_method]
|
||||
end
|
||||
|
||||
context "with a preferred visiblity of 'visible', default visibility of 'hidden'" do
|
||||
before {
|
||||
tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'visible')
|
||||
}
|
||||
before {
|
||||
default_tag_rule.update_attribute(:preferred_matched_payment_methods_visibility,
|
||||
'hidden')
|
||||
}
|
||||
|
||||
let(:order_cycle) { create(:sells_own_order_cycle) }
|
||||
let(:order) { build(:order, distributor: distributor, order_cycle: order_cycle) }
|
||||
|
||||
context "when the customer is nil" do
|
||||
let(:available_payment_methods) { OrderAvailablePaymentMethods.new(order).to_a }
|
||||
|
||||
it "applies default action (hide)" do
|
||||
expect(available_payment_methods).to include untagged_payment_method
|
||||
expect(available_payment_methods).to_not include tagged_payment_method
|
||||
end
|
||||
end
|
||||
|
||||
context "when a customer is present" do
|
||||
let(:available_payment_methods) { OrderAvailablePaymentMethods.new(order, customer).to_a }
|
||||
|
||||
context "and the customer's tags match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'local')
|
||||
end
|
||||
|
||||
it "applies the action (show)" do
|
||||
expect(available_payment_methods).to include(
|
||||
tagged_payment_method,
|
||||
untagged_payment_method
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "and the customer's tags don't match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'something')
|
||||
end
|
||||
|
||||
it "applies the default action (hide)" do
|
||||
expect(available_payment_methods).to include untagged_payment_method
|
||||
expect(available_payment_methods).to_not include tagged_payment_method
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with a preferred visiblity of 'hidden', default visibility of 'visible'" do
|
||||
before {
|
||||
tag_rule.update_attribute(:preferred_matched_payment_methods_visibility, 'hidden')
|
||||
}
|
||||
before {
|
||||
default_tag_rule.update_attribute(:preferred_matched_payment_methods_visibility,
|
||||
'visible')
|
||||
}
|
||||
|
||||
let(:order_cycle) { create(:sells_own_order_cycle) }
|
||||
let(:order) { build(:order, distributor: distributor, order_cycle: order_cycle) }
|
||||
|
||||
context "when the customer is nil" do
|
||||
let(:available_payment_methods) { OrderAvailablePaymentMethods.new(order).to_a }
|
||||
|
||||
it "applies default action (show)" do
|
||||
expect(available_payment_methods).to include(
|
||||
tagged_payment_method,
|
||||
untagged_payment_method
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "when a customer is present" do
|
||||
let(:available_payment_methods) { OrderAvailablePaymentMethods.new(order, customer).to_a }
|
||||
|
||||
context "and the customer's tags match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'local')
|
||||
end
|
||||
|
||||
it "applies the action (hide)" do
|
||||
expect(available_payment_methods).to include untagged_payment_method
|
||||
expect(available_payment_methods).to_not include tagged_payment_method
|
||||
end
|
||||
end
|
||||
|
||||
context "and the customer's tags don't match" do
|
||||
before do
|
||||
customer.update_attribute(:tag_list, 'something')
|
||||
end
|
||||
|
||||
it "applies the default action (show)" do
|
||||
expect(available_payment_methods).to include(
|
||||
tagged_payment_method,
|
||||
untagged_payment_method
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -135,7 +135,9 @@ describe OrderCycleForm do
|
||||
let(:distributor) { order_cycle.coordinator }
|
||||
let(:supplier) { create(:supplier_enterprise) }
|
||||
let(:user) { distributor.owner }
|
||||
let(:payment_method) { create(:payment_method, distributors: [distributor]) }
|
||||
let(:shipping_method) { create(:shipping_method, distributors: [distributor]) }
|
||||
let(:distributor_payment_method) { payment_method.distributor_payment_methods.first }
|
||||
let(:distributor_shipping_method) { shipping_method.distributor_shipping_methods.first }
|
||||
let(:variant) { create(:variant, product: create(:product, supplier: supplier)) }
|
||||
let(:params) { { name: 'Some new name' } }
|
||||
@@ -151,14 +153,14 @@ describe OrderCycleForm do
|
||||
}
|
||||
end
|
||||
|
||||
context "basic update i.e. without exchanges or shipping methods" do
|
||||
context "basic update i.e. without exchanges or payment/shipping methods" do
|
||||
it do
|
||||
expect(form.save).to be true
|
||||
expect(order_cycle.name).to eq 'Some new name'
|
||||
end
|
||||
end
|
||||
|
||||
context "updating basics, incoming exchanges, outcoming exchanges
|
||||
context "updating basics, incoming exchanges, outcoming exchanges, payment_methods
|
||||
and shipping methods simultaneously" do
|
||||
before do
|
||||
params.merge!(
|
||||
@@ -171,21 +173,31 @@ describe OrderCycleForm do
|
||||
enterprise_fee_ids: []
|
||||
}],
|
||||
outgoing_exchanges: [outgoing_exchange_params],
|
||||
selected_distributor_payment_method_ids: [distributor_payment_method.id],
|
||||
selected_distributor_shipping_method_ids: [distributor_shipping_method.id]
|
||||
)
|
||||
end
|
||||
|
||||
it "saves everything i.e. the basics, incoming and outgoing exchanges and shipping methods" do
|
||||
it "saves everything i.e. the basics, incoming and outgoing exchanges, payment methods and
|
||||
shipping methods" do
|
||||
expect(form.save).to be true
|
||||
expect(order_cycle.name).to eq 'Some new name'
|
||||
expect(order_cycle.cached_incoming_exchanges.count).to eq 1
|
||||
expect(order_cycle.cached_outgoing_exchanges.count).to eq 1
|
||||
expect(order_cycle.distributor_payment_methods).to eq [distributor_payment_method]
|
||||
expect(order_cycle.distributor_shipping_methods).to eq [distributor_shipping_method]
|
||||
end
|
||||
end
|
||||
|
||||
context "updating outgoing exchanges and shipping methods simultaneously but the shipping
|
||||
method doesn't belong to the new or any existing order cycle distributor" do
|
||||
context "updating outgoing exchanges and shipping methods simultaneously but the payment
|
||||
and shipping methods don't belong to the new or any existing order cycle
|
||||
distributor" do
|
||||
let(:other_distributor_payment_method) do
|
||||
create(
|
||||
:payment_method,
|
||||
distributors: [create(:distributor_enterprise)]
|
||||
).distributor_payment_methods.first
|
||||
end
|
||||
let(:other_distributor_shipping_method) do
|
||||
create(
|
||||
:shipping_method,
|
||||
@@ -196,6 +208,7 @@ describe OrderCycleForm do
|
||||
before do
|
||||
params.merge!(
|
||||
outgoing_exchanges: [outgoing_exchange_params],
|
||||
selected_distributor_payment_method_ids: [other_distributor_payment_method.id],
|
||||
selected_distributor_shipping_method_ids: [other_distributor_shipping_method.id]
|
||||
)
|
||||
end
|
||||
@@ -203,10 +216,59 @@ describe OrderCycleForm do
|
||||
it "saves the outgoing exchange but ignores the shipping method" do
|
||||
expect(form.save).to be true
|
||||
expect(order_cycle.distributors).to eq [distributor]
|
||||
expect(order_cycle.distributor_payment_methods).to be_empty
|
||||
expect(order_cycle.distributor_shipping_methods).to be_empty
|
||||
end
|
||||
end
|
||||
|
||||
context "updating payment methods" do
|
||||
context "and it's valid" do
|
||||
it "saves the changes" do
|
||||
distributor = create(:distributor_enterprise)
|
||||
distributor_payment_method = create(
|
||||
:payment_method,
|
||||
distributors: [distributor]
|
||||
).distributor_payment_methods.first
|
||||
order_cycle = create(:distributor_order_cycle, distributors: [distributor])
|
||||
|
||||
form = OrderCycleForm.new(
|
||||
order_cycle,
|
||||
{ selected_distributor_payment_method_ids: [distributor_payment_method.id] },
|
||||
order_cycle.coordinator
|
||||
)
|
||||
|
||||
expect(form.save).to be true
|
||||
expect(order_cycle.distributor_payment_methods).to eq [distributor_payment_method]
|
||||
end
|
||||
end
|
||||
|
||||
context "with a payment method which doesn't belong to any distributor on the order cycle" do
|
||||
it "ignores it" do
|
||||
distributor_i = create(:distributor_enterprise)
|
||||
distributor_ii = create(:distributor_enterprise)
|
||||
distributor_payment_method_i = create(
|
||||
:payment_method,
|
||||
distributors: [distributor_i]
|
||||
).distributor_payment_methods.first
|
||||
distributor_payment_method_ii = create(
|
||||
:payment_method,
|
||||
distributors: [distributor_ii]
|
||||
).distributor_payment_methods.first
|
||||
order_cycle = create(:distributor_order_cycle,
|
||||
distributors: [distributor_i])
|
||||
|
||||
form = OrderCycleForm.new(
|
||||
order_cycle,
|
||||
{ selected_distributor_payment_method_ids: [distributor_payment_method_ii.id] },
|
||||
order_cycle.coordinator
|
||||
)
|
||||
|
||||
expect(form.save).to be true
|
||||
expect(order_cycle.distributor_payment_methods).to eq [distributor_payment_method_i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "updating shipping methods" do
|
||||
context "and it's valid" do
|
||||
it "saves the changes" do
|
||||
|
||||
Reference in New Issue
Block a user