class OrderPaidSubscriber in Commerce Core 8.2
Same name in this branch
- 8.2 modules/payment/src/EventSubscriber/OrderPaidSubscriber.php \Drupal\commerce_payment\EventSubscriber\OrderPaidSubscriber
- 8.2 modules/order/tests/modules/commerce_order_test/src/EventSubscriber/OrderPaidSubscriber.php \Drupal\commerce_order_test\EventSubscriber\OrderPaidSubscriber
Hierarchy
- class \Drupal\commerce_payment\EventSubscriber\OrderPaidSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of OrderPaidSubscriber
1 string reference to 'OrderPaidSubscriber'
- commerce_payment.services.yml in modules/
payment/ commerce_payment.services.yml - modules/payment/commerce_payment.services.yml
1 service uses OrderPaidSubscriber
File
- modules/
payment/ src/ EventSubscriber/ OrderPaidSubscriber.php, line 9
Namespace
Drupal\commerce_payment\EventSubscriberView source
class OrderPaidSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events = [
'commerce_order.order.paid' => 'onPaid',
];
return $events;
}
/**
* Places the order after it has been fully paid through an off-site gateway.
*
* Off-site payments can only be made at checkout.
* If the gateway supports notifications, these two scenarios are possible:
*
* 1) The onNotify() method is called before the customer returns to the
* site. A payment is created, the order is now considered fully paid,
* causing the "payment" step to no longer be visible, sending the
* customer back to the first checkout step.
* 2) The customer never returns to the site. The onNotify() method completed
* the payment, but the order is still unplaced and stuck in checkout.
*
* To avoid both problems, this subscriber ensures that the order is placed,
* which also ensures that the customer is sent to the checkout complete
* page once they (eventually) return.
*
* @param \Drupal\commerce_order\Event\OrderEvent $event
* The event.
*/
public function onPaid(OrderEvent $event) {
$order = $event
->getOrder();
if ($order
->getState()
->getId() != 'draft') {
// The order has already been placed.
return;
}
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway = $order
->get('payment_gateway')->entity;
if (!$payment_gateway) {
// The payment gateway is unknown.
return;
}
if ($payment_gateway
->getPlugin() instanceof OffsitePaymentGatewayInterface) {
$order
->getState()
->applyTransitionById('place');
// A placed order should never be locked.
$order
->unlock();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OrderPaidSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
OrderPaidSubscriber:: |
public | function | Places the order after it has been fully paid through an off-site gateway. |