ruby - 为什么 server.rb 在终端中抛出此错误
问题描述
我正在关注这个条带教程,但是当我运行Ruby server.rb时,来自 Stripe 的 Github 示例中的 server.rb 抛出错误
我对红宝石很陌生,所以我可能做错了。
我所做的是:
- 已安装 Ruby、Rails、Stripe CLI、Sinatra 和 dotenv
- 通过键入 Stripe samples create developer-office-hours 从站点下载示例
- cd 进入服务器目录并运行 ruby 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
解决方案
stripe login
这是至关重要的一步。
stripe samples create adding-sales-tax
cd adding-sales-tax/server
bundle install
如果你没有捆绑器,
gem install bundler
bundle exec ruby server.rb
打开 http://localhost:4242
推荐阅读
- javascript - 无法从父节点中删除子节点
- python - 基于 Python Web 的解释器安全问题
- sql - 在时区 GMT+8 中获取相对于当前日期的记录
- html - 如何在 Post 的链接标签中添加 HTML 标签和类 - Wordpress
- r - R - 对多个种群的每个排列进行统计测试的最简洁方法
- python - Lua/Python 中的持久套接字连接
- javascript - jinja2 - TemplateSyntaxError:模板意外结束。Jinja 正在寻找以下标签:“endfor”或“else”
- radio-button - powerapps 单选按钮控件
- ejb - 寻找和展示住在纽约的客户的最佳和最差方式是什么?有状态的、无状态的还是单例的?
- woocommerce - 从 WooCommerce 中的产品和购物车页面获取产品的最大数量