17 2 Webhook Handling - RadLeoOFC/laravel-admin-panel GitHub Wiki

Webhook Handling (Optional)

Configuring webhooks is an essential step in receiving real-time payment status updates from Stripe. This ensures that the system stays synchronized with payment statuses, reflecting successful or failed transactions immediately.


1. Setting Up Stripe Webhook Listener

Before integrating the webhook, the Stripe CLI must be used to listen for webhook events and forward them to the local application.

stripe listen --forward-to http://127.0.0.1:8000/webhook/stripe

Screenshot: Stripe CLI log and webhook processing

Terminal showing the Stripe webhook listener active


2. Handling Webhook Events in Laravel

In Laravel, a WebhookController was created to handle incoming webhook requests from Stripe. The controller verifies the webhook signature and processes different types of events.

public function handle(Request $request)
{
    Stripe::setApiKey(env('STRIPE_SECRET'));

    // Retrieve the payload and signature from Stripe
    $payload = $request->getContent();
    $sigHeader = $request->header('Stripe-Signature');
    $endpointSecret = env('STRIPE_WEBHOOK_SECRET');

    try {
        $event = Webhook::constructEvent($payload, $sigHeader, $endpointSecret);
    } catch (\Exception $e) {
        Log::error("Webhook signature verification failed: " . $e->getMessage());
        return response()->json(['error' => 'Invalid signature'], 400);
    }

    Log::info("Webhook received: " . $event->type);

    switch ($event->type) {
        case 'payment_intent.succeeded':
            $this->handleSuccessfulPayment($event->data->object);
            break;
        case 'payment_intent.payment_failed':
            $this->handleFailedPayment($event->data->object);
            break;
    }

    return response()->json(['status' => 'success'], 200);
}

Screenshot: Code editor displaying the handle method in WebhookController.php.

Code editor displaying the handle method in StripeWebhookController.php


3. Testing Webhook Events

Once the webhook is set up, events can be triggered using the Stripe CLI to ensure they are correctly received.

stripe trigger payment_intent.succeeded

If the webhook is correctly set up, the event should be logged in Laravel.

Screenshot: Terminal displaying webhook events received from Stripe.

Terminal displaying webhook events received from Stripe


4. Webhook Execution Results

After triggering a payment event, the system logs should confirm that the webhook was processed correctly. Additionally, the database should reflect the updated payment status.

  • Screenshot: Database view in phpMyAdmin showing the payments table with recorded transactions.

Database view in phpMyAdmin showing the payments table with recorded transactions

  • Screenshot: Admin panel displaying updated membership payment statuses.

Admin panel displaying updated membership payment statuses


5. Handling Webhook Errors

Errors such as missing CSRF verification or incorrect webhook signing secret can prevent webhook processing. These can be debugged by:

  1. Ensuring the VerifyCsrfToken middleware excludes the webhook route.
  2. Checking the STRIPE_WEBHOOK_SECRET in the .env file.
  3. Restarting the Stripe listener and Laravel server.

Screenshot: Terminal output showing Stripe webhook errors. (Скриншот: Терминал с ошибками вебхуков Stripe)


This setup ensures that payments are automatically updated in real-time, improving system reliability and user experience.


5. Pushing the Project to GitHub

Steps Taken:

  • Committed changes:

git add .

`git commit -m "Completed Stripe integration with real-time webhook handling for payment updates"

Screenshot: Git commit `

Git commit

  • Switched to the develop branch:

git checkout develop

  • Pushed changes to GitHub:

git push origin develop

Screenshot: Git Push Output

Git Push Output


Conclusion

  • Successfully implemented email notifications for membership creation and expiration reminders.
  • Resolved timezone discrepancies between Laravel and MySQL.
  • Pushed the project to GitHub in the develop branch.