如果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();
答案 0 :(得分:1)
在这里回答我自己的问题,以防其他人遇到类似的问题。 问题是由我的示例代码中显示的doOrder()函数中执行的存储过程引起的。存储过程执行了某些在该点提交任何挂起更改的语句,因此在Laravel中发布回滚时,更改已经提交。
我通过抛弃存储过程方法并在PHP(Laravel)中重写该逻辑来解决这个问题,所以一切都在一个地方发生。