简体   繁体   English

未初始化的常数Stripe :: Error

[英]uninitialized constant Stripe::Error

I'm hitting an uninitialized constant, while working with Stripe, Rails (3.2.8) and Ruby (1.9.2). 在使用Stripe,Rails(3.2.8)和Ruby(1.9.2)时,我遇到了一个未初始化的常量。

Initially, my Sales model used the following (this works!): 最初,我的销售模型使用以下内容(这可行!):

def charge_card
  begin
    save!
    charge = Stripe::Charge.create(
      amount: self.amount,
      currency: "usd",
      card: self.stripe_token,
      description: self.email,
    )
    self.finish!
  rescue Stripe::Error => e
    self.update_attributes(error: e.message)
    self.fail!
  end
end

Then, I decided I wanted to update that record with some additional information from Stripe, so I changed it to the following: 然后,我决定我要使用Stripe的一些其他信息来更新该记录,因此将其更改为以下内容:

def charge_card
  begin
    save!
    charge = Stripe::Charge.create(
      amount: self.amount,
      currency: "usd",
      card: self.stripe_token,
      description: self.email,
    )
    self.update(
      stripe_id:       charge.id,
      card_expiration: Date.new(charge.card.exp_year, Charge.card.exp_month, 1),
      fee_amount:      charge.fee
    )
    self.finish!
  rescue Stripe::Error => e
    self.update_attributes(error: e.message)
    self.fail!
  end
end

This results in the following: uninitialized constant Stripe::Error 结果是: uninitialized constant Stripe::Error

I'd love to get some help/guidance on how I can properly update the record. 我很想获得一些有关如何正确更新记录的帮助/指导。

Thanks! 谢谢!

First add stripe to your gemfile 首先将条纹添加到您的gemfile

gem 'stripe'

then do a bundle install 然后进行bundle install

then create a file config/initializers/stripe.rb and put in the following code 然后创建一个文件config/initializers/stripe.rb并放入以下代码

require "stripe"

now restart your server. 现在重新启动服务器。

The Stripe module doesn't implement an Error class, only a StripeError class. Stripe模块不实现Error类,仅实现StripeError类。 See the stripe-ruby documentation 请参阅stripe-ruby文档

If you change your code to 如果您将代码更改为

def charge_card
  begin
    ...
    charge = Stripe::Charge.create()
  rescue Stripe::StripeError => e
    self.update_attributes(error: e.message)
    self.fail!
  end
end

It should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM