Não encontro na API do iugu onde colcoar os dados do cartão de crédito.
def pay
Iugu.api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
param = (params[:token].empty? ? "method" : "token").to_sym
@cupom = Coupon.find_by(name: params[:cupom])
if @cupom
if Time.zone.now > @cupom.end
redirect_to pagamento_index_path, notice: "O cupom <strong>#{@cupom.name}</strong> não é mais válido. Venceu em <strong>#{@cupom.start.strftime("%d/%m/%Y")}</strong>" and return
elsif Time.zone.now < @cupom.start
redirect_to pagamento_index_path, notice: "O cupom <strong>#{@cupom.name}</strong> só será válido a partir de <strong>#{@cupom.start.strftime("%d/%m/%Y")}</strong>" and return
elsif Time.zone.now > @cupom.start && Time.zone.now < @cupom.end
@desconto = @cupom
end
else
redirect_to pagamento_index_path, notice: "O cupom <strong>#{params[:cupom]}</strong> não existe" and return
end
if current_empresa.uso_sistema == 1
cpf_cnpj = current_empresa.cnpj
name = current_empresa.responsavel
elsif
cpf_cnpj = current_empresa.cpf
name = current_empresa.name
end
@nome = params[:full_name]
@ncartao = params[:number].delete(" ")
@cvv = params[:verification_value]
@validade = params[:expiration].delete("/")
Iugu::Charge.create({
"token" => params[:token],
"method" => params[:method],
"number" => @ncartao,
"email" => "con...@educadorpostural.com.br",
"months" => params[:meses],
"items" =>
[{
"description" => params[:plano],
"quantity" => "1",
"price_cents" => "1000"
}],
payer: {
cpf_cnpj: cpf_cnpj,
name: name,
phone: current_empresa.telefone,
email: current_empresa.email,
address: {
street: current_empresa.endereco,
city: current_empresa.cidade,
state: current_empresa.estado,
country: "Brasil",
zip_code: current_empresa.cep
}
}
}
)
customer = Iugu::Customer.create({
"email" => current_empresa.email,
"name" => current_empresa.name
})
sub_params = {
plan_identifier: 'empresas_05',
customer_id: customer.id
}
sub_params[:only_on_charge_success] = true if params[:payment_method] == "credit_card"
subscription = Iugu::Subscription.create(sub_params)
# subscription = Iugu::Subscription.create({
# "plan_identifier" => "individual", "customer_id" => current_empresa.id
# })
if subscription.errors
@plan = Iugu::Plan.fetch_by_identifier('plan_identifier')
@payment_method = params[:payment_method]
@error = "Erro na Cobrança!"
render :index, notice: 'Falha no pagamento.'
end
current_empresa.subscription_id = subscription.id
current_empresa.data_pagamento = Time.zone.now
current_empresa.save
if current_empresa.uso_sistema == 1
redirect_to users_path, notice: 'Pagamento realizado com sucesso!'
elsif current_empresa.uso_sistema == 2
redirect_to editar_empresa_path, notice: 'Pagamento realizado com sucesso!'
end
# redirect_to :index, notice: 'Falha no pagamento.'
# Exemplo de Downgrade/Upgrade de Conta (Com cálculo automático de diferença de valores entre planos, créditos, etc)
# subscription.change_plan( "novo_plano" );
# Histórico de Pagamentos do Cliente
# customer.invoices
end
end