2021-04-30 16:07:19 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-05-31 15:39:56 +02:00
|
|
|
require 'stripe/helper'
|
|
|
|
require 'pay_zen/helper'
|
|
|
|
|
2021-04-30 16:07:19 +02:00
|
|
|
# create remote items on currently active payment gateway
|
|
|
|
class PaymentGatewayService
|
|
|
|
def initialize
|
2021-05-19 18:12:52 +02:00
|
|
|
service = if Stripe::Helper.enabled?
|
|
|
|
require 'stripe/service'
|
|
|
|
Stripe::Service
|
|
|
|
elsif PayZen::Helper.enabled?
|
|
|
|
require 'pay_zen/service'
|
|
|
|
PayZen::Service
|
|
|
|
else
|
|
|
|
require 'payment/service'
|
|
|
|
Payment::Service
|
|
|
|
end
|
|
|
|
@gateway = service.new
|
2021-04-30 16:07:19 +02:00
|
|
|
end
|
|
|
|
|
2021-10-15 17:31:01 +02:00
|
|
|
def create_subscription(payment_schedule, *args)
|
|
|
|
@gateway.create_subscription(payment_schedule, *args)
|
2021-04-30 16:07:19 +02:00
|
|
|
end
|
|
|
|
|
2022-01-03 17:13:35 +01:00
|
|
|
def cancel_subscription(payment_schedule)
|
|
|
|
gateway = service_for_payment_schedule(payment_schedule)
|
|
|
|
gateway.cancel_subscription(payment_schedule)
|
|
|
|
end
|
|
|
|
|
2021-06-14 15:14:14 +02:00
|
|
|
def create_user(user_id)
|
|
|
|
@gateway.create_user(user_id)
|
|
|
|
end
|
|
|
|
|
2021-04-30 16:07:19 +02:00
|
|
|
def create_coupon(coupon_id)
|
|
|
|
@gateway.create_coupon(coupon_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_coupon(coupon_id)
|
|
|
|
@gateway.delete_coupon(coupon_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_update_product(klass, id)
|
|
|
|
@gateway.create_or_update_product(klass, id)
|
|
|
|
end
|
2021-06-03 12:22:37 +02:00
|
|
|
|
|
|
|
def process_payment_schedule_item(payment_schedule_item)
|
2021-06-03 16:25:42 +02:00
|
|
|
gateway = service_for_payment_schedule(payment_schedule_item.payment_schedule)
|
|
|
|
gateway.process_payment_schedule_item(payment_schedule_item)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pay_payment_schedule_item(payment_schedule_item)
|
|
|
|
gateway = service_for_payment_schedule(payment_schedule_item.payment_schedule)
|
|
|
|
gateway.pay_payment_schedule_item(payment_schedule_item)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def service_for_payment_schedule(payment_schedule)
|
2022-01-03 17:13:35 +01:00
|
|
|
service = case payment_schedule.gateway_subscription&.klass
|
2021-06-03 12:22:37 +02:00
|
|
|
when /^PayZen::/
|
|
|
|
require 'pay_zen/service'
|
|
|
|
PayZen::Service
|
|
|
|
when /^Stripe::/
|
|
|
|
require 'stripe/service'
|
|
|
|
Stripe::Service
|
|
|
|
else
|
|
|
|
require 'payment/service'
|
|
|
|
Payment::Service
|
|
|
|
end
|
2021-06-03 16:25:42 +02:00
|
|
|
service.new
|
2021-06-03 12:22:37 +02:00
|
|
|
end
|
2021-04-30 16:07:19 +02:00
|
|
|
end
|