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:
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:
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:
namespaceAcme\SyliusExamplePlugin\CommandHandler;useAcme\SyliusExamplePlugin\Command\CapturePaymentRequest;useSylius\Abstraction\StateMachine\StateMachineInterface;useSylius\Bundle\PaymentBundle\Provider\PaymentRequestProviderInterface;useSylius\Component\Payment\PaymentRequestTransitions;useSymfony\Component\Messenger\Attribute\AsMessageHandler;#[AsMessageHandler]finalreadonlyclassCapturePaymentRequestHandler{publicfunction__construct(privatePaymentRequestProviderInterface $paymentRequestProvider,privateStateMachineInterface $stateMachine, ) {}publicfunction__invoke(CapturePaymentRequest $capturePaymentRequest):void {// Retrieve the current PaymentRequest based on the hash provided in the CapturePaymentRequest command $paymentRequest =$this->paymentRequestProvider->provide($capturePaymentRequest);// Custom capture logic for the payment provider would go here.// Example: communicating with the payment gateway API to capture funds.// Mark the PaymentRequest as complete|process|fail|cancel.$this->stateMachine->apply( $paymentRequest,PaymentRequestTransitions::GRAPH,PaymentRequestTransitions::TRANSITION_COMPLETE); }}
In this handler, we:
Retrieve the current PaymentRequest using the PaymentRequestProviderInterface.
Implement any custom logic to handle the capture action, such as calling an external API.
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.
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).
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.