首页 > 解决方案 > 为什么 server.rb 在终端中抛出此错误

问题描述

我正在关注这个条带教程,但是当我运行Ruby server.rb时,来自 Stripe 的 Github 示例中的 server.rb 抛出错误

我对红宝石很陌生,所以我可能做错了。

我所做的是:

这是错误

1: from server.rb:10:in '<main.'
server.rb:10:in 'join': no implicit conversation of nil into string (TypeError)

这是 server.rb 文件

require 'stripe'
require 'sinatra'
require 'dotenv'

# Replace if using a different env file or config
Dotenv.load
Stripe.api_key = ENV['STRIPE_SECRET_KEY']

set :static, true
set :public_folder, File.join(File.dirname(__FILE__), ENV['STATIC_DIR'])
set :views, File.join(File.dirname(__FILE__), ENV['STATIC_DIR'])
set :port, 4242

get '/' do
  content_type 'text/html'
  send_file File.join(settings.public_folder, 'index.html')
end

post '/webhook' do
  # You can use webhooks to receive information about asynchronous payment events.
  # For more about our webhook events check out https://stripe.com/docs/webhooks.
  webhook_secret = ENV['STRIPE_WEBHOOK_SECRET']
  payload = request.body.read
  if !webhook_secret.empty?
    # Retrieve the event by verifying the signature using the raw body and secret if webhook signing is configured.
    sig_header = request.env['HTTP_STRIPE_SIGNATURE']
    event = nil

    begin
      event = Stripe::Webhook.construct_event(
        payload, sig_header, webhook_secret
      )
    rescue JSON::ParserError => e
      # Invalid payload
      status 400
      return
    rescue Stripe::SignatureVerificationError => e
      # Invalid signature
      puts "⚠️  Webhook signature verification failed."
      status 400
      return
    end
  else
    data = JSON.parse(payload, symbolize_names: true)
    event = Stripe::Event.construct_from(data)
  end
  # Get the type of webhook event sent - used to check the status of PaymentIntents.
  event_type = event['type']
  data = event['data']
  data_object = data['object']

  if event_type == 'some.event'
    puts "  Webhook received!"
  end

  content_type 'application/json'
  {
    status: 'success'
  }.to_json
end

标签: rubystripe-payments

解决方案


  1. stripe login

    这是至关重要的一步。

  2. stripe samples create adding-sales-tax

  3. cd adding-sales-tax/server

  4. bundle install

    如果你没有捆绑器,gem install bundler

  5. bundle exec ruby server.rb

  6. 打开 http://localhost:4242


推荐阅读