Sha256: ddbc4d7491fc2ffbd43e8fb0eb5034e100fde1b792fc9ca30d07f50f3c92fd1a

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module CatarsePagarme
  class CreditCardsController < CatarsePagarme::ApplicationController

    def create
      transaction = CreditCardTransaction.new(credit_card_attributes, contribution).charge!

      render json: { payment_status: transaction.status }
    rescue Exception => e
      render json: { payment_status: 'failed', message: e.message }
    end

    protected

    def credit_card_attributes
      hash = {
        payment_method: 'credit_card',
        amount: delegator.value_with_installment_tax(get_installment),
        postback_url: ipn_pagarme_index_url(host: CatarsePagarme.configuration.host,
                                            subdomain: CatarsePagarme.configuration.subdomain,
                                            protocol: CatarsePagarme.configuration.protocol),
        installments: get_installment,
        customer: {
          email: contribution.user.email,
          name: contribution.user.name
        },
        metadata: {
          key: contribution.key
        }
      }

      if params[:card_hash].present?
        hash[:card_hash] = params[:card_hash]
      else
        hash[:card_id] = params[:card_id]
      end

      if params[:save_card] === "true"
        hash[:save_card] = true
      end

      hash
    end

    def get_installment
      if contribution.value.to_f < CatarsePagarme.configuration.minimum_value_for_installment.to_f
        1
      elsif params[:payment_card_installments].to_i > 0
        params[:payment_card_installments].to_i
      else
        1
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
catarse_pagarme-2.5.1 app/controllers/catarse_pagarme/credit_cards_controller.rb
catarse_pagarme-2.5.0 app/controllers/catarse_pagarme/credit_cards_controller.rb