Stripe's Rail's Checkout Guide 代码说明

Stripe's Rail's Checkout Guide code clarification

我目前正在查看来自 Stripe 的以下 guide,并遇到了这段代码。

Rails.configuration.stripe = {
  :publishable_key => ENV['stripe_test_publishable_key'],
  :secret_key      => ENV['stripe_test_secret_key']
}

Stripe.api_key = Rails.configuration.stripe[:secret_key]

我想我明白前半部分说的是什么,但Stripe.api_key = Rails.configuration.stripe[:secret_key]到底在做什么?快速按 CTRL+F 后,Stripe.api_key 未在任何地方使用。

完成指南后,我注释掉了那一行,结帐时应用程序仍然运行良好。

谁能解释一下为什么首先包含该行,甚至需要它吗?

调用 Stripe 的 API 时,您需要提供您的密钥,以便 Stripe 可以识别您。

因此 Stripe.api_keyStripe Ruby gem 内部使用,代表您进行 API 调用。

令人惊讶的是,当您评论此行时您的应用程序可以运行,因为如果您没有指定键,所有 API 调用都应该失败,并出现以下异常:

Stripe::AuthenticationError: No API key provided. Set your API key using "Stripe.api_key = ". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email support@stripe.com if you have any questions.

请注意,Checkout will display the green checkmark when it successfully turns card details into a token. That doesn't mean the charge has been created yet! That may be why you think commenting out Stripe.api_key has no effect, but if you check your logs 在您的仪表板中,您应该会看到令牌创建请求之后没有跟随着费用创建请求(因为没有您的 API 密钥,Stripe 无法判断是你提出的要求!)。