89 lines
2.4 KiB
Ruby
89 lines
2.4 KiB
Ruby
|
|
module JamRuby
|
||
|
|
class LessonPaymentCharge < Charge
|
||
|
|
|
||
|
|
has_one :lesson_session, class_name: "JamRuby::LessonSession", foreign_key: :charge_id
|
||
|
|
has_one :lesson_package_purchase, class_name: "JamRuby::LessonPackagePurchase", foreign_key: :charge_id
|
||
|
|
|
||
|
|
def max_retries
|
||
|
|
5
|
||
|
|
end
|
||
|
|
|
||
|
|
def charged_user
|
||
|
|
@charged_user ||= target.student
|
||
|
|
end
|
||
|
|
|
||
|
|
def resolve_target
|
||
|
|
if is_lesson?
|
||
|
|
lesson_session
|
||
|
|
else
|
||
|
|
lesson_package_purchase
|
||
|
|
end
|
||
|
|
end
|
||
|
|
def target
|
||
|
|
@target ||= resolve_target
|
||
|
|
end
|
||
|
|
|
||
|
|
def lesson_booking
|
||
|
|
@lesson_booking ||= target.lesson_booking
|
||
|
|
end
|
||
|
|
|
||
|
|
def student
|
||
|
|
charged_user
|
||
|
|
end
|
||
|
|
|
||
|
|
def is_lesson?
|
||
|
|
!lesson_session.nil?
|
||
|
|
end
|
||
|
|
|
||
|
|
def do_charge(force)
|
||
|
|
|
||
|
|
if is_lesson?
|
||
|
|
result = Sale.purchase_lesson(student, lesson_booking, lesson_booking.lesson_package_type, lesson_session)
|
||
|
|
else
|
||
|
|
result = Sale.purchase_lesson(student, lesson_booking, lesson_booking.lesson_package_type, nil, lesson_package_purchase, force)
|
||
|
|
lesson_booking.unsuspend! if lesson_booking.is_suspended?
|
||
|
|
end
|
||
|
|
|
||
|
|
stripe_charge = result[:stripe_charge]
|
||
|
|
|
||
|
|
self.amount_in_cents = stripe_charge.amount
|
||
|
|
self.save(validate: false)
|
||
|
|
|
||
|
|
# update teacher distribution, because it's now ready to be given to them!
|
||
|
|
|
||
|
|
distribution = target.teacher_distribution
|
||
|
|
if distribution # not all lessons/payment charges have a distribution
|
||
|
|
distribution.ready = true
|
||
|
|
distribution.save(validate: false)
|
||
|
|
end
|
||
|
|
|
||
|
|
stripe_charge
|
||
|
|
end
|
||
|
|
|
||
|
|
def do_send_notices
|
||
|
|
if is_lesson?
|
||
|
|
UserMailer.student_lesson_normal_done(lesson_session).deliver
|
||
|
|
UserMailer.teacher_lesson_normal_done(lesson_session).deliver
|
||
|
|
else
|
||
|
|
UserMailer.student_lesson_monthly_charged(lesson_package_purchase).deliver
|
||
|
|
UserMailer.teacher_lesson_monthly_charged(lesson_package_purchase).deliver
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
def do_send_unable_charge
|
||
|
|
if is_lesson?
|
||
|
|
UserMailer.student_unable_charge(lesson_session)
|
||
|
|
else
|
||
|
|
if !billing_should_retry
|
||
|
|
lesson_booking.suspend!
|
||
|
|
end
|
||
|
|
|
||
|
|
UserMailer.student_unable_charge_monthly(lesson_package_purchase)
|
||
|
|
if lesson_booking.is_suspended?
|
||
|
|
# let the teacher know that we are having problems collecting from the student
|
||
|
|
UserMailer.teacher_unable_charge_monthly(lesson_package_purchase)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|