I want to switch the payment gateway based on the user's request. I don't want multiple endpoints for each gateway, so i'm trying write one endpoint that handles multiple payment gateways. With that, I also want the controller __construct method to use dependency injection using a gateway interface. The issue i'm running into is switching the instantiated class to match the payment gateway from the request.
use App\PaymentGatewayInterface
class XYZController {
...
public __construct(PaymentGatewayInterface $payment_gateway) {
$this->payment_gateway = $payment_gateway; //This should be a gateway instance of either Paypal, Stripe, Braintree, etc based on the $request of the user.
}
...
}
I know I can bind an interface to a class in the register()
method of a provider and add some logic there, but I can see myself adding a lot of interface dependencies in the controller actions and I feel that having logic in the service provider can become messy rather quick. Example:
...
class AppServiceProvider extends ServiceProvider
{
public function register()
{
if(request()->input('gateway') === "Paypal")
$this->app->bind(PaymentGatewayInterface::class, PaypalGateway::class);
else
$this->app->bind(PaymentGatewayInterface::class, StripeGateway::class);
}
}