【发布时间】: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