Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jdc1898's avatar
Level 21

Provider Switch

I am curious to know what others are doing or use when building an application that supports many third party integrations. As an example, we may support multiple payment providers, (PayPal, Stripe, Square) etc... Depending on which option is set in the config file or database, it will choose a different path through the application. There are different routes or controllers based on the selection so I am curios if there is a best practice on how to achieve this. Trying to keep the DRY principle in place as much as possible.

0 likes
1 reply
piljac1's avatar

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;
}

Please or to participate in this conversation.