2022-08-25 08:52:17 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for pay cart
|
|
|
|
class Checkout::PaymentService
|
|
|
|
require 'pay_zen/helper'
|
|
|
|
require 'stripe/helper'
|
2022-08-26 13:37:23 +02:00
|
|
|
include Payments::PaymentConcern
|
2022-08-25 08:52:17 +02:00
|
|
|
|
2022-08-26 20:10:21 +02:00
|
|
|
def payment(order, operator, coupon_code, payment_id = '')
|
2022-08-25 11:46:14 +02:00
|
|
|
raise Cart::OutStockError unless Orders::OrderService.new.in_stock?(order, 'external')
|
|
|
|
|
2022-08-26 10:46:30 +02:00
|
|
|
raise Cart::InactiveProductError unless Orders::OrderService.new.all_products_is_active?(order)
|
2022-08-25 11:46:14 +02:00
|
|
|
|
2022-09-07 17:24:14 +02:00
|
|
|
CouponService.new.validate(coupon_code, order.statistic_profile.user.id)
|
2022-08-26 20:10:21 +02:00
|
|
|
|
2022-08-26 13:37:23 +02:00
|
|
|
amount = debit_amount(order)
|
|
|
|
if operator.privileged? || amount.zero?
|
2022-08-26 20:10:21 +02:00
|
|
|
Payments::LocalService.new.payment(order, coupon_code)
|
2022-08-26 13:37:23 +02:00
|
|
|
elsif operator.member?
|
2022-08-25 08:52:17 +02:00
|
|
|
if Stripe::Helper.enabled?
|
2022-08-26 20:10:21 +02:00
|
|
|
Payments::StripeService.new.payment(order, coupon_code, payment_id)
|
2022-08-25 08:52:17 +02:00
|
|
|
elsif PayZen::Helper.enabled?
|
2022-08-26 20:10:21 +02:00
|
|
|
Payments::PayzenService.new.payment(order, coupon_code)
|
2022-08-25 08:52:17 +02:00
|
|
|
else
|
|
|
|
raise Error('Bad gateway or online payment is disabled')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-26 20:10:21 +02:00
|
|
|
def confirm_payment(order, operator, coupon_code, payment_id = '')
|
2022-09-14 15:15:47 +02:00
|
|
|
return unless operator.member?
|
|
|
|
|
|
|
|
if Stripe::Helper.enabled?
|
|
|
|
Payments::StripeService.new.confirm_payment(order, coupon_code, payment_id)
|
|
|
|
elsif PayZen::Helper.enabled?
|
|
|
|
Payments::PayzenService.new.confirm_payment(order, coupon_code, payment_id)
|
|
|
|
else
|
|
|
|
raise Error('Bad gateway or online payment is disabled')
|
2022-08-25 08:52:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|