2023-03-16 17:17:00 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods to help testing payment schedules
|
|
|
|
module PaymentScheduleHelper
|
|
|
|
# Force the payment schedule generation worker to run NOW and check the resulting file generated.
|
|
|
|
# Delete the file afterwards.
|
|
|
|
# @param schedule [PaymentSchedule]
|
|
|
|
def assert_schedule_pdf(schedule)
|
|
|
|
assert_not_nil schedule, 'Schedule was not created'
|
|
|
|
|
|
|
|
generate_schedule_pdf(schedule)
|
|
|
|
|
|
|
|
assert File.exist?(schedule.file), 'Schedule PDF was not generated'
|
|
|
|
|
|
|
|
File.delete(schedule.file)
|
|
|
|
end
|
|
|
|
|
|
|
|
# @param customer [User]
|
|
|
|
# @param operator [User]
|
|
|
|
# @return [PaymentSchedule] saved
|
|
|
|
def sample_schedule(customer, operator)
|
|
|
|
plan = plans(:plan_schedulable)
|
|
|
|
subscription = Subscription.new(plan: plan, statistic_profile_id: customer.statistic_profile, start_at: Time.current)
|
|
|
|
subscription.save
|
|
|
|
options = { payment_method: '' }
|
|
|
|
unless operator.privileged?
|
|
|
|
options = { payment_method: 'card', payment_id: 'pi_3LpALs2sOmf47Nz91QyFI7nP', payment_type: 'Stripe::PaymentIntent' }
|
|
|
|
end
|
|
|
|
schedule = PaymentScheduleService.new.create([subscription], 113_600, customer, operator: operator, **options)
|
2023-03-21 15:11:08 +01:00
|
|
|
schedule.save!
|
2023-03-16 17:17:00 +01:00
|
|
|
first_item = schedule.ordered_items.first
|
|
|
|
PaymentScheduleService.new.generate_invoice(first_item, **options)
|
2023-03-21 15:11:08 +01:00
|
|
|
first_item.update!(state: 'paid', payment_method: operator.privileged? ? 'check' : 'card')
|
2023-03-16 17:17:00 +01:00
|
|
|
schedule
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_schedule_pdf(schedule)
|
|
|
|
schedule_worker = PaymentScheduleWorker.new
|
|
|
|
schedule_worker.perform(schedule.id)
|
|
|
|
end
|
|
|
|
end
|