如何获得Stripe令牌?

时间:2017-04-03 23:52:55

标签: ruby-on-rails stripe-payments

我正在用条纹设置我的应用程序,我正在度过一段可怕的时光。 我终于意识到我的令牌正在返回零。 我的日志错误是

Parameters: {"utf8"=>"✓", "authenticity_token"=>"RqCVc9YMVLA4pQ1DkOyfw4c0+1Fvohq/TLCxkBl/dQJLKOdvdutHv65A6xU7c2m/f262JhP2h5knhSAcZoz6/w==", "order"=>{"address"=>"", "city"=>"", "state"=>""}, "listing_id"=>"1"}
  User Load (9.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 4], ["LIMIT", 1]]
  Listing Load (11.4ms)  SELECT  "listings".* FROM "listings" WHERE "listings"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
TOKEN IS:
Completed 500 Internal Server Error in 1474ms (ActiveRecord: 22.8ms)

我已经打印出我的令牌,它返回零。这反过来又给了我一个必须提供的来源或客户。错误。我的heroku日志说同样的事情。我的应用就在这里(https://abound.herokuapp.com/listings/16/orders/new

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 
      require "stripe"
      Stripe.api_key = ENV["STRIPE_API_KEY"]
      token = params[:stripeToken] 
  logger.debug "TOKEN IS: #{token}" 

      begin 
>>ERROR      charge = Stripe::Charge.create( 
          :amount => (@listing.price * 100).floor, 
          :currency => "usd", 
          :source => 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, notice: "Thanks for ordering!" } 
          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 

文档说格式应为

require "stripe"
Stripe.api_key = "sk_test_wCVovzzFUCYefEcIDpnCzcNq"

Stripe::Charge.create(
  :amount => 2000,
  :currency => "usd",
  :source => "tok_1A4KM1LAntdwOYiPJCq8tGRn", # obtained with Stripe.js
  :description => "Charge for abigail.miller@example.com"
)

1 个答案:

答案 0 :(得分:0)

这里的代码没有任何问题。我没有在我的密钥周围加上引号,这导致令牌为零。另外,我把它 来源'https://code.stripe.com' 在我的gemfile之上。