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
|
|
|
|
|
|
|
|
def debit_amount(order)
|
2022-08-26 10:46:30 +02:00
|
|
|
total = order.total
|
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 15:30:51 +02:00
|
|
|
def payment_success(order, payment_method = '', payment_id = nil, payment_type = nil)
|
2022-08-25 08:52:17 +02:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
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
|
|
|
|
order.state = 'in_progress'
|
|
|
|
order.payment_state = 'paid'
|
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
|
2022-08-25 08:52:17 +02:00
|
|
|
order.order_items.each do |item|
|
2022-08-26 11:06:09 +02:00
|
|
|
ProductService.update_stock(item.orderable, 'external', 'sold', -item.quantity, item.id)
|
2022-08-25 08:52:17 +02:00
|
|
|
end
|
2022-08-26 15:56:20 +02:00
|
|
|
order.reference = order.generate_reference
|
2022-08-26 13:37:23 +02:00
|
|
|
order.save
|
2022-08-25 08:52:17 +02:00
|
|
|
order.reload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|