2019-09-05 11:03:22 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for handling payments process in the front-end
|
|
|
|
class API::PaymentsController < API::ApiController
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
|
|
def confirm_payment
|
|
|
|
begin
|
2019-09-09 17:37:54 +02:00
|
|
|
if params[:payment_method_id].present?
|
2019-09-05 11:03:22 +02:00
|
|
|
# Create the PaymentIntent
|
2019-09-05 17:17:51 +02:00
|
|
|
# TODO the client has to provide the reservation details. Then, we use Price.compute - user.walletAmount to get the amount
|
|
|
|
# currency is set in Rails.secrets
|
2019-09-09 18:04:31 +02:00
|
|
|
|
2019-09-09 17:37:54 +02:00
|
|
|
reservable = cart_items_params[:reservable_type].constantize.find(cart_items_params[:reservable_id])
|
|
|
|
price_details = Price.compute(false,
|
2019-09-09 18:04:31 +02:00
|
|
|
current_user,
|
|
|
|
reservable,
|
|
|
|
cart_items_params[:slots_attributes] || [],
|
|
|
|
cart_items_params[:plan_id],
|
|
|
|
cart_items_params[:nb_reserve_places],
|
|
|
|
cart_items_params[:tickets_attributes],
|
|
|
|
coupon_params[:coupon_code])
|
2019-09-09 17:37:54 +02:00
|
|
|
|
2019-09-05 11:03:22 +02:00
|
|
|
intent = Stripe::PaymentIntent.create(
|
2019-09-09 17:37:54 +02:00
|
|
|
payment_method: params[:payment_method_id],
|
|
|
|
amount: price_details[:total],
|
2019-09-09 18:04:31 +02:00
|
|
|
currency: 'eur',
|
2019-09-05 11:03:22 +02:00
|
|
|
confirmation_method: 'manual',
|
|
|
|
confirm: true
|
|
|
|
)
|
2019-09-09 17:37:54 +02:00
|
|
|
elsif params[:payment_intent_id].present?
|
|
|
|
intent = Stripe::PaymentIntent.confirm(params[:payment_intent_id])
|
|
|
|
|
2019-09-09 18:04:31 +02:00
|
|
|
|
2019-09-05 11:03:22 +02:00
|
|
|
end
|
|
|
|
rescue Stripe::CardError => e
|
|
|
|
# Display error on client
|
2019-09-05 17:17:51 +02:00
|
|
|
render status: 200, json: { error: e.message }
|
2019-09-05 11:03:22 +02:00
|
|
|
end
|
|
|
|
|
2019-09-09 18:04:31 +02:00
|
|
|
# TODO extract in subroutine
|
|
|
|
if intent.status == 'succeeded'
|
2019-09-09 17:37:54 +02:00
|
|
|
begin
|
2019-09-09 18:04:31 +02:00
|
|
|
user_id = params[:cart_items][:reservation][:user_id]
|
2019-09-09 17:37:54 +02:00
|
|
|
|
|
|
|
@reservation = Reservation.new(reservation_params)
|
|
|
|
is_reserve = Reservations::Reserve.new(user_id, current_user.invoicing_profile.id)
|
|
|
|
.pay_and_save(@reservation, :stripe, coupon_params[:coupon_code])
|
2019-09-09 18:04:31 +02:00
|
|
|
|
2019-09-09 17:37:54 +02:00
|
|
|
if is_reserve
|
|
|
|
SubscriptionExtensionAfterReservation.new(@reservation).extend_subscription_if_eligible
|
2019-09-09 18:04:31 +02:00
|
|
|
|
2019-09-09 17:37:54 +02:00
|
|
|
render('api/reservations/show', status: :created, location: @reservation) and return
|
|
|
|
else
|
|
|
|
render(json: @reservation.errors, status: :unprocessable_entity) and return
|
|
|
|
end
|
|
|
|
rescue InvalidCouponError
|
|
|
|
render(json: { coupon_code: 'wrong coupon code or expired' }, status: :unprocessable_entity) and return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-05 16:17:02 +02:00
|
|
|
render generate_payment_response(intent)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_payment_response(intent)
|
|
|
|
if intent.status == 'requires_action' && intent.next_action.type == 'use_stripe_sdk'
|
|
|
|
# Tell the client to handle the action
|
|
|
|
{
|
|
|
|
status: 200,
|
|
|
|
json: {
|
|
|
|
requires_action: true,
|
|
|
|
payment_intent_client_secret: intent.client_secret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif intent.status == 'succeeded'
|
2019-09-09 18:04:31 +02:00
|
|
|
# The payment didn't need any additional actions and is completed!
|
2019-09-05 16:17:02 +02:00
|
|
|
# Handle post-payment fulfillment
|
|
|
|
{ status: 200, json: { success: true } }
|
|
|
|
else
|
|
|
|
# Invalid status
|
|
|
|
{ status: 500, json: { error: 'Invalid PaymentIntent status' } }
|
|
|
|
end
|
2019-09-05 11:03:22 +02:00
|
|
|
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-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
|