条带/ Rails保持返回错误

时间:2014-01-08 06:23:26

标签: ruby-on-rails coffeescript stripe-payments

我正在尝试允许用户在Rails应用中将钱存入他们的帐户,但我不断收到错误消息:“您的信用卡出现问题。”

我正在跟随这个RailsCast(http://railscasts.com/episodes/288-billing-with-stripe),但是试图允许用户设置数量,并且还在相应的模型中保存它的记录。

这就是我所拥有的:

型号:

class Deposit < Transaction

    attr_accessor :stripe_card_token

    def save_with_payment
    if valid?
        customer = Stripe::Charge.create(amount: 10, currency: "usd", card: stripe_card_token, description: "Deposit for test@example.com")
        save!
    end
    rescue Stripe::InvalidRequestError => e
        logger.error "Stripe error while creating deposit: #{e.message}"
        errors.add :base, "There was a problem with your credit card."
        false
    end
end

控制器:

class DepositsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @deposits = Deposit.where(user_id: current_user.id).order(created_at: :desc).all
  end

  def new
    @deposit = Deposit.new
  end

  def create
    @deposit = Deposit.new(deposit_params)
    @deposit.user_id = current_user.id
    if @deposit.save_with_payment
        redirect_to @deposit, :notice => "Thank you"
    else
        render :new
    end
  end

  private
    def deposit_params
        params.require(:deposit).permit(:amount, :stripe_card_token) #add attributes in permit
    end
end

新动作:

<h1>Make a Deposit</h1>

<%= form_for(@deposit) do |f| %>
  <% if @deposit.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@deposit.errors.count, "error") %> prohibited this deposit from being saved:</h2>

      <ul>
      <% @deposit.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.hidden_field :stripe_card_token %>

  <div class="field">
    <%= f.label :amount %><br>
    <%= f.text_field :amount %>
  </div>

  <% if @deposit.stripe_card_token.present? %>
    Credit card has been provided.
  <% else %>
      <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 'Submit', :class => 'btn btn-primary' %>
  </div>

<% end %>

CoffeeScript的:

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

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

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

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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为可能的问题是您的Coffescript文件中的handleStripeResponse回调未正确设置条带令牌。您关注的railscast的格式在<%= form_for @subscription do |f| %>内,而您的格式为<%= form_for(@deposit) do |f| %>,所以我认为您的条带标记字段应该能够像这样访问:

$('#deposit_stripe_card_token').val(response.id)

您应该能够在控制台中验证这一点,或者直接检查DOM以查看隐藏字段上的ID。