Sha256: 7d6263dd5cf241d5e782e24ea4bc86f0efecd55a81249d4746dc44decd4a0678

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module SolidusStripe
  class IntentsController < Spree::BaseController
    include Spree::Core::ControllerHelpers::Order

    def create_intent
      @intent = create_payment_intent
      generate_payment_response
    end

    def create_payment
      create_payment_service = SolidusStripe::CreateIntentsPaymentService.new(
        params[:stripe_payment_intent_id],
        stripe,
        self
      )

      if create_payment_service.call
        render json: { success: true }
      else
        render json: { error: "Could not create payment" }, status: 500
      end
    end

    private

    def stripe
      @stripe ||= Spree::PaymentMethod::StripeCreditCard.find(params[:spree_payment_method_id])
    end

    def generate_payment_response
      response = @intent.params
      # Note that if your API version is before 2019-02-11, 'requires_action'
      # appears as 'requires_source_action'.
      if %w[requires_source_action requires_action].include?(response['status']) && response['next_action']['type'] == 'use_stripe_sdk'
        render json: {
          requires_action: true,
          stripe_payment_intent_client_secret: response['client_secret']
        }
      elsif response['status'] == 'requires_capture'
        render json: {
          success: true,
          requires_capture: true,
          stripe_payment_intent_id: response['id']
        }
      else
        render json: { error: response['error']['message'] }, status: 500
      end
    end

    def create_payment_intent
      stripe.create_intent(
        (current_order.total * 100).to_i,
        params[:stripe_payment_method_id],
        description: "Solidus Order ID: #{current_order.number} (pending)",
        currency: current_order.currency,
        confirmation_method: 'automatic',
        capture_method: 'manual',
        confirm: true,
        setup_future_usage: 'off_session',
        metadata: { order_id: current_order.id }
      )
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_stripe-4.3.0 app/controllers/solidus_stripe/intents_controller.rb
solidus_stripe-4.2.0 app/controllers/solidus_stripe/intents_controller.rb
solidus_stripe-4.1.0 app/controllers/solidus_stripe/intents_controller.rb