In our previous post(How to Add and Implement Payment Processing Interfaces in Laravel 11: Hardcoded Binding), we explored the first step in setting up payment processors by hardcoding the binding between the PaymentProcessorInterface and a specific implementation, like StripePaymentProcessor.
While this approach is simple and effective for small applications, it lacks flexibility for more complex scenarios where you might need to handle multiple payment gateways, but by using an interface, allowed us to decouple the code so we can further extend it, in accordance to open close principle, to inject the proper functionality:
In this second part, we will dive into contextual binding, a more advanced technique in Laravel’s service container that allows you to inject different implementations of an interface based on the specific context. This is useful when the choice of a payment processor depends on the application state, such as which controller is handling the request.
Contextual binding in Laravel allows the service container to inject different implementations of an interface depending on the class or context requesting it. Instead of relying on a single, hardcoded implementation, we can use contextual binding to resolve different payment processors based on the controller or some other contextual factor.
Let’s start by configuring contextual bindings in the AppServiceProvider. We will bind different payment processors based on the controller that is requesting them. For example, the StripePaymentController will use StripePaymentProcessor, and the PayPalPaymentController will use PayPalPaymentProcessor.
Here’s how you can do it:
use App\Contracts\PaymentProcessorInterface; use App\Services\StripePaymentProcessor; use App\Services\PayPalPaymentProcessor; public function register() { $this->app->when(StripePaymentController::class) ->needs(PaymentProcessorInterface::class) ->give(StripePaymentProcessor::class); $this->app->when(PayPalPaymentController::class) ->needs(PaymentProcessorInterface::class) ->give(PayPalPaymentProcessor::class); }
What's Happening Here?
With contextual binding in place, each controller can now have its dedicated payment processor injected automatically. Here's how you can set up your controllers:
Example: StripePaymentController
use App\Contracts\PaymentProcessorInterface; class StripePaymentController extends Controller { protected $paymentProcessor; public function __construct(PaymentProcessorInterface $paymentProcessor) { $this->paymentProcessor = $paymentProcessor; } // Methods to handle Stripe-specific payments... }
Example: PayPalPaymentController
use App\Contracts\PaymentProcessorInterface; class PayPalPaymentController extends Controller { protected $paymentProcessor; public function __construct(PaymentProcessorInterface $paymentProcessor) { $this->paymentProcessor = $paymentProcessor; } // Methods to handle PayPal-specific payments... }
In both examples, Laravel automatically injects the correct payment processor based on the controller context. This is thanks to the contextual binding set up in the AppServiceProvider.
Contextual binding is particularly useful when you know which implementation of an interface to use based on specific classes or contexts, such as controllers. It helps keep your code clean and manageable, especially when dealing with multiple payment gateways, each with its own controller.
In this post, we’ve explored how to implement contextual binding in Laravel 11 for payment processing. Here’s a quick recap of the benefits of this approach:
In the next post, we will explore the Factory Pattern, which allows for dynamic selection of payment processors at runtime, providing even more flexibility for complex applications.
Stay tuned for the next part, where we’ll cover how to use factories for payment processing in Laravel 11!
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3