mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-26 20:56:48 +00:00
These factories are not used in testing and this way we avoid collisions on the root namespace as it was happening already with OrderFactory
59 lines
1.5 KiB
Ruby
59 lines
1.5 KiB
Ruby
require "tasks/sample_data/addressing"
|
|
require "tasks/sample_data/logging"
|
|
|
|
module SampleData
|
|
class PaymentMethodFactory
|
|
include Logging
|
|
include Addressing
|
|
|
|
def create_samples(enterprises)
|
|
log "Creating payment methods:"
|
|
distributors = enterprises.select(&:is_distributor)
|
|
distributors.each do |enterprise|
|
|
create_payment_methods(enterprise)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def create_payment_methods(enterprise)
|
|
return if enterprise.payment_methods.present?
|
|
|
|
log "- #{enterprise.name}"
|
|
create_cash_method(enterprise)
|
|
create_card_method(enterprise)
|
|
end
|
|
|
|
def create_cash_method(enterprise)
|
|
create_payment_method(
|
|
Spree::PaymentMethod::Check,
|
|
enterprise,
|
|
"Cash on collection",
|
|
"Pay on collection!",
|
|
::Calculator::FlatRate.new
|
|
)
|
|
end
|
|
|
|
def create_card_method(enterprise)
|
|
create_payment_method(
|
|
Spree::Gateway::Bogus,
|
|
enterprise,
|
|
"Credit card (fake)",
|
|
"We charge 1%, but won't ask for your details. ;-)",
|
|
::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 1)
|
|
)
|
|
end
|
|
|
|
def create_payment_method(provider_class, enterprise, name, description, calculator)
|
|
payment_method = provider_class.new(
|
|
name: name,
|
|
description: description,
|
|
environment: Rails.env,
|
|
distributor_ids: [enterprise.id]
|
|
)
|
|
calculator.calculable = payment_method
|
|
calculator.save!
|
|
end
|
|
end
|
|
end
|