ruby-on-rails - 如何设置 Stripe gem

标签 ruby-on-rails ruby ruby-on-rails-4 stripe-payments

作为引用,我一直在关注 this guide在 Ruby on Rails 上的 Stripe 上。完成后,我收到此错误:

Cannot charge a customer that has no active card

我现在已经无计可施了......

我的包裹里有 Stripe 。我运行了 rails g controller payments

这是我设置的支付 Controller :

class PaymentsController < ApplicationController
  before_action :session_check

  def index
    redirect_to new_payment_path
  end

  def new
  end

  def create
    @amount = 900

    customer = Stripe::Customer.create(
      :email => current_user.email,
      :card  => params[:stripetoken]
    )

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

    redirect_to account_path
  rescue Stripe::CardError => e
    render text: e.message
  end
end

:session_check 是应用程序 Controller 内部的一个方法:

def session_check
  redirect_to login_path unless current_user
end

在 route 我有resources :payments

这是我在 config/initializers/stripe.rb 中的内容:

# For Runnning on Development or Test Environments:
# sandbox number is 4242 4242 4242 4242
# any three digit CVC
# expiry date must be in future

Rails.configuration.stripe = {
  :publishable_key => Rails.env.production? ? ENV['PUBLISHABLE_KEY'] : "pk_test_long_hash",
  :secret_key      => Rails.env.production? ? ENV['SECRET_KEY'] : "sk_test_another_long_hash"
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

payments/new.html.erb 中,我们有:

<h1 class="text-center"> Upgrade Today </h1>

<div class="row">
  <div class="container">
    <div class="text-center">
      <%= form_tag payments_path do %>
        <article>
          <label class="amount">
            <span>Amount: $9.00</span>
          </label>
        </article>

        <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
                data-name="Site Using Stripe"
                data-description="One Month Subscription"
                data-amount="900"
                data-currency="cad"
                data-email="<%= current_user.email %>"
                data-allow-remember-me="true"></script>
      <% end %>
    </div>
  </div>
</div>

payments/create.html.erb 非常简单。 Stripe 默认值:

<h2> Thanks, you paid <strong>$5.00</strong>!</h2>

关键问题:为什么我在处理测试信用卡时收到Cannot charge a customer that has no active card

最佳答案

我不是这方面的专家,但我确实有一个使用 String 的应用程序,在我的 Create 方法中我们有这一行:

:card => 参数[:stripetoken]

在 Stripe::Charge 中,而不是在 Stripe::Customer 中 这对我来说很有意义,客户可能拥有不止一张卡,但每次收费都必须提供一张唯一的卡。

这是我的支付模型中的内容

  def save_with_payment(payment)
    if valid?
      Stripe::Charge.create(
        :amount => payment.amount*100, #amount in cents
        :currency => "usd",
        :card => stripe_card_token,
        :description => "description of payment");
      save
    end
    rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

关于ruby-on-rails - 如何设置 Stripe gem ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23333534/

相关文章:

没有 Rails 的 Ruby/Postgres ActiveRecord : Undefined Table ERROR: relation "..." does not exist

ruby-on-rails - 检查多插入事务是否成功

ruby-on-rails - rails 5 : How to pass collection_select values through strong_params in a fields_for?

ruby-on-rails - 进入那个数据库!使用 Ruby 解析 CSV

ruby-on-rails - 验证失败后保留表单字段, "render"不起作用

php - 在 php 和 Ruby On Rails 应用程序之间传递身份验证

ruby-on-rails - factory_girl (4.2.0) 多对多关系

ruby-on-rails - 测试 Sidekiq 的重试队列

ruby-on-rails - 如何针对组合字段的唯一性对这种复杂的验证进行建模

javascript - JQuery Mobile/Rails 4 - 仅对表单禁用 Ajax