Never had to implement something like this (we're using Stripe only), but my approach would probably be to create a trait with a function calling the function of the specific provider. So lets say the function is called processPayment, it could receive all your necessary elements as parameters (including the provider) and then it would pass these to the appropriate function. Here I'm returning values from functions that are called, but it's not necessary if you have nothing to return.
/**
* Process the payment using the specified provider.
*
* @param string $provider Name of the provider
* @param mixed $paramX Whatever it is
* @param mixed $paramY Whatever it is
* @param mixed $paramZ Whatever it is
* @return mixed Whatever you want to return
*/
public function processPayment(string $provider, $paramX, $paramY, $paramZ)
{
$method = 'process' . Str::studly($provider) . 'Payment';
if (method_exists($this, $method)) {
return $this->{$method}($paramX, $paramY, $paramZ);
}
return $this->invalidProvider();
}
/**
* Process the payment using the PayPal provider.
*
* @param mixed $paramX Whatever it is
* @param mixed $paramY Whatever it is
* @param mixed $paramZ Whatever it is
* @return mixed Whatever you want to return
*/
public function processPaypalPayment($paramX, $paramY, $paramZ)
{
// Your logic
return $whatever;
}
/**
* Process the payment using the Square provider.
*
* @param mixed $paramX Whatever it is
* @param mixed $paramY Whatever it is
* @param mixed $paramZ Whatever it is
* @return mixed Whatever you want to return
*/
public function processSquarePayment($paramX, $paramY, $paramZ)
{
// Your logic
return $whatever;
}
/**
* Process the payment using the Stripe provider.
*
* @param mixed $paramX Whatever it is
* @param mixed $paramY Whatever it is
* @param mixed $paramZ Whatever it is
* @return mixed Whatever you want to return
*/
public function processStripePayment($paramX, $paramY, $paramZ)
{
// Your logic
return $whatever;
}
/**
* Do whatever if the provider is invalid.
*
* @return mixed Whatever you want to return
*/
public function invalidProvider()
{
// Your logic
return $whatever;
}