2021-04-22 19:24:08 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A payment schedule applied to plan in the shopping cart
|
|
|
|
class CartItem::PaymentSchedule
|
2022-06-07 16:55:24 +02:00
|
|
|
attr_reader :requested, :errors
|
2021-05-19 18:12:52 +02:00
|
|
|
|
2021-10-14 18:20:10 +02:00
|
|
|
def initialize(plan, coupon, requested, customer, start_at = nil)
|
2021-04-26 11:40:26 +02:00
|
|
|
raise TypeError unless coupon.is_a? CartItem::Coupon
|
2021-04-22 19:24:08 +02:00
|
|
|
|
|
|
|
@plan = plan
|
|
|
|
@coupon = coupon
|
|
|
|
@requested = requested
|
2021-10-14 18:20:10 +02:00
|
|
|
@customer = customer
|
|
|
|
@start_at = start_at
|
2022-06-07 16:55:24 +02:00
|
|
|
@errors = {}
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def schedule(total, total_without_coupon)
|
2021-04-27 17:18:20 +02:00
|
|
|
schedule = if @requested && @plan&.monthly_payment
|
2021-10-14 18:20:10 +02:00
|
|
|
PaymentScheduleService.new.compute(@plan, total_without_coupon, @customer, coupon: @coupon.coupon, start_at: @start_at)
|
2021-04-22 19:24:08 +02:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
total_amount = if schedule
|
|
|
|
schedule[:items][0].amount
|
|
|
|
else
|
|
|
|
total
|
|
|
|
end
|
|
|
|
|
|
|
|
{ schedule: schedule, total: total_amount }
|
|
|
|
end
|
2022-03-18 19:44:30 +01:00
|
|
|
|
|
|
|
def type
|
|
|
|
'subscription'
|
|
|
|
end
|
2022-06-07 16:55:24 +02:00
|
|
|
|
|
|
|
def valid?(_all_items)
|
|
|
|
if @plan&.disabled
|
|
|
|
@errors[:item] = 'plan is disabled'
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
2021-04-22 19:24:08 +02:00
|
|
|
end
|