Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jnewbon's avatar

Database insert not working when followed by an exception

I have a payment system in place and working, and I'm trying to implement a log of the responses from the processor API, in the database.

If payments are successful the code works and a new entry in the database is created, however if the payment fails, the entry in the database isn't created.

The thrown exception for failed payment is handled by the calling function, but something is happening to cause the create action for the log to not only fail but pretend to succeed. No exception for the database is thrown and the next lines are run. When testing the result of the create it returns true.

       $charge = $this->chargeUser($user, $price, $nonce);

        TransactionApiLog::create([
            'user_id'           => $user->id,
            'transaction_id'    => $transaction->id,
            'success'           => $charge->success,
            'message'           => $charge->message,
            'api_response'      => serialize($charge)
        ])->save();

        if (!$charge->success) {
            throw new Exception('Unable to process a charge: ' . $charge->message);
        }

I would rather not remove the payment failed exception as that would require some extensive changes in the handling system, but from what i can tell that is the cause of the failed insert to the database.

I tried also tried using DB:table and it results in the same situation.

Another interesting thing is this error only occurs when i deploy to a staging site, (obvs cant test this on live) the code works as expected within a development environment.

Edit Using Laravel 5.3

0 likes
4 replies
click's avatar

"... the payment failed exception.."

Which exception is this? Is this the one you throw when $charge->success is false? Or is this one thrown within $this->chargeUser() which we don't see in this chunk of code?

If it is the last one you should wrap your code in a try catch

try {
  $charge = $this->chargeUser(); 

 // ... etc
} catch (PaymentFailedException $e) {
   // if you have specific exception log it here
} catch (\Exception $e) {
   // catch any other unexpected exceptions and do what you want 
}

ps. The ->save() is not necessary in TransactionApiLog::create()->save(). Create is already saving the object.

jnewbon's avatar

Which exception is this?

The one that's thrown after payment $charge->success is false

Delving into the $this->chargeUser() goes through 2 wrappers functions then into, Braintrees API the it returns all success and failures are a returned response variable so the only exception is after the create.

What i find interesting is i have proven the the lines of code after the 'TransactionApiLog::create are being executed, yet the create acts as if it wasn't even there, no error, and a return of true.

I have started the task of removing the exception as part of the logic flow and using a return array with true false and a message. But I'm keen to understand why this failed.

I should also mention this is on Laravel 5.3.

click's avatar

Could it be a database transaction that is not being comitted? Instead of writing it to a DB, write it to a file.

jnewbon's avatar

I managed to solve the issue, my mistake on not checking the dependency version on staging, matched the version and it fixed itself.

The old version was throwing an exception in the charge user function which broke everything.

Please or to participate in this conversation.