<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jovi Salonga on Medium]]></title>
        <description><![CDATA[Stories by Jovi Salonga on Medium]]></description>
        <link>https://medium.com/@jovisalonga_86529?source=rss-ad708716872e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lxG5g2ZuctRHrDHr1DlWtQ.jpeg</url>
            <title>Stories by Jovi Salonga on Medium</title>
            <link>https://medium.com/@jovisalonga_86529?source=rss-ad708716872e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 16:06:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jovisalonga_86529/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Handling Webhook in Laravel]]></title>
            <link>https://medium.com/@jovisalonga_86529/handling-webhook-in-laravel-03aab8874474?source=rss-ad708716872e------2</link>
            <guid isPermaLink="false">https://medium.com/p/03aab8874474</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[web-programming]]></category>
            <category><![CDATA[webhooks]]></category>
            <dc:creator><![CDATA[Jovi Salonga]]></dc:creator>
            <pubDate>Thu, 17 Oct 2024 08:54:22 GMT</pubDate>
            <atom:updated>2024-10-17T09:38:51.380Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kICpjSLOWxPjlW91T8RRbw.jpeg" /></figure><p>Webhooks are a vital part of modern web applications, enabling servers to communicate with each other automatically. For example, payment gateways use webhooks to notify your application when a transaction is completed. Laravel makes it easy to handle webhooks with its powerful routing and controller features. Let’s walk through how to set up a basic webhook handler in Laravel.</p><h4>Step 1: Create a Webhook Route</h4><p>First, define a route that will listen for incoming webhook requests. Webhooks are typically sent as POST requests, so we’ll create a POST route:</p><pre>// routes/web.php<br>Route::post(&#39;/webhook/payment&#39;, [WebhookController::class, &#39;handlePayment&#39;]);</pre><h4>Step 2: Create the Webhook Controller</h4><p>Next, generate a controller to handle the webhook request:</p><pre>php artisan make:controller WebhookController</pre><p>In this controller, we’ll add a method to process the webhook data:</p><pre>// app/Http/Controllers/WebhookController.php<br>namespace App\Http\Controllers;<br><br>use Illuminate\Http\Request;<br>use Illuminate\Support\Facades\Log;<br><br>class WebhookController extends Controller<br>{<br>    public function handlePayment(Request $request)<br>    {<br>        // Log the incoming webhook for debugging<br>        Log::info(&#39;Webhook Received&#39;, $request-&gt;all());<br><br>        // Validate the webhook payload<br>        $request-&gt;validate([<br>            &#39;transaction_id&#39; =&gt; &#39;required|string&#39;,<br>            &#39;status&#39; =&gt; &#39;required|string&#39;,<br>            &#39;amount&#39; =&gt; &#39;required|numeric&#39;,<br>        ]);<br><br>        // Process the webhook (e.g., update transaction status in your database)<br>        $transactionId = $request-&gt;input(&#39;transaction_id&#39;);<br>        $status = $request-&gt;input(&#39;status&#39;);<br>        $amount = $request-&gt;input(&#39;amount&#39;);<br><br>        // For demonstration, we&#39;ll log the transaction details<br>        Log::info(&quot;Transaction {$transactionId} has status {$status} and amount {$amount}&quot;);<br><br>        // Return a 200 response to acknowledge receipt of the webhook<br>        return response()-&gt;json([&#39;message&#39; =&gt; &#39;Webhook handled successfully&#39;], 200);<br>    }<br>}</pre><h4>Step 3: Secure the Webhook</h4><p>It’s crucial to verify the authenticity of the webhook to ensure it’s coming from a trusted source. Many services like Stripe or PayPal provide a signature in the webhook request headers that can be used for verification.</p><p>Here’s how to validate the signature for an incoming webhook:</p><pre>// Assuming the signature is sent in a custom header &#39;X-Signature&#39;<br>$signature = $request-&gt;header(&#39;X-Signature&#39;);<br><br>// Calculate a hash of the payload using your webhook secret<br>$calculatedHash = hash_hmac(&#39;sha256&#39;, $request-&gt;getContent(), env(&#39;WEBHOOK_SECRET&#39;));<br><br>if (!hash_equals($calculatedHash, $signature)) {<br>    abort(403, &#39;Invalid signature.&#39;);<br>}</pre><p>This ensures that only webhooks from your trusted service can be processed.</p><h4>Step 4: Testing the Webhook</h4><p>To test your webhook locally, you can use tools like <a href="https://ngrok.com/">ngrok</a> to create a public URL that tunnels to your local development server:</p><pre>ngrok http 8000</pre><p>Once ngrok is running, you can use the public URL (e.g., https://randomstring.ngrok.io/webhook/payment) to receive webhook requests on your local machine.</p><h4>Conclusion</h4><p>Handling webhooks in Laravel is straightforward with its routing and controller system. By setting up a route, creating a controller to process the data, and verifying the authenticity of the webhook, you can build a secure and reliable webhook listener in just a few steps. Webhooks are key for integrating external services into your Laravel application, from payment processors to notification systems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=03aab8874474" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>