4

I am trying to retrieve the Stripe charge id upon a failed charge, so I can retrieve my record thanks to that id when the charge.failed hook is fired. I tried inspect the exception fired but I cannot find any way to get it. Here is my code :

  def charge
    token = params[:stripeToken]
    type = params[:stripeTokenType]
    metadata = {}
    record = Record.new(amount: Random.rand(2000), valid: false)
    charge = nil
    begin
      charge = Stripe::Charge.create(
          {
              amount: 2000,
              currency: 'eur',
              source: token,
              description: 'Test',
              metadata: metadata
          }, { stripe_account: 'xxxxx' })
      record.stripe_charge_id
      flash[:notice] = 'Transaction validée'
    rescue Exception => e
      record.error = e.code
      flash[:error] = 'Erreur de paiement'
    end
    flash[:error] = 'Erreur de paiement' unless record.save || flash[:error]
    redirect_to :stripe_test
  end
4

2 回答 2

0

我遇到过同样的问题。在充电尝试失败时检索充电 ID 的正确方法很简单,但没有完全记录在案。Stripe 在错误中向您发送费用 ID:

这是蟒蛇!

try:
    # make the charge
except stripe.error.CardError as e:
    # Since it's a decline, stripe.error.CardError will be caught
    body = e.json_body
    err = body.get('error', {})
    charge_id = err.get('charge') # this will return ch_1EPVmxKT>DPET....
于 2019-04-15T14:36:54.390 回答
0

我终于使用元数据来存储我的记录 ID 和费用。所以我可以使用这个元数据来检索它。

charge = Stripe::Charge.create(
    {
        amount: 2000,
        currency: 'eur',
        source: token,
        description: 'Test',
        metadata: { record_id: 23 }
    }, { stripe_account: 'xxxxx' })
于 2016-03-30T08:43:50.920 回答