1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/services/checkout/payment_service.rb

44 lines
1.6 KiB
Ruby
Raw Normal View History

2022-08-25 08:52:17 +02:00
# frozen_string_literal: true
# Provides methods to pay the cart
2022-08-25 08:52:17 +02:00
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 = '')
raise Cart::InactiveProductError unless Orders::OrderService.all_products_is_active?(order)
raise Cart::OutStockError unless Orders::OrderService.in_stock?(order, 'external')
raise Cart::QuantityMinError unless Orders::OrderService.greater_than_quantity_min?(order)
raise Cart::ItemAmountError unless Orders::OrderService.item_amount_not_equal?(order)
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)
2022-11-07 18:51:49 +01:00
if (operator.privileged? && operator != order.statistic_profile.user) || amount.zero?
2022-08-26 20:10:21 +02:00
Payments::LocalService.new.payment(order, coupon_code)
2022-11-07 18:51:49 +01:00
elsif Stripe::Helper.enabled? && payment_id.present?
Payments::StripeService.new.payment(order, coupon_code, payment_id)
elsif PayZen::Helper.enabled?
Payments::PayzenService.new.payment(order, coupon_code)
else
raise PaymentGatewayError, 'Bad gateway or online payment is disabled'
2022-08-25 08:52:17 +02:00
end
end
2022-08-26 20:10:21 +02:00
def confirm_payment(order, operator, coupon_code, payment_id = '')
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 PaymentGatewayError, 'Bad gateway or online payment is disabled'
2022-08-25 08:52:17 +02:00
end
end
end