Installation
composer require sylius/payment
Add to config/app.php under providers:
Sylius\Component\Payment\PaymentComponent::class,
First Use Case: Basic Payment Flow
PaymentMethod (e.g., credit card, PayPal):
$paymentMethod = new PaymentMethod('credit_card', 'Credit Card');
Payment entity for an order:
$payment = new Payment();
$payment->setAmount(1000); // 1000 cents = $10.00
$payment->setCurrencyCode('USD');
$payment->setMethod($paymentMethod);
New state (default):
$payment->complete(); // Transitions to 'completed' state
Where to Look First
src/Resources/config/payment.yaml (default state machine config)src/PaymentMethodInterface.php and src/PaymentInterface.php (core interfaces)Payment State Management
Use the state machine to handle transitions (e.g., new → completed → cancelled):
$payment->complete(); // Validates amount, currency, and method
$payment->cancel(); // Reverts to 'cancelled' state
PaymentStates or override the state machine in config.Payment Methods
$paymentMethodRepository->add(new PaymentMethod('paypal', 'PayPal'));
$paypalMethod = $paymentMethodRepository->findOneBy(['code' => 'paypal']);
Integration with Gateways (e.g., Payum)
PaymentGatewayInterface to abstract gateway logic:
$gateway = new PayumGateway(); // Example (not part of Sylius/Payment)
$payment->setGateway($gateway);
$payment->execute(); // Delegates to gateway
Events for Extensibility Listen to payment state changes:
$dispatcher->addListener(
PaymentEvents::PAYMENT_COMPLETED,
function (PaymentCompletedEvent $event) {
// Send confirmation email, update inventory, etc.
}
);
PaymentValidator to check amounts/currencies before transitions.PaymentRetryStrategy for failed payments.PaymentEvents (e.g., PAYMENT_FAILED).State Machine Quirks
$payment->getState() after transitions.PaymentStates to add custom validation:
# config/payment.yaml
sylius_payment:
states:
completed:
transitions:
cancel:
to: cancelled
guard: 'paymentGuard' # Custom guard method
Currency/Amount Handling
$10.00 = 1000). Use Money class for conversions:
$money = new Money(1000, 'USD');
$payment->setAmount($money->getAmount());
bcmath or gmp for calculations.Gateway Integration
PaymentGatewayInterface to decouple logic:
interface PaymentGatewayInterface {
public function execute(Payment $payment): void;
}
Thread Safety
Payment instance.$payment->setDebug(true); // Logs transitions to Symfony's logger
PaymentValidationContext for errors:
$validator = new PaymentValidator();
$errors = $validator->validate($payment);
PaymentEvents to trace flow:
$dispatcher->addListener(PaymentEvents::PAYMENT_CREATED, function ($event) {
\Log::debug('Payment created:', [$event->getPayment()->getId()]);
});
Custom States/Transitions Extend the state machine in config:
sylius_payment:
states:
pending_review:
type: workflow
transitions:
approve:
to: completed
Payment Methods
Create dynamic methods via PaymentMethodRegistry:
$registry->add('stripe', function () {
return new StripePaymentMethod();
});
Validation
Add custom rules to PaymentValidationContext:
$context->addConstraint(new CustomPaymentConstraint());
Gateways
Implement PaymentGatewayInterface for custom providers:
class CustomGateway implements PaymentGatewayInterface {
public function execute(Payment $payment) {
// Custom logic (e.g., API calls)
}
}
How can I help you explore Laravel packages today?