【问题标题】:Integrating Stripe in Rails app在 Rails 应用程序中集成 Stripe
【发布时间】:2015-09-01 22:04:29
【问题描述】:

我正在尝试创建一个带有条纹的自定义表单,虽然它似乎都在提交,但当我在 Stripe 中检查我的仪表板时,虽然我确实看到了交易记录 - 我没有看到金额或任何参考到通过的cc。话虽如此,我不太确定我应该在仪表板中看到什么。但我很确定我做错了什么。这是我的表单的代码:

<div class="container">
<div class="row Row one">
    <div class="col-sm-12 col-md-10">
       <%= form_for @project, url: project_charges_path, :html => {:id => "payment-form"}, method: 'post' do |f| %>

            <%= f.hidden_field :user_id, :value => current_user.id %>
            <%= f.hidden_field :@project_id, :value => @project.id %>

            <div class= "field">
              <%= label_tag :card_number, "Credit Card Number" %><br>
              <%= text_field_tag :card_number, nil, name: nil %><br>
            </div>

            <div class= "field">
              <%= label_tag :card_code, "Security Code (cvc)" %><br>
              <%= text_field_tag :card_code, nil, name: nil %><br>
            </div>

            <div class= "field">
              <%= label_tag :card_month, "Expiration" %>
              <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
              <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}%>
            </div>

            <div class= "actions">
              <%= f.submit("Submit") %>
            </div>

            <div id="stipe-error">
              <%= flash[:error] %> 
            </div>

      <% end %> 
    </div>
</div>
<!-- <div class="row"></div> -->

这是我的收费控制器:

class ChargesController < ApplicationController

    def new
        @project = Project.find(params[:project_id])
    end

    def create

         @project = Project.find(params[:project_id])
         binding.pry
          # Amount in cents, this is being read and recorded in stripe dashboard
          @amount = 500
          customer = Stripe::Customer.create(
            :email => 'helloWorld@stripe.com',
            :card  => params[:stripeToken]
          )

          charge = Stripe::Charge.create(
            :customer    => customer.id,
            :amount      => @amount,
            :description => 'Rails Stripe customer',
            :currency    => 'usd'
          )

              @payment = Payment.create({
                user_id: current_user.id,
                project_id: @project,
                amount: @amount
                })
              @payment.save

    rescue Stripe::CardError => e
      flash[:error] = e.message
    end

    # private
    # def charges_params
    #   params.require(:payment).permit(:comments, :user_id, :project_id)
    # end

结束

根据教程,我还在 application.js 中包含了一些 javascript:

$('#payment-form').submit(function(event) {
    var $form = $(this);
    alert('you clicked submit');
    // Disable the submit button to prevent repeated clicks
    $form.find('button').prop('disabled', true);

    Stripe.card.createToken($form, stripeResponseHandler);

    // Prevent the form from submitting with the default action
    return false;
  });

function stripeResponseHandler(status, response) {
  var $form = $('#payment-form');

  if (response.error) {
    // Show the errors on the form
    $form.find('.payment-errors').text(response.error.message);
    $form.find('button').prop('disabled', false);
  } else {
    // response contains id and card, which contains additional card details
    var token = response.id;
    // Insert the token into the form so it gets submitted to the server
    $form.append($('<input type="hidden" name="stripeToken" />').val(token));
    // and submit
    $form.get(0).submit();
  }
}

在我看到的条纹仪表板内部:

邮件通过了,但没有涉及金额或卡。我不希望看到卡号写成,但有一些参考,可能只是类型,或者最后四位数字?同样在仪表板的首页(给出图表的区域,我想我应该看到付款的总和,甚至是测试付款,尽管已经支付了十多次每次 5 美元的测试付款,但总和仍然是 0 美元。

我在这里错过了什么?

此外,我遇到的大多数教程要么很老,要么是我不熟悉的 PHP。如果有人可以推荐一个很棒的资源,那也真的很有帮助。我计划将条带用于多个项目,并且真的很想了解它...

【问题讨论】:

    标签: ruby-on-rails stripe-payments


    【解决方案1】:

    我可能回复得太晚了,你一定已经这样做了,但以防万一这可能有助于其他人。我刚刚在我的应用程序中集成了条带。我不确定你在问什么,但我认为一个可行的例子可能会有所帮助。这与您所做的非常相似,我找不到问题所在。

    我正在做的是将条带返回的 customer_id 保存在我的用户表中。当用户保存信用卡时,会根据订阅计划扣除一定的金额。您将在仪表板的订阅者下的计划详细信息中看到 customer_id。此外,在客户中,当您引用该 customer_id 时,您将看到他订阅的计划。

    查看:(creditcard.html.erb)

    <div class="row">
      <% if flash[:error].present? %>
      <div class="col-lg-12 alert alert-danger">
        <%= flash[:error] %>
      </div>
      <% else %>
      <div class="col-lg-12" id = "payment-errors">
        <span class="payment-errors"></span>
      </div>
      <% end %>
    </div>
    <div>
    
      <%= form_tag plans_billings_chargecreditcard_path, id: "payment-form" do%>
      <div class="row">
        <div class="col-lg-3">
          <div class="form-group">
            <label>Card Number</label>
            <%= text_field_tag nil, nil, size: 20, "data-stripe": "number", class: "form-control" %>
          </div>
        </div>
        <div class="col-lg-2">
          <div class="form-group">
            <label>CVC</label>
            <%= text_field_tag nil, nil, size: 4, "data-stripe": "cvc", class: "form-control" %>
    
          </div>
        </div>
      </div>
      <div class="row">
        <div class="col-lg-12">
          <div class="form-group">
            <label>Expiration Date(MM/YY)</label>
            <select class="selectpicker set-width" data-live-search="true" data-stripe = "exp_month">
              <option>- Month -</option>
              <option>1</option>
              <option>2</option>
              <option>3</option>
              <option>4</option>
              <option>5</option>
              <option>6</option>
              <option>7</option>
              <option>8</option>
              <option>9</option>
              <option>10</option>
              <option>11</option>
              <option>12</option>
            </select>
            <select class="selectpicker set-width" data-live-search="true" data-stripe = "exp_year"> <!-- form-control input-lg -->
              <option>- Year -</option>
              <option>16</option>
              <option>17</option>
              <option>18</option>
              <option>19</option>
              <option>20</option>
              <option>21</option>
              <option>22</option>
              <option>23</option>
              <option>24</option>
              <option>25</option>
              <option>26</option>
            </select>
          </div>
        </div>
      </div>
    
      <div class="row">
        <div class="col-lg-12">
          <div class="form-group">
            <%= submit_tag "Save Card", class: "btn btn-primary" %>
          </div>
        </div>
      </div>
      <% end %>
    </div>
    

    CofeeScript:(plan_billings.coffee)

    stripeResponseHandler = (status, response) ->
      # Grab the form:
      $form = $('#payment-form')
      if response.error
        # Problem!
        # Show the errors on the form:
        $('#payment-errors').addClass 'alert'
        $('#payment-errors').addClass 'alert-danger'
        $('.payment-errors').text response.error.message    
        $('.submit').prop 'disabled', false
        # Re-enable submission
      else
        # Token was created!
        # Get the token ID:
        token = response.id
        # Insert the token ID into the form so it gets submitted to the server:
        $form.append $('<input type="hidden" name="stripeToken">').val(token)
        # Submit the form:
        $form.get(0).submit()
      return
    
    $ ->
      $form = $('#payment-form')
      $form.submit (event) ->
        # Disable the submit button to prevent repeated clicks:
        $form.find('.submit').prop 'disabled', true
        # Request a token from Stripe:
        Stripe.card.createToken $form, stripeResponseHandler
        # Prevent the form from being submitted:
        false
      return
    

    控制器:(在 PlanBilling 控制器中,chargecreditcard 操作)

    @plan_and_billing = current_user.plan_billing
    @current_plan = DataPlan.find_by(id: @plan_and_billing.data_plan_id)
    token = params[:stripeToken]
    if current_user.customer_id.present?
      customer = Stripe::Customer.retrieve(current_user.customer_id)
      customer.sources.create(source: token)
      redirect_to plans_billings_planbilling_path
    else
      customer = Stripe::Customer.create( :source => token, plan: YOUR_PLAN_ID_YOU_HAVE_INYOUR__DASHBOARD  )
      @credit_card = current_user.update(customer_id: customer.id)
      redirect_to plans_billings_planbilling_path
    end
    rescue Stripe::CardError => e
      flash[:error] = e.message
      redirect_to plans_billings_creditcard_path
    

    控制器中发生的情况是,当用户没有卡并保存卡详细信息时,卡被保存并扣除您提到的计划的价格。如果他已经保存了一张信用卡,并且正在保存另一张信用卡,则该卡只会被保存,因为您将在仪表板中看到他的详细信息。不会向新卡收费。它只是为该客户保存新信用卡。

    在这方面我还有很长的路要走,当然这可能不是一个很好的代码,但这只是非常基本的东西,你可能会觉得有帮助。如果有人尝试过并面临一些问题,我很乐意提供帮助。另外,如果有人能指导我改进这段代码,我将不胜感激。干杯:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-21
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      相关资源
      最近更新 更多