簡體   English   中英

未初始化的常數Stripe :: Error

[英]uninitialized constant Stripe::Error

在使用Stripe,Rails(3.2.8)和Ruby(1.9.2)時,我遇到了一個未初始化的常量。

最初,我的銷售模型使用以下內容(這可行!):

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

然后,我決定我要使用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

結果是: uninitialized constant Stripe::Error

我很想獲得一些有關如何正確更新記錄的幫助/指導。

謝謝!

首先將條紋添加到您的gemfile

gem 'stripe'

然后進行bundle install

然后創建一個文件config/initializers/stripe.rb並放入以下代碼

require "stripe"

現在重新啟動服務器。

Stripe模塊不實現Error類,僅實現StripeError類。 請參閱stripe-ruby文檔

如果您將代碼更改為

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

它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM