首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何连接rails中的Mercadopago

如何连接rails中的Mercadopago
EN

Stack Overflow用户
提问于 2018-04-19 04:38:48
回答 1查看 227关注 0票数 0

你好,谁能帮我连接红宝石铁路上的mercado pago。我知道信息流,但不知道如何在我的控制器中实现它。有一个例子的人,我希望用户按下一个按钮,然后保存令牌

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-19 04:47:47

我在观景中暗示了这一点。这是我使用MercadoPago订阅月度或年度计划的一个例子:

代码语言:javascript
复制
<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>

这是控制器,其中有subscribe_year和subscribe_month:

代码语言:javascript
复制
 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49912839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档