Step-by-Step Guide: Integrating Authorize.net Payment Gateway with Laravel 10 (Part-2)

radhwan ben youssef
2 min readFeb 8, 2024

--

Welcome back! In Part 1, we integrated Authorize.net with Laravel 10, creating a seamless payment gateway for our application. Now, let’s dive into two crucial aspects to enhance our payment workflow further: Middleware and Webhooks.

Step 1: Implementing Middleware

Middleware in Laravel allows us to filter HTTP requests entering our application. We can use middleware to perform actions before or after the request reaches our controllers. In the context of our payment workflow, middleware can be employed to validate and secure incoming requests.

Create a new middleware by running:

php artisan make:middleware AuthorizeNetMiddleware

In AuthorizeNetMiddleware.php, we can implement custom logic such as checking if a user has a subscription,

// app/Http/Middleware/AuthorizeNetMiddleware.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class AuthorizeNetMiddleware
{
public function handle($request, Closure $next)
{
// Custom authorization logic: Check if the user has a subscription
$user = Auth::user();

if ($user && $user->subscription) {
// User has an active subscription, proceed with the request
return $next($request);
}

// User doesn't have a subscription, handle accordingly (e.g., redirect or deny access)
return redirect()->route('subscription.required'); // Adjust the route accordingly
}
}

Now, register the middleware in App\Http\Kernel.php:

// app/Http/Kernel.php

protected $routeMiddleware = [
// Other middleware entries...
'authorize.net' => \App\Http\Middleware\AuthorizeNetMiddleware::class,
];

Apply the middleware to a route in routes/web.php:

// routes/web.php

Route::get('/courses', [PaymentController::class, 'coursesList'])
->middleware('authorize.net')
->name('courses.list');

Step 2: Implementing Webhooks

Webhooks are a crucial part of a payment system, allowing us to receive real-time updates on transaction events. Authorize.net provides webhooks to notify our application of successful payments, chargebacks, and more.

Let’s create a new controller for handling webhooks:

php artisan make:controller WebhookController

In WebhookController.php, define methods for handling different webhook events:

// app/Http/Controllers/WebhookController.php

use Illuminate\Http\Request;

class WebhookController extends Controller
{
public function handlePaymentSuccess(Request $request)
{
// Handle successful payment event
}

public function handleChargeback(Request $request)
{
// Handle chargeback event
}

// Add more methods for other webhook events
}

Register the webhook routes in routes/web.php:

// routes/web.php

Route::post('/webhook/payment-success', [WebhookController::class, 'handlePaymentSuccess']);
Route::post('/webhook/chargeback', [WebhookController::class, 'handleChargeback']);

In your Authorize.net account, configure webhooks to point to these routes.

Conclusion

By implementing middleware and webhooks, we’ve added an extra layer of security and real-time event handling to our Laravel 10 and Authorize.net payment integration. This ensures a robust and secure payment workflow for your application.

Stay tuned for Part 3, where we’ll explore advanced features and optimizations for our payment gateway!

--

--

radhwan ben youssef

I am a Tunisa based Full Stack developer who specializes in PHP . I have 6 years of experience in Web Development. I'm highly motivated, disciplined and a fast