Stripe :: Customer实例具有无效ID:nil

时间:2015-11-16 21:59:10

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

我在我的rails应用程序上执行条带付款有些困难。我想将用户重定向到新的付款页面进行确认,然后在创建操作中完成付款。

我收到错误:

  

PaymentsController中的Stripe :: InvalidRequestError #create

     

无法确定要请求的URL:Stripe :: Customer实例   有无效的ID:nil

payment_controller.rb

class PaymentsController < ApplicationController

    def new
        @product = Product.find(params[:product_id])
        @user = current_user

        token = params[:stripeToken]
        @customer = Stripe::Customer.create(
            :source => token,
            :email => @user.email   
        )
    end

    def create
        @product = Product.find(params[:product_id])
        @customer = Stripe::Customer.retrieve(params[:customer_id])

        begin
            charge = Stripe::Charge.create(
                :amount => @product.amount, #amount in cents, again
                :currency => "eur",
                :description => params[:stripeEmail],
                :customer => @customer.id
            )
        rescue Stripe::CardError => e
            # The card has been declined
            body = e.json_body
            err = body[:error]
            flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]}"
        end
    end
end

付款/ new.html.erb

<h1>Thank you for using our services.</h1>

<p>
    You (<%= @user.email %>) have ordered the <%= @product.name %>.
</p>

<div class="payment_info">
  <p>
    <strong>Name:</strong>
    <%= @product.name %>
  </p>

  <p>
    <strong>Description:</strong>
    <%= @product.description %>
  </p>

  <p>
    <strong>Colour:</strong>
    <%= @product.colour %>
  </p>

  <p>
    <strong>Price:</strong>
    <%= '%.2f' % (@product.amount/100.00) %>
  </p>
 </div>

<%= form_for(@payment) do |f| %>
  <%= hidden_field_tag :product_id, @product.id %>
  <%= hidden_field_tag :customer_id, @customer.id %>
  <%= f.submit %>
<% end %>

<%= link_to 'Back', product_path(@payment.product) %>

2 个答案:

答案 0 :(得分:0)

这是因为您的params[:customer_id]为nil且条带无法确定/准备用于调用条带服务器的URL。

您需要确保params[:customer_id]不是nil

答案 1 :(得分:0)

也许你应该升级到最新的条带api版本。 Stripe在"api upgrades".

下的网站上有指南