[英]Laravel transaction not rolling back on Stripe exception
如果在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();
如果有人遇到類似問題,請在這里回答我自己的問題。 該問題是由示例代碼中所示的doOrder()函數中執行的存儲過程引起的。 存儲過程執行了某些語句,這些語句當時提交了所有未決的更改,因此,在Laravel中發出回滾時,更改已被提交。
我通過放棄存儲過程方法並用PHP(Laravel)重寫該邏輯來解決了這個問題,因此所有事情都發生在一個地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.