我一直在努力将Stripe与我的应用程序集成。目前正在建立一个双面市场。
即使我使用他们在文档/测试部分提供的卡详细信息,仍然会收到上面列出的错误。
尝试了一些修复但似乎无法解决问题。
所有相关代码都附在下面,非常感谢您的帮助。
我的 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
$('#new_order').append($('<input type="hidden" name="stripeToken" />').val(response.id))
$('#new_order')[0].submit()
else
$('#stripe_error').text(response.error.message).show()
$('input[type=submit]').attr('disabled', false)
我的 _form.html.erb
<div class="row">
<div class="col-md-6">
<div class="thumbnail">
<%= image_tag @listing.image.url %>
</div>
<h3><%= @listing.name %></h3>
<h4><%= number_to_currency(@listing.price) %></h4>
</div>
<div class="row">
<div class="col-md-5">
<%= 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>
<h4><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h4>
<ul>
<% @order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div id="stripe_error" class="alert alert-danger" style="display:none">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
<div class="form-group">
<%= f.label :email %><br>
<%= f.text_field :email, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :name %><br>
<%= f.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :address %><br>
<%= f.text_field :address, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :city %><br>
<%= f.text_field :city, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :county %><br>
<%= f.text_field :county, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :post_code %><br>
<%= f.text_field :post_code, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :phone %><br>
<%= f.text_field :phone, class: "form-control" %>
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, { :name => nil, :'data-stripe' => "number", class: "form-control" } %>
<br>
<%= label_tag :card_code, "CVC" %>
<%= text_field_tag :card_code, nil, { :name => nil, :'data-stripe' => "cvc", class: "form-control" } %>
</div>
<div class="form-group">
<%= label_tag nil, "Expiration Date" %>
<%= select_month nil, { use_two_digit_numbers: true }, { :name => nil, :'data-stripe' => "exp-month", class: "form-control" } %>
</div>
<div class="form-group">
<%= select_year nil, { start_year: Date.today.year, end_year: Date.today.year+10 }, { :name => nil, :'data-stripe' => "exp-year", class: "form-control" } %>
</div>
<div class="form-group">
<%= f.submit "Complete Order", class: "btn btn-success" %>
</div>
<% end %>
</div>
</div>
最后是 orders_controller.rb
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /orders
# GET /orders.json
def sales
@orders = Order.all.where(seller: current_user).order("created_at DESC")
end
def purchases
@orders = Order.all.where(buyer: current_user).order("created_at DESC")
end
# GET /orders/new
def new
@order = Order.new
@listing = Listing.find(params[:listing_id])
end
# POST /orders
# POST /orders.json
def create
@order = Order.new(order_params)
@listing = Listing.find(params[:listing_id])
@seller = @listing.user
@order.listing_id = @listing.id
@order.buyer_id = current_user.id
@order.seller_id = @seller.id
Stripe.api_key = ENV["STRIPE_API_KEY"]
token = params[:stripeToken]
begin
charge = Stripe::Charge.create(
:amount => (@listing.price * 100).floor,
:currency => "gbp",
:card => token
)
flash[:notice] = "Thanks for ordering!"
rescue Stripe::CardError => e
flash[:danger] = e.message
end
respond_to do |format|
if @order.save
format.html { redirect_to root_url }
format.json { render action: 'show', status: :created, location: @order }
else
format.html { render action: 'new' }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /orders/1
# PATCH/PUT /orders/1.json
private
# Use callbacks to share common setup or constraints between actions.
def set_order
@order = Order.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def order_params
params.require(:order).permit(:email, :name, :address, :city, :county, :post_code, :phone)
end
end