2022-08-25 08:52:17 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Concern for Payment
|
|
|
|
module Payments::PaymentConcern
|
|
|
|
private
|
|
|
|
|
|
|
|
def get_wallet_debit(user, total_amount)
|
|
|
|
wallet_amount = (user.wallet.amount * 100).to_i
|
|
|
|
wallet_amount >= total_amount ? total_amount : wallet_amount
|
|
|
|
end
|
|
|
|
|
2022-08-26 20:10:21 +02:00
|
|
|
def debit_amount(order, coupon_code = nil)
|
2022-09-07 17:24:14 +02:00
|
|
|
total = CouponService.new.apply(order.total, coupon_code, order.statistic_profile.user.id)
|
2022-08-25 08:52:17 +02:00
|
|
|
wallet_debit = get_wallet_debit(order.statistic_profile.user, total)
|
|
|
|
total - wallet_debit
|
|
|
|
end
|
|
|
|
|
2022-08-26 20:10:21 +02:00
|
|
|
def payment_success(order, coupon_code, payment_method = '', payment_id = nil, payment_type = nil)
|
2022-08-25 08:52:17 +02:00
|
|
|
ActiveRecord::Base.transaction do
|
2022-08-26 20:10:21 +02:00
|
|
|
order.paid_total = debit_amount(order, coupon_code)
|
|
|
|
coupon = Coupon.find_by(code: coupon_code)
|
|
|
|
order.coupon_id = coupon.id if coupon
|
2022-08-25 08:52:17 +02:00
|
|
|
WalletService.debit_user_wallet(order, order.statistic_profile.user)
|
2022-08-26 14:07:18 +02:00
|
|
|
order.operator_profile_id = order.statistic_profile.user.invoicing_profile.id if order.operator_profile.nil?
|
2022-08-26 13:37:23 +02:00
|
|
|
order.payment_method = if order.total == order.wallet_amount
|
|
|
|
'wallet'
|
|
|
|
else
|
|
|
|
payment_method
|
|
|
|
end
|
2022-09-14 19:54:24 +02:00
|
|
|
order.state = 'paid'
|
2023-02-16 11:22:42 +01:00
|
|
|
# we change the order creation date to the date of payment to be more consistent for admins when filtering on orders
|
2023-02-14 13:10:58 +01:00
|
|
|
order.created_at = Time.current
|
2022-08-26 15:30:51 +02:00
|
|
|
if payment_id && payment_type
|
|
|
|
order.payment_gateway_object = PaymentGatewayObject.new(gateway_object_id: payment_id, gateway_object_type: payment_type)
|
|
|
|
end
|
2023-02-16 11:22:42 +01:00
|
|
|
activity = order.order_activities.create(activity_type: 'paid', operator_profile_id: order.operator_profile_id)
|
2022-08-25 08:52:17 +02:00
|
|
|
order.order_items.each do |item|
|
2022-09-13 15:01:55 +02:00
|
|
|
ProductService.update_stock(item.orderable,
|
|
|
|
[{ stock_type: 'external', reason: 'sold', quantity: item.quantity, order_item_id: item.id }]).save
|
2022-09-09 16:35:49 +02:00
|
|
|
end
|
2023-02-16 11:22:42 +01:00
|
|
|
if order.save
|
|
|
|
create_invoice(order, coupon, payment_id, payment_type)
|
|
|
|
NotificationCenter.call type: 'notify_admin_order_is_paid',
|
|
|
|
receiver: User.admins_and_managers,
|
|
|
|
attached_object: activity
|
|
|
|
end
|
2022-08-25 08:52:17 +02:00
|
|
|
order.reload
|
|
|
|
end
|
|
|
|
end
|
2022-09-14 15:15:47 +02:00
|
|
|
|
|
|
|
def create_invoice(order, coupon, payment_id, payment_type)
|
|
|
|
invoice = InvoicesService.create(
|
|
|
|
{ total: order.total, coupon: coupon },
|
|
|
|
order.operator_profile_id,
|
|
|
|
order.order_items,
|
|
|
|
order.statistic_profile.user,
|
|
|
|
payment_id: payment_id,
|
|
|
|
payment_type: payment_type,
|
|
|
|
payment_method: order.payment_method
|
|
|
|
)
|
|
|
|
invoice.wallet_amount = order.wallet_amount
|
|
|
|
invoice.wallet_transaction_id = order.wallet_transaction_id
|
2023-03-27 11:43:36 +02:00
|
|
|
invoice.order = order
|
2022-12-22 17:18:34 +01:00
|
|
|
invoice.save unless Setting.get('prevent_invoices_zero') && order.total.zero?
|
2022-09-14 15:15:47 +02:00
|
|
|
end
|
2022-08-25 08:52:17 +02:00
|
|
|
end
|