对rails很新,并尝试使用条带创建简单的订单表单并收到此错误:
(index):3 POST https://api.stripe.com/v1/tokens 400 (Bad Request)Stripe.isDoubleLoaded.c @ (index):3Stripe.isDoubleLoaded.e @ (index):3Stripe.isDoubleLoaded.a @ (index):3Stripe.isDoubleLoaded.Stripe.xhr @ (index):3Stripe.a._rawRequest @ (index):2Stripe.a.request @ (index):2Stripe.token.a.create @ (index):2Stripe.card.b.createToken @ (index):2Stripe.a._channelListener @ (index):2t.concat.incoming @ (index):2f @ (index):2
我看到this帖子,尝试过但没有成功。
这是我的orders.js.coffee:
jQuery ->
Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
payment.setupForm()
payment =
setupForm: ->
$('#new_order').submit ->
$('input[type=submit]').attr('disabled', true)
Stripe.card.createToken($('#new_order'), payment.handleStripeResponse)
false
handleStripeResponse: (status, response) ->
if status == 200
alert(response.id)
else
alert(response.error.message)
以下是我的订单:
<div class="row">
<div class="col-md-4">
<div class="thumbnail">
<%= image_tag @listing.image.url %>
</div>
<h3><%= @listing.name %></h3>
<h4><%= number_to_currency(@listing.price) %></h4>
</div>
<div class="col-md-8">
<%= form_for([@listing, @order]) do |f| %>
<% if @order.errors.any? %>
<div id="error_explanation" class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<h3><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h3>
<ul>
<% @order.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 not enabled and is reqiuered for this form. First enable it in your web browser settings.
</noscript>
</div>
<div class="form-group">
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :city %>
<%= f.text_field :city, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :state %>
<%= f.text_field :state, class: 'form-control' %>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-8">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, { :name => nil, :'data-stripe' => "number", class: "form-control card-number" } %>
</div>
<div class="col-md-4">
<%= label_tag :card_code, "CVC" %>
<%= text_field_tag :card_code, nil, { :name => nil, :'data-stripe' => "cvc", class: "form-control card-code" } %>
</div>
</div>
</div>
<div class="form-group">
<%= label_tag nil, 'Expiration Date' %>
<div class="row">
<div class="col-md-3">
<%= select_month nil, { use_two_digit_numbers: true }, { :name => nil, :'data-stripe' => "exp-month", class: "form-control exp-month" } %>
</div>
<div class="col-md-3">
<%= select_year nil, { start_year: Date.today.year, end_year: Date.today.year+10 }, { :name => nil, :'data-stripe' => "exp-year", class: "form-control exp-year" } %>
</div>
</div>
</div>
<div class="form-group">
<%= f.submit "Complete Order", class: 'btn btn-success' %>
</div>
<% end %>
</div>
</div>
这是我的订单控制器创建方法:
def create
@order = Order.new(order_params)
# Find listing id in url
@listing = Listing.find(params[:listing_id])
# Seller of a listing is the same as the user who created it
@seller = @listing.user
# Order id is same as the listing id
@order.listing_id = @listing.id
# Buyer Id is equal to current signed in user
@order.buyer_id = current_user.id
# Link order and seller id
@order.seller_id = @seller.id
respond_to do |format|
if @order.save
format.html { redirect_to root_url, notice: 'Order was successfully created.' }
format.json { render :show, status: :created, location: @order }
else
format.html { render :new }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
端
应用程序布局头:
<head>
<title></title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks- track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= javascript_include_tag "https://js.stripe.com/v2/" %>
<%= csrf_meta_tags %>
<%= tag :meta, :name => 'stripe-key', :content => ENV["STRIPE_PUBLIC_KEY"] %>
</head>
关闭turbolinks的列表显示页面按钮:
<div class="center">
<%= link_to 'Buy It Now', new_listing_order_path(@listing), class: 'btn btn-primary', data: { no_turbolink: true } %>
</div>
感谢您的帮助