Sha256: 92c899269094a6abd9f3c1cca56eb6a70b58503b3257421a2785b7b82bd40304

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

module SolidusBraintree
  class ClientTokensController < ::Spree::Api::BaseController
    skip_before_action :authenticate_user

    before_action :load_gateway

    def create
      token = @gateway.generate_token
      if token
        render json: { client_token: token, payment_method_id: @gateway.id }
      else
        render json: { error: Gateway::TOKEN_GENERATION_DISABLED_MESSAGE }, status: :unprocessable_entity
      end
    end

    private

    def load_gateway
      if params[:payment_method_id]
        @gateway = ::SolidusBraintree::Gateway.find(params[:payment_method_id])
      else
        store_payment_methods_scope =
          if current_store.payment_methods.empty?
            ::SolidusBraintree::Gateway.all
          else
            ::SolidusBraintree::Gateway.where(id: current_store.payment_method_ids)
          end
        @gateway = ::SolidusBraintree::Gateway.where(active: true).merge(store_payment_methods_scope).first!
      end
    end

    def generate_token
      @gateway.generate_token
    rescue ::SolidusBraintree::Gateway::TokenGenerationDisabledError => e
      Rails.logger.error e
      nil
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
solidus_braintree-3.2.0 app/decorators/controllers/solidus_braintree/client_tokens_controller.rb
solidus_braintree-3.1.0 app/decorators/controllers/solidus_braintree/client_tokens_controller.rb
solidus_braintree-3.0.1 app/decorators/controllers/solidus_braintree/client_tokens_controller.rb
solidus_braintree-3.0.0 app/decorators/controllers/solidus_braintree/client_tokens_controller.rb
solidus_braintree-2.0.0 app/decorators/controllers/solidus_braintree/client_tokens_controller.rb