Reputation: 118
hello how could someone help me connect with the mercado pago in Ruby on rail. I know the flow of information but I do not know how to implement it in my controller. someone who has an example I want the user to press a button and I save the Tokens
Upvotes: 0
Views: 275
Reputation: 1089
I implemment this in the view. This is an example where I use MercadoPago to subscribe to monthly or yearly plan:
<input type="hidden" id="subscribe_year" name="subscribe_year" ajax_path=<%= subscribe_year_path() %> >
<input type="hidden" id="subscribe_month" name="subscribe_month" ajax_path=<%= subscribe_month_path() %> >
<script type="text/javascript">
function execute_month_onreturn (json) {
$.ajax({
url: $('#subscribe_month').attr('ajax_path'),
data: { state: json.collection_status, payment_id: json.collection_id },
async: true,
dataType: 'script',
success: function(){
}
});
refresh();
}
function execute_year_onreturn (json) {
$.ajax({
url: $('#subscribe_year').attr('ajax_path'),
data: { state: json.collection_status, payment_id: json.collection_id },
async: true,
dataType: 'script',
success: function(){
}
});
refresh();
}
</script>
<%-
require 'mercadopago.rb'
mp = MercadoPago.new(ENV['MERCADO_PAGO_CLIENT_ID'] , ENV['MERCADO_PAGO_CLIENT_SECRET'])
-%>
<%= render 'shared/error_messages', :object => @company %>
<dl class="dl-horizontal">
<dt><strong><%= @company.counter %> de <%= @company.limit %></strong></dt>
</dl>
<div class="progress progress-warning">
<div class="bar" style="width: <%= 100*@company.counter/@company.limit %>%"></div>
</div>
<dl class="dl-horizontal">
<dt><strong><%=t '.initial_cycle', :default => t("helpers.labels.initial_cycle") %></strong></dt>
<dd><%= @company.initial_cycle.strftime(@company.date_format) %></dd>
</dl>
<dl class="dl-horizontal">
<dt><strong><%=t '.final_cycle', :default => t("helpers.labels.final_cycle") %></strong></dt>
<dd><%= @company.final_cycle.strftime(@company.date_format) %></dd>
</dl>
<dl class="dl-horizontal">
<dt><strong><%=t '.plan', :default => t("helpers.labels.plan") %></strong></dt>
<% if @company.plan == nil || @company.plan == "GRATIS" %>
<dd><td><span class="label label-important">GRATIS</span> </td></dd>
</dl>
<% if mp != nil %>
<dl class="dl-horizontal">
<dt><a href="<%= mp.create_preference({"items" => ["title"=>t("helpers.links.subscribe_month"), "quantity"=>1, "unit_price"=>ClienteEspecial::MONTHLY_PRICE, "currency_id"=>"VEN"]})['response']['init_point'].to_s %>" name="MP-Checkout" class="blue-rn-m" onreturn="execute_month_onreturn"><%= t '.subscribe_month', :default => t("helpers.links.subscribe_month") %></a></dt>
<dd><%= number_to_currency(ClienteEspecial::MONTHLY_PRICE, unit: @company.unit, separator: @company.separator , delimiter: @company.delimiter, format: "%u %n") %></dd>
</dl>
<dl class="dl-horizontal">
<dt><a href="<%= mp.create_preference({"items" => ["title"=>t("helpers.links.subscribe_year"), "quantity"=>1, "unit_price"=>ClienteEspecial::YEARLY_PRICE, "currency_id"=>"VEN"]})['response']['init_point'].to_s %>" name="MP-Checkout" class="red-rn-m" onreturn="execute_year_onreturn"><%= t '.subscribe_year', :default => t("helpers.links.subscribe_year") %></a></dt>
<dd><%= number_to_currency(ClienteEspecial::YEARLY_PRICE, unit: @company.unit, separator: @company.separator , delimiter: @company.delimiter, format: "%u %n") %></dd>
</dl>
<% end %>
<% elsif @company.plan != nil && @company.plan == "PAGO" %>
<dd><td><span class="label label-success"> <%= @company.plan %> </span></td></dd>
</dl>
<% end %>
<dl class="dl-horizontal">
<%= link_to t('.edit_formats', :default => t("helpers.links.edit_formats")),
edit_formats_path(:id => @company.id), :class => 'btn',:style => "width:90%;", remote: true %>
</dl>
And this is the controller where is subscribe_year and subscribe_month:
class ServicePaymentsController < ApplicationController
before_action :authenticate_user!
def index
@service_payments = current_user.company.service_payments.all( :limit => 10, :order => "id DESC" )
end
def subscribe_month
@service_payment = ServicePayment.new
@service_payment.amount = ClienteEspecial::MONTHLY_PRICE
@service_payment.description = t("helpers.links.subscribe_month")
@service_payment.period = 'month'
@service_payment.method = 'Mercado Pago'
@service_payment.state = params[:state]
@service_payment.payment_id = params[:payment_id]
subscribe(@service_payment)
end
def subscribe_year
@service_payment = ServicePayment.new
@service_payment.amount = ClienteEspecial::YEARLY_PRICE
@service_payment.description = t("helpers.links.subscribe_year")
@service_payment.period = 'year'
@service_payment.method = 'Mercado Pago'
@service_payment.state = params[:state]
@service_payment.payment_id = params[:payment_id]
subscribe(@service_payment)
end
def subscribe(service_payment)
service_payment.domain = current_user.domain
if service_payment.save
if service_payment.state =='approved'
execute(service_payment)
redirect_to company_path(:id => current_user.company.id), :notice => t('helpers.labels.service_payments')+" "+t('helpers.labels.approved')
else
redirect_to company_path(:id => current_user.company.id), :notice => t('helpers.labels.service_payments')+" "+t('helpers.labels.cancelled')+" ("+service_payment.state.to_s+")"
end
else
render company_path(:id => current_user.company.id), :alert => service_payment.errors.to_a.join(", ")
end
end
def execute(service_payment)
if service_payment.period == "month"
@company = current_user.company
@company.plan = "PAGO"
@company.initial_cycle = Time.new
@company.final_cycle = Time.now.months_since(1)
@company.counter = 0
@company.limit = 1000000
@company.save
elsif service_payment.period == "year"
@company = current_user.company
@company.plan = "PAGO"
@company.initial_cycle = Time.new
@company.final_cycle = Time.now.years_since(1)
@company.counter = 0
@company.limit = 1000000
@company.save
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_service_payment
@service_payment = ServicePayments.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def service_payment_params
params.require(:service_payment).permit(:amount, :description, :payment_id, :state, :period, :method, :domain)
end
end
Upvotes: 1