2019-09-05 11:03:22 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-12 10:45:41 +02:00
|
|
|
# Abstract API Controller to be extended by each gateway, for handling the payments processes in the front-end
|
2019-09-05 11:03:22 +02:00
|
|
|
class API::PaymentsController < API::ApiController
|
|
|
|
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-04-26 17:42:03 +02:00
|
|
|
def post_reservation_save(_gateway_item_id, _gateway_item_type); end
|
|
|
|
|
|
|
|
def post_subscription_save(_gateway_item_id, _gateway_item_type); end
|
|
|
|
|
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
|
|
|
|
|
2019-09-11 10:31:22 +02:00
|
|
|
def card_amount
|
2021-04-23 12:52:06 +02:00
|
|
|
cs = CartService.new(current_user)
|
|
|
|
cart = cs.from_hash(params[:cart_items])
|
|
|
|
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
|
|
|
|
|
|
|
|
def check_coupon
|
|
|
|
return if coupon_params[:coupon_code].nil?
|
|
|
|
|
|
|
|
coupon = Coupon.find_by(code: coupon_params[:coupon_code])
|
|
|
|
raise InvalidCouponError if coupon.nil? || coupon.status(current_user.id) != 'active'
|
|
|
|
end
|
|
|
|
|
2019-09-11 16:19:24 +02:00
|
|
|
def check_plan
|
|
|
|
plan_id = if params[:cart_items][:subscription]
|
|
|
|
subscription_params[:plan_id]
|
|
|
|
elsif params[:cart_items][:reservation]
|
|
|
|
reservation_params[:plan_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless plan_id
|
|
|
|
|
|
|
|
plan = Plan.find(plan_id)
|
|
|
|
raise InvalidGroupError if plan.group_id != current_user.group_id
|
|
|
|
end
|
|
|
|
|
2021-04-26 17:42:03 +02:00
|
|
|
def on_reservation_success(gateway_item_id, gateway_item_type, details)
|
|
|
|
@reservation = Reservation.new(reservation_params)
|
|
|
|
if params[:cart_items][:subscription] && params[:cart_items][:subscription][:plan_id]
|
|
|
|
@reservation.plan_id = params[:cart_items][:subscription][:plan_id]
|
|
|
|
end
|
|
|
|
payment_method = params[:cart_items][:reservation][:payment_method] || 'card'
|
|
|
|
user_id = if current_user.admin? || current_user.manager?
|
|
|
|
params[:cart_items][:reservation][:user_id]
|
|
|
|
else
|
|
|
|
current_user.id
|
|
|
|
end
|
|
|
|
is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id)
|
|
|
|
.pay_and_save(@reservation,
|
|
|
|
payment_details: details,
|
|
|
|
payment_id: gateway_item_id,
|
|
|
|
payment_type: gateway_item_type,
|
|
|
|
schedule: params[:cart_items][:reservation][:payment_schedule],
|
|
|
|
payment_method: payment_method)
|
|
|
|
post_reservation_save(gateway_item_id, gateway_item_type)
|
|
|
|
|
|
|
|
if is_reserve
|
|
|
|
SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible
|
|
|
|
|
|
|
|
{ template: 'api/reservations/show', status: :created, location: @reservation }
|
|
|
|
else
|
|
|
|
{ json: @reservation.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_subscription_success(gateway_item_id, gateway_item_type, details)
|
|
|
|
@subscription = Subscription.new(subscription_params)
|
|
|
|
user_id = if current_user.admin? || current_user.manager?
|
|
|
|
params[:cart_items][:subscription][:user_id]
|
|
|
|
else
|
|
|
|
current_user.id
|
|
|
|
end
|
|
|
|
is_subscribe = Subscriptions::Subscribe.new(current_user.invoicing_profile.id, user_id)
|
|
|
|
.pay_and_save(@subscription,
|
|
|
|
payment_details: details,
|
|
|
|
payment_id: gateway_item_id,
|
|
|
|
payment_type: gateway_item_type,
|
|
|
|
schedule: params[:cart_items][:subscription][:payment_schedule],
|
|
|
|
payment_method: 'card')
|
|
|
|
|
|
|
|
post_subscription_save(gateway_item_id, gateway_item_type)
|
|
|
|
|
|
|
|
if is_subscribe
|
|
|
|
{ template: 'api/subscriptions/show', status: :created, location: @subscription }
|
|
|
|
else
|
|
|
|
{ json: @subscription.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-09 17:37:54 +02:00
|
|
|
def reservation_params
|
|
|
|
params[:cart_items].require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :nb_reserve_places,
|
2019-09-09 18:04:31 +02:00
|
|
|
tickets_attributes: %i[event_price_category_id booked],
|
|
|
|
slots_attributes: %i[id start_at end_at availability_id offered])
|
2019-09-09 17:37:54 +02:00
|
|
|
end
|
2019-09-09 18:04:31 +02:00
|
|
|
|
2019-09-10 17:57:46 +02:00
|
|
|
def subscription_params
|
|
|
|
params[:cart_items].require(:subscription).permit(:plan_id)
|
|
|
|
end
|
|
|
|
|
2019-09-09 17:37:54 +02:00
|
|
|
def cart_items_params
|
|
|
|
params[:cart_items].require(:reservation).permit(:reservable_id, :reservable_type, :plan_id, :user_id, :nb_reserve_places,
|
2019-09-09 18:04:31 +02:00
|
|
|
tickets_attributes: %i[event_price_category_id booked],
|
|
|
|
slots_attributes: %i[id start_at end_at availability_id offered])
|
2019-09-09 17:37:54 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def coupon_params
|
|
|
|
params.require(:cart_items).permit(:coupon_code)
|
|
|
|
end
|
2019-09-09 18:04:31 +02:00
|
|
|
end
|