RAILS - 条带收件人Id错误

时间:2014-07-05 01:06:00

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

我正在建立一个双面市场,用户在他们的商品销售时得到支付。

我想要在市场上列出商品时,用户填写的表单。它包含许多输入,但这个问题的重要内容是Stripe COUNTRY,ACCOUNT和ROUTING输入。这样他们就可以在商品销售后自动付款。

我将Stripe收件人ID存储在用户数据库中,这样他们就不必在每次发布新项目时都输入他们的银行帐户信息。 (可以在#create动作中看到)我将要发布的所有内容都能完美呈现,但仅限于。在第一次发布项目后,用户收件人ID成功保存到我的用户数据库。 ,如果该用户尝试发布其他商品,我会收到Stripe错误,并说:“您必须提供卡片,客户或银行帐户才能创建令牌。“,当试图提交表格时。此错误也会显示在我的Stripe仪表板中。

我认为这与Strong Params有关?起初,我没有:Country,:Routing_number,或者:listing_controller中的params中的Account_number,所以我已经添加了它们,但它没有修复错误。我接近了吗?谁能发现我做错了什么?

_FORM.HTML.ERB

<%= form_for @listing, :html => { :multipart => true } do |f| %>
<% if @listing.errors.any? %>
<div id="error_explanation" class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;    </button>
  <h2><%= pluralize(@listing.errors.count, "error") %> prohibited this listing from being saved:</h2>

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

<div id="stripe_error" class="alert alert-danger" style="display:none">
<noscript> Javascript is disabled. Order cannot be placed. First enable it in your  browser.</noscript>

<% if current_user.recipient.blank? %>
<br>
<h1>Bank Account Information</h1>
<p class="sm-message">(You'll only need to enter this once.)</p>

<div class="form-group">
  <%= label_tag :country %>
  <%= text_field_tag :country, nil, { :name => nil, :'data-stripe' => "country", class:  "form-control" } %>
</div>
<div class="form-group">
  <%= label_tag :routing_number %>
  <%= text_field_tag :routing_number, nil, { :name => nil, :'data-stripe' => "routingNumber", class: "form-control" } %>
 </div>
 <div class="form-group">
  <%= label_tag :account_number %>
  <%= text_field_tag :account_number, nil, { :name => nil, :'data-stripe' => "accountNumber", class: "form-control" } %>
</div>
<% end %>



<div class="form-group">
<%= f.submit class: "btn btn-primary btn-lg" %>
</div>
<% end %>
</div>

LISTING_CONTROLLER.RB

class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, only: [:seller, :new, :create, :edit, :update,  :destroy]
before_filter :check_user, only: [:edit, :update, :destroy]

def seller
@listings = Listing.where(user: current_user).order("created_at DESC")
end

# GET /listings
# GET /listings.json
def index
@listings = Listing.all.order("created_at DESC")
end

# GET /listings/1
# GET /listings/1.json
def show
end

# GET /listings/new
def new
@listing = Listing.new

end

# GET /listings/1/edit
def edit
end

# POST /listings
# POST /listings.json
def create
@listing = Listing.new(listing_params)
@listing.user_id = current_user.id
if current_user.recipient.blank?
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]

recipient = Stripe::Recipient.create(
  :name => current_user.name,
  :type => "individual",
  :bank_account => token
)

 current_user.recipient = recipient.id
 current_user.save
 end

 respond_to do |format|
  if @listing.save
    format.html { redirect_to @listing, notice: 'Listing was successfully created.' }
    format.json { render :show, status: :created, location: @listing }
  else
    format.html { render :new }
    format.json { render json: @listing.errors, status: :unprocessable_entity }
  end
  end
  end

  # PATCH/PUT /listings/1
  # PATCH/PUT /listings/1.json
  def update
  respond_to do |format|
  if @listing.update(listing_params)
    format.html { redirect_to @listing, notice: 'Listing was successfully updated.' }
    format.json { render :show, status: :ok, location: @listing }
  else
    format.html { render :edit }
    format.json { render json: @listing.errors, status: :unprocessable_entity }
  end
  end
  end

  # DELETE /listings/1
  # DELETE /listings/1.json
  def destroy
   @listing.destroy
  respond_to do |format|
  format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.'  }
  format.json { head :no_content }
  end
  end

  private
  # Use callbacks to share common setup or constraints between actions.
  def set_listing
  @listing = Listing.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def listing_params
  params.require(:listing).permit(:name, :description, :price, :duration, :street, :city, :state, :image, :duration, :category, :terms,
   :country, :account_number, :routing_number, :stripeToken)
  end

 def check_user
  if current_user != @listing.user
    redirect_to root_url, alert: "Sorry, that is not your listing."
 end
 end
 end

LISTING.JS.COFFEE

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

listing =
setupForm: ->
$('#new_listing').submit ->
  if $('input').length > 8
    $('input[type=submit]').attr('disabled', true)
    Stripe.bankAccount.createToken($('#new_listing'), listing.handleStripeResponse)
    false

 handleStripeResponse: (status, response) ->
  if status == 200
  $('#new_listing').append($('<input type="hidden" name="stripeToken"  />').val(response.id))
  $('#new_listing')[0].submit()
 else
  $('#stripe_error').text(response.error.message).show()
  $('input[type=submit]').attr('disabled', false)

STRIPE DASHBOARD信息被退回:

key: "pk_test_WgTil6dDhXN6JqzqZI4Gjw0M"
callback: "sjsonp1404518718631"
_method: "POST"

error:
type: "invalid_request_error"
message: "You must supply either a card, customer, or bank account to create a token."

1 个答案:

答案 0 :(得分:0)

问题可能在这里:

current_user.recipient = recipient.id
current_user.save

您正在保存期望对象的ID。尝试:

current_user.recipient = recipient
current_user.save