如何在创建新订单时获取stripe_card_token以进行保存

时间:2013-10-19 13:02:58

标签: ruby-on-rails stripe-payments

这是我正在产生的回应:

Processing by OrdersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"C/1Fmc4iEAiRGXwTcQNcDdX3j81KyhMHnfNcuYC/p1Y=", "order"=>{"video_id"=>"99", "member_id"=>"36", "price"=>"1000", "stripe_card_token"=>"tok_2mYQqbzBgt1EYy"}}
Completed 404 Not Found in 22ms

ActionController::RoutingError - No route matches {:action=>"show", :controller=>"orders", :id=>#<Order id: nil, video_id: 99, member_id: 36, stripe_card_token: nil, price: 1000, created_at: nil, updated_at: nil>}:

基本上,在创建新订单时会生成所有正确的属性,但由于某种原因,stripe_card_token没有传递给params。

这是我的订单型号:

  attr_accessible :price, :stripe_card_token, :member_id, :video_id

  belongs_to :video
  belongs_to :member

  accepts_nested_attributes_for :video
    accepts_nested_attributes_for :member

    validates :member_id, presence: true
    validates :video_id, presence: true
    validates :price, presence: true

  attr_accessor :stripe_card_token


   def save_with_stripe
    if valid?
      @charge = Stripe::Charge.create(
        amount: price,
        currency: "gbp",
        card: stripe_card_token,
        description: "something"
      )

      save!
    end
  rescue
    errors.add :base, "There was a problem with Stripe"
  end

这是我的订单管理员:

  def new
    @order = Order.new(:member_id => @member.id, :video_id => @video.id, :price => @video.price)    
  end

  def create
    @order = Order.new(params[:order])
    if @order.save_with_stripe
      flash[:success] = "Enjoy the video!"
      redirect_to order_path(@order)
    else
      render partial: 'orders/buynow'
    end
  end

  def show
    @order = Order.find(params[:id])
  end

我的表格:

<%= form_for @order do |f| %>
    <% if @order.errors.any? %>
      <div class="error_messages">
        <h2><%= pluralize(@order.errors.count, "error") %> stopped your card from being charged:</h2>
        <ul>
        <% @order.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
        </ul>
      </div>
    <% end %>

    <%= f.hidden_field :video_id %>
    <%= f.hidden_field :member_id %>
    <%= f.hidden_field :price %>
    <%= f.hidden_field :stripe_card_token %>

    <% if current_user.stripe_customer_token.present? %>
      Credit card has been provided.
    <% else %>
      <div class="field">
        <%= label_tag :name, "Name on card" %>
        <%= text_field_tag :name, nil, name: nil %>
      </div>    
      <div class="field">
        <%= label_tag :card_number, "Credit Card Number" %>
        <%= text_field_tag :card_number, nil, name: nil %>
      </div>
      <div class="field">
        <%= label_tag :card_code, "Security Code on Card (CVV)" %>
        <%= text_field_tag :card_code, nil, name: nil %>
      </div>
      <div class="field">
        <%= label_tag :card_month, "Card 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>
    <% end %>
    <div id="stripe_error">
      <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
    </div>
    <div class="actions">
      <%= f.submit "Buy Now" %>
    </div>
  <% end %>

我的咖啡:

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  order.setupForm()

order =
  setupForm: ->
    $('#new_order').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        order.processCard()
        false
      else
        true

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, order.handleStripeResponse)

  handleStripeResponse: (status, response) ->
    if status == 200
      $('#order_stripe_card_token').val(response.id)
      $('#new_order')[0].submit()
    else
      $('#stripe_error').text(response.error.message)
      $('input[type=submit]').attr('disabled', false)

你能帮我理解我的错吗?

编辑1:

这是我的开发日志中生成的新响应现在我修复了尝试将'email'的未定义局部变量传递到'description'键的问题。

Started POST "/orders" for 127.0.0.1 at 2013-10-19 14:45:22 +0100
Processing by OrdersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"C/1Fmc4iEAiRGXwTcQNcDdX3j81KyhMHnfNcuYC/p1Y=", "order"=>{"video_id"=>"99", "member_id"=>"36", "price"=>"1000", "stripe_card_token"=>"tok_2mZBErSMVjAa3F"}}
   (1.9ms)  BEGIN
  SQL (2.4ms)  INSERT INTO "orders" ("created_at", "member_id", "price", "stripe_card_token", "updated_at", "video_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["created_at", Sat, 19 Oct 2013 13:45:24 UTC +00:00], ["member_id", 36], ["price", 1000], ["stripe_card_token", nil], ["updated_at", Sat, 19 Oct 2013 13:45:24 UTC +00:00], ["video_id", 99]]
   (2.3ms)  COMMIT
Redirected to http://eqrfqer.xavy.dev/orders/13

由于某种原因,params中生成的stripe_card_token未插入到我的Order模型中。这是问题,但我不明白为什么?

编辑2:

我想也许问题实际上是我试图将未定义的电子邮件传递给'description'。由于我改变了这一点,我可以看到收费已经传递到我的Stripe帐户。我不明白为什么它在日志中看起来好像stripe_card_token是'nil'但我认为它必须是一个安全功能?

无论如何,它现在似乎正在发挥作用。

1 个答案:

答案 0 :(得分:0)

写下这一行:

attr_accessor :stripe_card_token

您正在终止用于设置stripe_card_token

的rails功能

如果它是数据库中的列,请将其删除!