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
Create a Sylius Plugin First, set up a plugin for your custom gateway. Follow the guide to creating a Sylius Plugin.
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
:
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
).
Add Field Templates Create a template for each field you need for example
templates/admin/payment_method/form/api_key.html.twig
:
Register Field Templates in Twig Hooks Register the field templates in
config/config.yaml
using Twig hooks:
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:
Retrieve the current
PaymentRequest
using thePaymentRequestProviderInterface
.Implement any custom logic to handle the capture action, such as calling an external API.
Apply the
complete
transition to thePaymentRequest
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
, orrefund
, 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 assubscription
, 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.
1. 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
2. Register the Response Provider
To activate your response provider, add it to your service configuration.
Service Registration: config/services.yaml
3. 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.
4. 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