'How to use multiple payment gateways function in single controller in laravel 8?

I am using PayPal, Razor pay, Stripe payment gateways in my project. When select user one of them payment gateway it redirects to assigned controller function from there it redirects its particular controller. I want to write all the functions for payment gateways in single controller to cut off redirections for customer.

enter image description here

when user select payment gateway and submit the form it redirects to mentioned below controller function

 if ($payment_mode->payment_gateway_name == "Paypal") {
                return redirect()->route('paywithpaypal', $planId);
            } else if ($payment_mode->payment_gateway_name == "Razorpay") {
                return redirect()->route('paywithrazorpay', $planId);
            } else if ($payment_mode->payment_gateway_name == "Stripe") {
                return redirect()->route('paywithstripe', $planId);
            } else if ($payment_mode->payment_gateway_name == "Bank Transfer") {
    }

and in that controller all functions is defined.



Solution 1:[1]

Create Classes as below:

  1. PaymentController
  2. Seperate classes implementing RazorpayService, StripeService, BankTransferService apis
  3. Factory Method to create class object of RazorpayService, StripeService, BankTransferService based on request parameters

PaymentController uses factory method to create object of appropriate service and calls appropriate method on this object using input parameters

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pks11