refberg / sylius-payu-plugin
PayU REST API payment gateway for Sylius 2, built on Payment Requests (no Payum).
Package info
github.com/refberg/SyliusPayUPlugin
Type:sylius-plugin
pkg:composer/refberg/sylius-payu-plugin
Requires
- php: ^8.2
- ext-filter: *
- ext-json: *
- ext-mbstring: *
- doctrine/dbal: ^3.10 || ^4.0
- doctrine/orm: ^2.20 || ^3.3
- psr/log: ^3.0
- sylius/resource-bundle: ^1.12
- sylius/sylius: ^2.2
- symfony/cache-contracts: ^3.4
- symfony/config: ^6.4 || ^7.4 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.4 || ^8.0
- symfony/form: ^6.4 || ^7.4 || ^8.0
- symfony/http-client: ^6.4 || ^7.4 || ^8.0
- symfony/http-client-contracts: ^3.4
- symfony/http-foundation: ^6.4 || ^7.4 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.4 || ^8.0
- symfony/routing: ^6.4 || ^7.4 || ^8.0
- symfony/validator: ^6.4 || ^7.4 || ^8.0
- symfony/workflow: ^6.4 || ^7.4 || ^8.0
Requires (Dev)
- doctrine/data-fixtures: ^1.8 || ^2.0
- doctrine/persistence: ^3.4 || ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-doctrine: ^2.1
- phpstan/phpstan-phpunit: ^2.1
- phpstan/phpstan-strict-rules: ^2.1
- phpstan/phpstan-symfony: ^2.1
- phpstan/phpstan-webmozart-assert: ^2.0
- phpunit/phpunit: ^11.5 || ^12.5 || ^13.4
- rector/rector: ^2.8
- shipmonk/composer-dependency-analyser: ^1.8
- sylius-labs/coding-standard: ^4.4
- sylius/test-application: ^2.2.0@alpha || ^2.3.0@alpha
- symfony/browser-kit: ^6.4 || ^7.4 || ^8.0
- symfony/cache: ^6.4 || ^7.4 || ^8.0
- symfony/debug-bundle: ^6.4 || ^7.4 || ^8.0
- symfony/dotenv: ^6.4 || ^7.4 || ^8.0
- symfony/framework-bundle: ^6.4 || ^7.4 || ^8.0
- symfony/intl: ^6.4 || ^7.4 || ^8.0
- symfony/process: ^6.4 || ^7.4 || ^8.0
- symfony/runtime: ^6.4 || ^7.4 || ^8.0
- symfony/web-profiler-bundle: ^6.4 || ^7.4 || ^8.0
- symfony/webpack-encore-bundle: ^2.2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-27 06:30:29 UTC
README
PayU payment gateway for Sylius 2, built on Sylius Payment Requests and the PayU REST API v2.1. It does not use Payum.
Features
- PayU hosted payment page (redirect) in the shop checkout and in the Sylius API.
- Payment state is changed only by a signed PayU notification or by a direct query to the PayU API. The customer's return from PayU is never treated as proof of payment.
- Notifications are verified (
OpenPayu-Signature), then the order is read again from the PayU API and checked against the Sylius payment: PayU order ID,extOrderId, POS ID, amount and currency. - Duplicated and out-of-order notifications do not change the result and never pay an order twice.
- Full refunds from the admin panel; refunds made in the PayU panel are synchronized.
- Cancelling a payment in Sylius cancels the open PayU order.
- Credentials are stored encrypted by Sylius and are not written to logs.
Out of scope: saved cards, recurring payments, embedded card form, partial refunds started from Sylius.
Requirements
| Sylius | PHP | Symfony |
|---|---|---|
| 2.2.13+ | 8.2–8.4 | 6.4, 7.4 |
| 2.3.3+ | 8.3–8.5 | 6.4, 7.4, 8.x (8.x needs PHP 8.4+) |
The Sylius shop (SyliusShopBundle) and admin panel (SyliusAdminBundle) are optional, so the plugin also works
in API-only applications.
The sylius_payment state machine must use the Symfony Workflow adapter, which is the Sylius default.
The plugin sends refunds and cancellations to PayU from listeners of the workflow.sylius_payment.completed.refund
and workflow.sylius_payment.completed.cancel events. When sylius_payment uses the winzou adapter
(winzou_state_machine in sylius_state_machine_abstraction.graphs_to_adapters_mapping or default_adapter),
refunds and cancellations made in Sylius are not sent to PayU and have to be made in the PayU panel.
Installation
-
Install the package:
composer require refberg/sylius-payu-plugin
-
Register the bundle in
config/bundles.php, if Symfony Flex did not do it:Refberg\SyliusPayUPlugin\RefbergSyliusPayUPlugin::class => ['all' => true],
-
Import the configuration in
config/packages/refberg_sylius_payu.yaml:imports: - { resource: "@RefbergSyliusPayUPlugin/config/config.yaml" }
-
Make sure the Sylius gateway configuration encryption key exists (Sylius generates it during installation):
bin/console sylius:payment:generate-key
-
Applications without the Sylius shop only: import the payment notification route, which Sylius otherwise imports together with the shop routes, in
config/routes/sylius_payment.yaml:sylius_payment_notify: resource: "@SyliusPaymentBundle/Resources/config/routing/integrations/sylius.yaml"
Without this route, capture requests fail and PayU cannot deliver notifications. In an application with the admin panel, the edit page of a PayU payment method cannot be rendered either, because it shows the notification URL. API clients must also send
continue_urlin the capture payload (see API).
No database migrations are needed. The plugin keeps its data in sylius_payment.details.
Configuration
In the admin panel, go to Configuration → Payment methods → Create → PayU and fill in:
| Field | PayU panel name |
|---|---|
| Environment | sandbox or production |
| POS ID | pos_id |
| Second key (MD5) | second key (MD5) |
| OAuth client ID | OAuth protocol – client_id |
| OAuth client secret | OAuth protocol – client_secret |
The second key and the client secret are never shown again after saving. Leave the fields empty to keep the stored values.
The notification URL is sent to PayU with every order, so it does not have to be configured in the PayU panel.
It is https://<your-shop>/payment-methods/<PAYMENT_METHOD_CODE>.
Keep automatic collection (autoReceive) enabled for the POS in the PayU panel.
Orders waiting for manual confirmation (WAITING_FOR_CONFIRMATION) keep the Sylius payment processing (unpaid).
PayU retries a notification until it gets HTTP 200, for up to 72 hours. If your firewall filters incoming traffic, allow the PayU notification servers:
- production: 185.68.12.10, 185.68.12.11, 185.68.12.12, 185.68.12.26, 185.68.12.27, 185.68.12.28
- sandbox: 185.68.14.10, 185.68.14.11, 185.68.14.12, 185.68.14.26, 185.68.14.27, 185.68.14.28
For manual tests you can use the public PLN sandbox POS published in the PayU sandbox documentation.
If payment requests are processed asynchronously (SYLIUS_MESSENGER_TRANSPORT_PAYMENT_REQUEST_DSN other than sync://),
set framework.router.default_uri to the public shop URL, so the notification URL is generated correctly.
Rejected notifications still get HTTP 400, 404 or 503 as described below, because they are checked before the payment
request is dispatched. Errors while handling an accepted notification (PayU API unreachable or rejecting the plugin
credentials, a final refund status not yet shown by the PayU API) get HTTP 200 instead of 5xx, so PayU does not retry.
Messenger retries only these errors (not payment requests marked as failed), according to the retry_strategy of the
payment_request transport, and then sends the message to the payment_request_failed failure transport. That transport
is sync:// by default (SYLIUS_MESSENGER_TRANSPORT_PAYMENT_REQUEST_FAILED_DSN), which handles the message once more
instead of keeping it; set it to a persistent transport to keep failed messages, then retry them with
messenger:failed:retry --transport=payment_request_failed. A longer retry_strategy for payment_request also helps.
Requests to PayU (OAuth and REST API) use an idle timeout of 15 seconds, set by the refberg_payu.http_timeout
parameter. To change it, set the parameter in the application configuration:
parameters: refberg_payu.http_timeout: 30.0
Creating the payment method outside the admin panel
A PayU payment method created in fixtures, migrations or code must have usePayum set to false.
Sylius sets it to true by default; the admin form turns it off by itself for gateways without a Payum factory.
With usePayum set to true, the shop hands the payment over to Payum instead of the plugin, so the pay page
fails (Payum has no gateway for it), and Sylius does not encrypt the gateway configuration.
Fixture (sylius_fixtures):
sylius_fixtures: suites: default: fixtures: payment_method: options: custom: payu: code: PAYU name: PayU gatewayName: payu gatewayFactory: !php/const Refberg\SyliusPayUPlugin\PayUGateway::FACTORY_NAME gatewayConfig: !php/const Refberg\SyliusPayUPlugin\PayUGateway::CONFIG_ENVIRONMENT: sandbox !php/const Refberg\SyliusPayUPlugin\PayUGateway::CONFIG_POS_ID: '...' !php/const Refberg\SyliusPayUPlugin\PayUGateway::CONFIG_SECOND_KEY: '...' !php/const Refberg\SyliusPayUPlugin\PayUGateway::CONFIG_OAUTH_CLIENT_ID: '...' !php/const Refberg\SyliusPayUPlugin\PayUGateway::CONFIG_OAUTH_CLIENT_SECRET: '...' usePayum: false enabled: true channels: [FASHION_WEB]
Code, with the sylius.factory.payment_method and sylius.repository.payment_method services:
use Refberg\SyliusPayUPlugin\PayUGateway; $paymentMethod = $paymentMethodFactory->createWithGateway(PayUGateway::FACTORY_NAME); $paymentMethod->setCode('PAYU'); $paymentMethod->setName('PayU'); $paymentMethod->addChannel($channel); /** @var \Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface $gatewayConfig */ $gatewayConfig = $paymentMethod->getGatewayConfig(); $gatewayConfig->setGatewayName('payu'); $gatewayConfig->setUsePayum(false); $gatewayConfig->setConfig([ PayUGateway::CONFIG_ENVIRONMENT => 'sandbox', PayUGateway::CONFIG_POS_ID => '...', PayUGateway::CONFIG_SECOND_KEY => '...', PayUGateway::CONFIG_OAUTH_CLIENT_ID => '...', PayUGateway::CONFIG_OAUTH_CLIENT_SECRET => '...', ]); $paymentMethodRepository->add($paymentMethod);
How it works
Shop
- The customer places the order and clicks Pay. A PayU order is created and the customer is redirected to PayU.
- After the payment the customer returns to the shop (
/order/after-pay/{hash}). The plugin asks the PayU API for the order status. Query parameters added by PayU (e.g.error=501) are ignored. While PayU is still processing the payment, the customer sees the thank-you page and the payment isprocessing(unpaid). - PayU sends a notification to
/payment-methods/{code}. The payment is completed or failed based on the order state read from the PayU API.
Opening the pay page again reuses the PayU order that is still unpaid (NEW), instead of creating a new one.
While the PayU order is PENDING or WAITING_FOR_CONFIRMATION, Pay leads to the thank-you page
(see Payment state rules).
Once the notification or the status check finds that PayU order completed or canceled, its capture payment request
is completed or failed, so Pay creates a new PayU order instead of sending the customer to a finished one.
API
Create a payment request for a placed order:
POST /api/v2/shop/orders/{tokenValue}/payment-requests Content-Type: application/ld+json { "paymentId": 123, "paymentMethodCode": "PAYU", "payload": {"continue_url": "https://app.example/payment/return"} }
responseData.redirect_url contains the PayU payment page. continue_url is optional when the shop is installed.
Without it, the customer returns to the shop's after-pay page. Without the shop, a capture request without
continue_url fails and PayU is not called.
When the customer is back, ask for the status:
POST /api/v2/shop/orders/{tokenValue}/payment-requests Content-Type: application/ld+json {"paymentId": 123, "paymentMethodCode": "PAYU", "action": "status"}
responseData.payment_state holds the Sylius payment state and responseData.payu_status the PayU order status.
Payment state rules
| PayU order status | Sylius payment |
|---|---|
NEW |
unchanged (new) |
PENDING, WAITING_FOR_CONFIRMATION |
processing, only from new and only when the payment still uses a PayU method |
COMPLETED |
completed, once |
CANCELED |
failed, only when no other PayU order of this payment is still open and the payment still uses a PayU method |
A payment that is completed is never moved back. If a second PayU order of the same payment is paid
(for example after the customer retried in another tab), the Sylius order is not paid twice. The plugin
records a warning in the payment details (refberg_payu.warnings) and logs a critical message on the payu channel.
The same applies to a PayU order paid after the payment failed or was cancelled in Sylius
(payment_completed_after_failure warning). Refund such a payment manually in the PayU panel.
While the payment is processing, Sylius does not let the customer change its payment method in the shop or through
the API: the shop order page shows no payment method choice, Pay leads to the thank-you page, and
PATCH /api/v2/shop/orders/{tokenValue}/payments/{paymentId} ends with HTTP 500 from the Sylius core
(Assert::same in PaymentMethodChanger), not with a 4xx error. The API still accepts
POST /api/v2/shop/orders/{tokenValue}/payment-requests with another payment method for a processing payment,
because the Sylius core does not block it. The PayU capture rejects a payment that is not new, but the gateway
of another method may handle it; the plugin leaves this decision to Sylius. When PayU then cancels the order,
the payment fails and Sylius adds a new payment to the order, so the customer can pay again.
If the payment still has another open PayU order in NEW (for example, the cancel request sent to PayU
when the customer switched the payment method failed) and PayU cancels the PENDING order, the payment stays
processing until that other order is cancelled or expires, or an administrator changes the payment state.
When the customer switches to another payment method while the PayU order is still NEW, Sylius cancels the capture
payment request and the plugin asks PayU to cancel that order. The answer is only logged: PayU answers with success
also for an order it did not cancel, so the order status changes only with a notification or a status check.
A CANCELED order of a payment that now uses a method outside PayU does not fail the payment.
The payment becomes processing only when the plugin learns the PENDING status. If the customer starts paying in PayU
while the order is still NEW locally and switches the method before the PENDING notification or a status check arrives,
the switch is allowed. A later PENDING does not move that payment to processing, because it no longer uses a PayU
method, so the shop Pay page pays with the other method without any PayU check. If both payments succeed,
the PayU one gets the payment_completed_after_failure or paid_with_previous_payment_method warning
(see above); refund the extra payment manually.
If the customer switches the order to another payment method after being redirected to PayU, the PayU order
created before the switch stays linked to the payment. It can still be paid only if it was not cancelled: it was
not NEW locally at the switch (so the plugin did not ask PayU to cancel it) or PayU did not cancel it. The payment is completed by its notification
or by a status check (the customer's return from PayU or the status payment request through the API) once
the PayU API confirms the order is paid; the plugin sets the payment method back to the PayU method that was paid,
records the paid_with_previous_payment_method warning and logs a warning. Refunds of that payment from Sylius
therefore reach PayU.
A payment cancelled in Sylius before the old PayU order is paid still has the new method, so the cancellation may not reach that order:
- Another PayU method: the cancellation is sent with the new method's credentials. For an order of another POS PayU is expected to refuse it; the payment request then fails and an error is logged.
- A method outside PayU: the cancellation is not sent to PayU. The old PayU order stays open unless it was cancelled
at the method switch (see above); if the customer pays it,
the payment stays cancelled and gets the
payment_completed_after_failurewarning.
In both cases refund or cancel the order in the PayU panel.
Notifications with a missing or invalid signature get HTTP 400 and are not stored. Notifications for unknown orders get HTTP 404. When the PayU payment method is not configured, the endpoint answers with HTTP 503. When the PayU API cannot be reached or rejects the plugin credentials (HTTP 401/403), the endpoint answers with HTTP 5xx, so PayU retries later.
Refunds
Click Refund on a completed PayU payment in the admin panel. The plugin requests a full refund in PayU
(extRefundId is the payment request ID, so a redelivered request does not create a second refund). The final
refund status is stored in the payment details.
After verifying the signature of a refund notification, the plugin reads the refund from the PayU API
(GET /api/v2_1/orders/{orderId}/refunds) and takes its status, amount and currency from there, not from the
notification body. A refund that the API does not list for the order in the payment currency fails the notification
payment request and changes nothing. When the notification reports a final refund status that the API does not show yet,
the endpoint answers with HTTP 5xx and changes nothing, so PayU retries the notification later.
Refunds already made in the PayU panel (except canceled ones) are subtracted from the requested amount. When they cover the whole payment, the plugin does not call PayU and completes the refund payment request.
Panel refunds still in progress are subtracted as well. If PayU cancels such a refund after the refund from Sylius,
the customer gets back only the amount sent to PayU (nothing when the panel refunds covered the whole payment).
The payment stays refunded and the plugin logs an error; refund the rest manually in the PayU panel.
Sylius marks the payment as refunded right away, before PayU confirms the refund. If PayU rejects the refund, the refund payment request is marked as failed and an error is logged. The refund then has to be handled in the PayU panel. Sylius does not retry a failed request automatically, also when PayU was unavailable (timeout, HTTP 5xx).
Cancellation
When a PayU payment is cancelled in Sylius (for example when the order is cancelled), the plugin cancels every
PayU order of that payment that may still be paid (DELETE /api/v2_1/orders/{orderId}) and then reads the order
back, because PayU accepts the cancel of an already paid order too. If PayU refuses the cancel or the order is not
CANCELED afterwards (for example it was already paid), the cancel payment request is marked as failed and an error
is logged; the later COMPLETED notification is recorded as a warning and the payment is not completed. Sylius does not retry a failed
cancel request automatically, also when PayU was unavailable (timeout, HTTP 5xx); check the order in the PayU panel.
Refunds made in the PayU panel
A signed refund notification for a refund that Sylius did not request is recorded in the payment details, unless
its amount read from the PayU API is larger than the payment.
When finalized refunds cover the whole payment amount, the payment is moved to refunded without sending another
refund to PayU. Partial refunds only show up in the payment details, because Sylius has no state for them.
Security notes
- Secrets are stored in the gateway configuration, which Sylius encrypts.
- Refunds and cancellations are sent to PayU only for a payment that is already refunded or cancelled in Sylius, so they cannot be triggered through the shop API.
- Secrets and OAuth tokens are never logged or put into payment request data. Exception messages contain only HTTP and PayU status codes.
- The OAuth token is cached in the
refberg_payu.cachepool (by defaultcache.app) until shortly before it expires. - PayU traffic goes through the
refberg_payu.http_clientservice (an alias ofhttp_client). You can override it, for example to use a proxy.
Extending
The services below are registered under their interface. Decorate or replace them by the interface:
| Service ID | Responsibility |
|---|---|
Refberg\SyliusPayUPlugin\Api\AccessTokenProviderInterface |
OAuth token for the PayU API |
Refberg\SyliusPayUPlugin\Api\PayUClientInterface |
PayU REST API calls |
Refberg\SyliusPayUPlugin\Api\PayURefundReaderInterface |
Reading the refunds of a PayU order |
Refberg\SyliusPayUPlugin\Config\CredentialsResolverInterface |
PayU credentials from the gateway configuration |
Refberg\SyliusPayUPlugin\Signature\SignatureVerifierInterface |
OpenPayu-Signature header verification |
Refberg\SyliusPayUPlugin\Notification\NotificationParserInterface |
Signed notification parsing |
Refberg\SyliusPayUPlugin\Order\OrderRequestFactoryInterface |
Body of the PayU order request |
Refberg\SyliusPayUPlugin\Order\ExtOrderIdGeneratorInterface |
extOrderId values |
Refberg\SyliusPayUPlugin\Order\PayUOrderValidatorInterface |
Check of a PayU order against the Sylius payment |
Refberg\SyliusPayUPlugin\Payment\PaymentLockerInterface |
Payment lock |
Refberg\SyliusPayUPlugin\Payment\PaymentStateApplierInterface |
Payment state changes |
Refberg\SyliusPayUPlugin\Payment\PaymentSynchronizerInterface |
Fetching, checking and applying a PayU order |
PayURefundReaderInterface is a separate service (also PayUClient), so a decorator or replacement of
PayUClientInterface does not change how refunds are read; decorate both when you need to.
For example, to change the extOrderId format:
services: App\Payment\ExtOrderIdGenerator: decorates: Refberg\SyliusPayUPlugin\Order\ExtOrderIdGeneratorInterface arguments: ['@.inner']
extractPaymentId() must read the payment ID back from every value returned by generate(),
because notifications are matched to payments by it.
Development
composer install
vendor/bin/ecs check
vendor/bin/rector process --dry-run
vendor/bin/composer-dependency-analyser
APP_ENV=test vendor/bin/console cache:warmup # PHPStan reads the compiled test container
vendor/bin/phpstan analyse
vendor/bin/phpunit --testsuite=unit
PHPStan runs at level max with the strict, deprecation, Doctrine, Symfony and PHPUnit rules. CI also checks
composer normalize, composer audit and runs mutation tests with Infection (PHP 8.3+, infection.json5).
Integration tests run the Sylius test application against MySQL:
docker run -d --name payu-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -p 3306:3306 mysql:8.0 APP_ENV=test vendor/bin/console doctrine:database:create APP_ENV=test vendor/bin/console doctrine:schema:create vendor/bin/phpunit --testsuite=integration
Tests never call PayU. All requests go to an in-memory fake of the PayU sandbox (tests/Support/FakePayU.php),
which rejects any other host. The tests include signed, duplicated, out-of-order and forged notifications.
Tests against the PayU sandbox
tests/Sandbox runs the whole flow against the real PayU sandbox: payment in the shop, payment on the PayU page
(accepted and rejected, using the provider simulator), status check, refund and cancellation. It is not part of CI
and is skipped unless enabled. Credentials are read from environment variables only; you can use the public
sandbox POS from the PayU sandbox documentation.
npm install playwright && npx playwright install chromium
PAYU_SANDBOX=1 PAYU_SANDBOX_POS_ID=... PAYU_SANDBOX_SECOND_KEY=... \
PAYU_SANDBOX_CLIENT_ID=... PAYU_SANDBOX_CLIENT_SECRET=... \
vendor/bin/phpunit --testsuite=sandbox
PayU cannot deliver notifications to a local machine, so these tests read the payment state from the PayU API. To check notification delivery, run the shop on a public URL (for example through a tunnel).
License
MIT, see LICENSE.