【问题标题】:Laravel transaction not rolling back on Stripe exceptionLaravel 事务不回滚条纹异常
【发布时间】:2015-05-21 01:41:13
【问题描述】:

如果在 Stripe 收费期间出现任何问题,我需要回滚交易。我通过删除 Stripe 令牌来强制异常,只是为了测试回滚。由于某种原因,异常被捕获但事务没有回滚。我正在使用 Laravel 4.2 和 MySQL 5.6。我的表都是 InnoDB。有人遇到过这个问题吗?

DB::beginTransaction();

// create order.
try
{
   // this function writes to two tables: orders and items
   $totals = $this->doOrder();
}
catch (Exception $e)
{
   DB::rollback();
   $this->logError($e);
   return Redirect::back()->with('danger', 'An error occurred while saving your order. Your card has not been charged.');
}

// Charge user's credit card.
try 
{
   \Stripe\Stripe::setApiKey(Config::get('stripe.secret_key'));

   $charge = \Stripe\Charge::create([
      'amount'      => ($totals['grand_total'] * 100),
      'currency'    => 'usd',
      'description' => Auth::user()->email_address,
      'source'      => ''  //<-- force exception by removing Stripe token
   ]);
} 
catch(\Stripe\Error\InvalidRequest $e)
{
   DB::rollback();
   $this->logError($e);
   return Redirect::back()->with('danger', 'An error occurred while processing your payment. Your card has not been charged.');
}

// If we made it this far, all is good and we can commit.
DB::commit();

【问题讨论】:

  • 它会在第一个 try-catch 上回滚吗?
  • 是的,如果抛出 MySQL 异常,它会回滚。问题是当 Stripe 在第二次 try-catch 中抛出异常时。
  • 你能验证\Stripe\Error\InvalidRequest是否真的发现了错误?
  • 是的,错误被记录下来,并在 catch 块中重定向回消息,因此这表明它必须调用回滚函数。我已经尝试了两天了...

标签: php exception laravel transactions


【解决方案1】:

在这里回答我自己的问题,以防其他人遇到类似问题。 该问题是由我的示例代码中显示的 doOrder() 函数中执行的存储过程引起的。存储过程执行了某些语句,此时提交了任何未决的更改,因此当在 Laravel 中发出回滚时,更改已经提交。

我放弃了存储过程方法并在 PHP (Laravel) 中重写了该逻辑,从而解决了这个问题,因此一切都发生在一个地方。

【讨论】:

    猜你喜欢
    • 2013-10-16
    • 1970-01-01
    • 2012-12-28
    • 2018-08-09
    • 2016-08-14
    • 1970-01-01
    • 2020-09-24
    • 2013-11-23
    • 2015-10-03
    相关资源
    最近更新 更多