You are here

class OrderPaidSubscriber in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/payment/src/EventSubscriber/OrderPaidSubscriber.php \Drupal\commerce_payment\EventSubscriber\OrderPaidSubscriber
  2. 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
commerce_payment.order_paid_subscriber in modules/payment/commerce_payment.services.yml
Drupal\commerce_payment\EventSubscriber\OrderPaidSubscriber

File

modules/payment/src/EventSubscriber/OrderPaidSubscriber.php, line 9

Namespace

Drupal\commerce_payment\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
OrderPaidSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
OrderPaidSubscriber::onPaid public function Places the order after it has been fully paid through an off-site gateway.