<commit_before>module Spree::Conekta
  class Provider
    include Spree::Conekta::Client

    attr_accessor :auth_token, :source_method

    attr_reader :options

    PAYMENT_SOURCES = {
        'card' => Spree::Conekta::PaymentSource::Card,
        'bank' => Spree::Conekta::PaymentSource::Bank,
        'cash' => Spree::Conekta::PaymentSource::Cash
    }

    def initialize(options = {})
      @options       = options
      @auth_token    = options[:auth_token]
      @source_method = payment_processor(options[:source_method])
    end

    def authorize(amount, method_params, gateway_options = {})
      common = build_common(amount, gateway_options)
      commit common, method_params, gateway_options
    end

    alias_method :purchase, :authorize

    def capture(amount, method_params, gateway_options = {})
      Response.new({}, gateway_options)
    end

    def endpoint
      'charges'
    end

    def payment_processor(source_name)
      PAYMENT_SOURCES[source_name]
    end

    private

    def commit(common, method_params, gateway_options)
      source_method.request(common, method_params, gateway_options)
      Spree::Conekta::Response.new post(common), source_method
    end

    def build_common(amount, gateway_params)
      if source_method == Spree::Conekta::PaymentSource::Cash && gateway_params[:currency] != 'MXN'
        return build_common_to_cash(amount, gateway_params) 
      else
        {
          'amount' => amount,
          'reference_id' => gateway_params[:order_id],
          'currency' => gateway_params[:currency],
          'description' => gateway_params[:order_id],
          'monthly_installments' => installments_number
        }
      end
    end

    def installments_number
      [1,6,12].include?(options[:installments]) ? options[:installments] : 1
    end
    
    def build_common_to_cash(amount, gateway_params)
      amount_exchanged = Spree::Conekta::Exchange.new(amount, gateway_params[:currency]).amount_exchanged
      {
        'amount' => amount_exchanged,
        'reference_id' => gateway_params[:order_id],
        'currency' => "MXN",
        'description' => gateway_params[:order_id]
      }
    end
  end
end<commit_msg>Fix installments parameter to avoid cards being rejected<commit_after>module Spree::Conekta
  class Provider
    include Spree::Conekta::Client

    attr_accessor :auth_token, :source_method

    attr_reader :options

    PAYMENT_SOURCES = {
        'card' => Spree::Conekta::PaymentSource::Card,
        'bank' => Spree::Conekta::PaymentSource::Bank,
        'cash' => Spree::Conekta::PaymentSource::Cash
    }

    def initialize(options = {})
      @options       = options
      @auth_token    = options[:auth_token]
      @source_method = payment_processor(options[:source_method])
    end

    def authorize(amount, method_params, gateway_options = {})
      common = build_common(amount, gateway_options)
      commit common, method_params, gateway_options
    end

    alias_method :purchase, :authorize

    def capture(amount, method_params, gateway_options = {})
      Response.new({}, gateway_options)
    end

    def endpoint
      'charges'
    end

    def payment_processor(source_name)
      PAYMENT_SOURCES[source_name]
    end

    private

    def commit(common, method_params, gateway_options)
      source_method.request(common, method_params, gateway_options)
      Spree::Conekta::Response.new post(common), source_method
    end

    def build_common(amount, gateway_params)
      if source_method == Spree::Conekta::PaymentSource::Cash && gateway_params[:currency] != 'MXN'
        return build_common_to_cash(amount, gateway_params) 
      else
        {
          'amount' => amount,
          'reference_id' => gateway_params[:order_id],
          'currency' => gateway_params[:currency],
          'description' => gateway_params[:order_id],
          'monthly_installments' => installments_number
        }
      end
    end

    def installments_number
      [6,12].include?(options[:installments]) ? options[:installments] : nil
    end
    
    def build_common_to_cash(amount, gateway_params)
      amount_exchanged = Spree::Conekta::Exchange.new(amount, gateway_params[:currency]).amount_exchanged
      {
        'amount' => amount_exchanged,
        'reference_id' => gateway_params[:order_id],
        'currency' => "MXN",
        'description' => gateway_params[:order_id]
      }
    end
  end
end
{'fraction_non_alphanumeric': 0.10710259301014656, 'fraction_numerical': 0.0018038331454340473, 'mean_word_length': 3.232824427480916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1182917', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1318, 'n_words': 238}