How to integrate a Payment Gateway as a Plugin?

A custom payment gateway is a common need in Sylius, given the wide range of payment providers and regional differences. This guide explains how to set up a new payment gateway that sends payment details to an external API.

Step 1: Generic Configuration

  1. Create a Sylius Plugin First, set up a plugin for your custom gateway. Follow the guide to creating a Sylius Plugin.

  2. Create the Gateway Configuration Form Define a form for your gateway’s configuration settings.

Form Type: Create the configuration type in src/Form/Type/GatewayConfigurationType.php:

namespace Acme\SyliusExamplePlugin\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

final class GatewayConfigurationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('api_key', TextType::class);
    }
}

Service Configuration: Add the form type to your service configuration in config/services.xml or config/services.yaml:

Translation: Add a label for the gateway in translations/messages.en.yaml:

From now on, your new Payment Gateway should be available in the admin panel (Url: /admin/payment-methods).

New payment method available
  1. Add Field Templates Create a template for each field you need for example templates/admin/payment_method/form/api_key.html.twig:

  1. Register Field Templates in Twig Hooks Register the field templates in config/config.yaml using Twig hooks:

Payment Gateway Config form

NOTE:

To create a new payment configuration, you need a valid encryption key. If you don’t have one, generate it by running:

Sylius encrypts configuration data like API keys and payment credentials to protect sensitive information, prevent unauthorized access, and comply with security regulations.

Step 2: Command Provider & Handler

With Sylius 2.0, a new "Payment Request" system is available. This system allows you to handle payment actions such as capture, status updates, and more through Symfony’s Messenger component. This is especially beneficial for headless implementations.

Creating a Gateway Command Provider

First, set up a command provider to specify which command should be executed for each payment request action.

Provider Service: Define the command provider service in config/services.yaml:

This setup uses a tagged locator to identify services that are tagged with acme.sylius_example.command_provider.sylius_payment and to index them by the action tag property. Each tagged service will provide the appropriate command for a specific action.

Creating an action Command Provider

Create the action CommandProvider which provides your future Command:

Then create the class related to this service:

Defining the Capture Command

Next, create a command to handle the capture action. Place this in src/Command/CapturePaymentRequest.php:

Here, the CapturePaymentRequest class implements the PaymentRequestHashAwareInterface using a trait to handle the payment request hash. This command will be dispatched to handle capture actions specifically.

Creating the Capture Command Handler

Now, create a command handler that processes the CapturePaymentRequest. Place this in src/CommandHandler/CapturePaymentRequestHandler.php:

In this handler, we:

  1. Retrieve the current PaymentRequest using the PaymentRequestProviderInterface.

  2. Implement any custom logic to handle the capture action, such as calling an external API.

  3. Apply the complete transition to the PaymentRequest state machine once the action is successfully processed.

Important Tips

  • Defining Other Actions: Follow similar steps to create commands and handlers for other actions, such as authorize, status, or refund, as required by your payment gateway.

  • Customizing the Payment Flow: You can also define additional actions beyond the predefined ones (e.g., capture, authorize). For example, if your provider supports unique actions such as subscription, define custom commands and handlers to process those actions.

  • Testing the Setup: After implementing, you can test the capture flow by simulating the action from Sylius’ admin panel or the shop's front end to ensure the entire flow works seamlessly with your new provider.

This setup allows you to handle payment actions through a clean, event-driven architecture using Symfony’s Messenger component, making it flexible and easy to integrate with various payment gateways in Sylius 2.0.

Step 3: Handling Payment via the UI and API

Sylius "Payment Request" system is designed to work statelessly, making it compatible with both API and UI interactions. For UI scenarios where you need to display specific pages, perform redirects, or present a form, you’ll need to create a custom HTTP response provider. Here’s how to configure your payment handling.

Create an Actions HTTP Response Provider

The Actions HTTP Response Provider is a service very similar to the Actions Command Provider we created earlier. Its goal is to route an action to the right HTTP Response Provider which we will create in the next chapter.

Create an HTTP Response Provider

Define a custom HTTP response provider to manage UI behavior for actions, like capture. This provider will allow you to control the response for different payment states (e.g., redirecting to a payment portal or displaying a confirmation page).

Provider Class: src/OrderPay/Provider/CaptureHttpResponseProvider.php

Register the Response Provider

To activate your response provider, add it to your service configuration.

Service Registration: config/services.yaml

Handling Other Payment Actions

For additional actions like status, refund, or cancel, repeat the command and handler creation process. This modularity allows you to tailor each payment action (e.g., StatusPaymentRequest, RefundPaymentRequest) according to your business requirements.

Using Payment Request for UI

The Payment Request system is inherently stateless, making it ideal for headless or standard setups. For UI interactions, like redirecting after payment, you can use the response providers configured above to customize the "after pay" route or provide specific UI feedback based on the payment status.


By following this setup, you’ll have a fully functional "Payment Gateway" in Sylius, equipped to manage both API and UI-based payment flows, tailored to your specific business logic.

Last updated

Was this helpful?