简体   繁体   中英

uninitialized constant Stripe::Error

I'm hitting an uninitialized constant, while working with Stripe, Rails (3.2.8) and 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:

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

I'd love to get some help/guidance on how I can properly update the record.

Thanks!

First add stripe to your gemfile

gem 'stripe'

then do a bundle install

then create a file config/initializers/stripe.rb and put in the following code

require "stripe"

now restart your server.

The Stripe module doesn't implement an Error class, only a StripeError class. See the stripe-ruby documentation

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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