2019-09-05 11:03:22 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
# Abstract API Controller to be extended by each payment gateway/mean, for handling the payments processes in the front-end
|
2023-02-24 17:26:55 +01:00
|
|
|
class API::PaymentsController < API::APIController
|
2019-09-05 11:03:22 +02:00
|
|
|
before_action :authenticate_user!
|
|
|
|
|
2021-04-13 17:16:05 +02:00
|
|
|
# This method must be overridden by the the gateways controllers that inherits API::PaymentsControllers
|
|
|
|
def confirm_payment
|
|
|
|
raise NoMethodError
|
|
|
|
end
|
|
|
|
|
2021-04-09 17:17:58 +02:00
|
|
|
protected
|
2019-09-09 17:37:54 +02:00
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
def post_save(_gateway_item_id, _gateway_item_type, _payment_document); end
|
2021-04-26 17:42:03 +02:00
|
|
|
|
2019-09-10 11:46:14 +02:00
|
|
|
def get_wallet_debit(user, total_amount)
|
|
|
|
wallet_amount = (user.wallet.amount * 100).to_i
|
|
|
|
wallet_amount >= total_amount ? total_amount : wallet_amount
|
|
|
|
end
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
def debit_amount(cart)
|
2021-04-23 12:52:06 +02:00
|
|
|
price_details = cart.total
|
2019-09-11 10:31:22 +02:00
|
|
|
|
|
|
|
# Subtract wallet amount from total
|
|
|
|
total = price_details[:total]
|
|
|
|
wallet_debit = get_wallet_debit(current_user, total)
|
2020-05-11 10:56:23 +02:00
|
|
|
{ amount: total - wallet_debit, details: price_details }
|
2019-09-11 10:31:22 +02:00
|
|
|
end
|
|
|
|
|
2021-05-19 18:12:52 +02:00
|
|
|
def shopping_cart
|
|
|
|
cs = CartService.new(current_user)
|
|
|
|
cs.from_hash(params[:cart_items])
|
2019-09-11 10:31:22 +02:00
|
|
|
end
|
|
|
|
|
2021-05-28 17:34:20 +02:00
|
|
|
def on_payment_success(gateway_item_id, gateway_item_type, cart)
|
|
|
|
res = cart.build_and_save(gateway_item_id, gateway_item_type)
|
|
|
|
if res[:success]
|
|
|
|
post_save(gateway_item_id, gateway_item_type, res[:payment])
|
|
|
|
res[:payment].render_resource.merge(status: :created)
|
2021-04-26 17:42:03 +02:00
|
|
|
else
|
2021-10-07 17:07:46 +02:00
|
|
|
{ json: res[:errors].drop_while(&:empty?), status: :unprocessable_entity }
|
2021-04-26 17:42:03 +02:00
|
|
|
end
|
2022-12-27 17:15:49 +01:00
|
|
|
rescue StandardError => e
|
2022-12-28 17:51:27 +01:00
|
|
|
Rails.logger.debug e.backtrace
|
2022-12-27 17:15:49 +01:00
|
|
|
{ json: e, status: :unprocessable_entity }
|
2021-04-26 17:42:03 +02:00
|
|
|
end
|
2019-09-09 18:04:31 +02:00
|
|
|
end
|