Class: Stay::PaymentsController

Inherits:
ApplicationController show all
Includes:
StripeConcern
Defined in:
app/controllers/stay/payments_controller.rb

Instance Method Summary collapse

Methods included from StripeConcern

#attach_payment_method_to_customer, #confirm_payment_intent, #create_customer, #create_or_retrieve_customer, #create_payment_intent, #create_payment_method, #create_payment_method_from_token, #destroy_payment_method, #get_payment_intent, #user

Methods inherited from ApplicationController

#after_sign_in_path_for, #current_store

Methods included from LocaleHelper

#all_locales_options, #available_locales, #available_locales_options, #config_locale?, #current_locale, #find_with_fallback_default_locale, #locale_full_name, #locale_param, #locale_presentation, #params_locale?, #should_render_locale_dropdown?, #supported_locale?, #supported_locales, #supported_locales_for_all_stores, #supported_locales_options, #user_locale?

Methods included from CurrencyHelper

#currency_options, #currency_presentation, #currency_symbol, #should_render_currency_dropdown?, #supported_currency_options

Instance Method Details

#confirmObject



33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/stay/payments_controller.rb', line 33

def confirm
  payment_intent = get_payment_intent
  if payment_intent.status == "succeeded"
    @booking.update(status: "completed")
    flash[:notice] = "Payment Completed Successfully"
    render json: { success: true, redirect_url: params[:redirect_url] }
  else
    flash[:alert] = payment_intent.last_payment_error.message
    render json: { error: payment_intent.last_payment_error.message }, status: 422
  end
end

#createObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/stay/payments_controller.rb', line 11

def create
  begin
      @customer = create_or_retrieve_customer
      payment_intent = @booking.payment_intent_id ? get_payment_intent : create_payment_intent.tap { |pi| @booking.update(payment_intent_id: pi.id) }
      if params[:confirm]
          if payment_intent.status == "succeeded"
              render json: { success: true, redirect_url: params[:redirect_url] }, status: :ok
          elsif payment_intent.status == "requires_action"
              render json: { message: "Payment requires further action", client_secret: payment_intent.client_secret }, status: :ok
          else
              render json: { message: "Payment failed", error: payment_intent.last_payment_error }, status: :unprocessable_entity
          end
      else
          render json: { payment_intent: payment_intent, client_secret: payment_intent.client_secret }, status: :ok
      end
  rescue Stripe::CardError => e
    render json: { error: e.message }, status: :unprocessable_entity
  rescue => e
    render json: { error: e.message }, status: :internal_server_error
  end
end

#newObject



7
8
9
# File 'app/controllers/stay/payments_controller.rb', line 7

def new
  render layout: "stay/application"
end