2022-08-21 19:08:10 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-09-28 12:54:29 +02:00
|
|
|
require 'stripe/helper'
|
|
|
|
require 'pay_zen/helper'
|
|
|
|
|
2022-08-21 19:08:10 +02:00
|
|
|
# API Controller for cart checkout
|
|
|
|
class API::CheckoutController < API::ApiController
|
|
|
|
include ::API::OrderConcern
|
2022-08-25 08:52:17 +02:00
|
|
|
before_action :authenticate_user!
|
|
|
|
before_action :current_order
|
|
|
|
before_action :ensure_order
|
|
|
|
|
|
|
|
def payment
|
2022-08-27 18:59:59 +02:00
|
|
|
authorize @current_order, policy_class: CheckoutPolicy
|
|
|
|
if @current_order.statistic_profile_id.nil? && current_user.privileged?
|
2022-08-27 18:00:43 +02:00
|
|
|
user = User.find(params[:customer_id])
|
|
|
|
@current_order.statistic_profile = user.statistic_profile
|
|
|
|
end
|
|
|
|
res = Checkout::PaymentService.new.payment(@current_order, current_user, params[:coupon_code],
|
|
|
|
params[:payment_id])
|
2022-08-25 08:52:17 +02:00
|
|
|
render json: res
|
2022-09-28 12:54:29 +02:00
|
|
|
rescue Stripe::StripeError => e
|
|
|
|
render json: Stripe::Helper.human_error(e), status: :unprocessable_entity
|
|
|
|
rescue PayzenError => e
|
|
|
|
render json: PayZen::Helper.human_error(e), status: :unprocessable_entity
|
2022-08-25 08:52:17 +02:00
|
|
|
rescue StandardError => e
|
2022-12-30 17:52:28 +01:00
|
|
|
Rails.logger.error e
|
|
|
|
Rails.logger.debug e.backtrace
|
2022-08-25 08:52:17 +02:00
|
|
|
render json: e, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm_payment
|
2022-08-27 18:59:59 +02:00
|
|
|
authorize @current_order, policy_class: CheckoutPolicy
|
2022-08-26 20:10:21 +02:00
|
|
|
res = Checkout::PaymentService.new.confirm_payment(@current_order, current_user, params[:coupon_code], params[:payment_id])
|
2022-08-25 08:52:17 +02:00
|
|
|
render json: res
|
|
|
|
rescue StandardError => e
|
|
|
|
render json: e, status: :unprocessable_entity
|
|
|
|
end
|
2022-08-21 19:08:10 +02:00
|
|
|
end
|