You are here

public function Sermepa::onReturn in Commerce sermepa 8.2

Processes the "return" request.

This method should only be concerned with creating/completing payments, the parent order does not need to be touched. The order state is updated automatically when the order is paid in full, or manually by the merchant (via the admin UI).

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

\Symfony\Component\HttpFoundation\Request $request: The request.

Throws

\Drupal\commerce_payment\Exception\PaymentGatewayException Thrown when the request is invalid or the payment failed.

Overrides OffsitePaymentGatewayBase::onReturn

File

src/Plugin/Commerce/PaymentGateway/Sermepa.php, line 258

Class

Sermepa
Provides the Sermepa/Redsýs payment gateway.

Namespace

Drupal\commerce_sermepa\Plugin\Commerce\PaymentGateway

Code

public function onReturn(OrderInterface $order, Request $request) {

  // Do not process the notification if the payment is being processed.

  /* @see \Drupal\commerce_sermepa\Plugin\Commerce\PaymentGateway\Sermepa::onNotify() */
  if ($this->lock
    ->lockMayBeAvailable($this
    ->getLockName($order))) {
    $this
      ->processRequest($request, $order);
  }
  else {

    // Wait for onNotify request that is doing this work, this occurs on
    // asynchronous calls, when  onNotify and onReturn can collide.
    $this->lock
      ->wait($this
      ->getLockName($order));
  }

  // We could have an outdated order, just reload it and check the states.
  // @TODO Change this when #3043180 is fixed.

  /* @see https://www.drupal.org/project/commerce/issues/3043180 */
  $order_storage = $this->entityTypeManager
    ->getStorage('commerce_order');
  $updated_order = $order_storage
    ->loadUnchanged($order
    ->id());

  // If we have different states is because the payment has been validated
  // on the onNotify method and we need to force the redirection to the next
  // step or it the order will be placed twice.
  if ($updated_order
    ->getState()
    ->getId() != $order
    ->getState()
    ->getId()) {

    // Get the current checkout step and calculate the next step.
    $step_id = $this->currentRouteMatch
      ->getParameter('step');

    /** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $checkout_flow */
    $checkout_flow = $order
      ->get('checkout_flow')
      ->first()
      ->get('entity')
      ->getTarget()
      ->getValue();
    $checkout_flow_plugin = $checkout_flow
      ->getPlugin();
    $redirect_step_id = $checkout_flow_plugin
      ->getNextStepId($step_id);
    throw new NeedsRedirectException(Url::fromRoute('commerce_checkout.form', [
      'commerce_order' => $updated_order
        ->id(),
      'step' => $redirect_step_id,
    ])
      ->toString());
  }
  $this
    ->messenger()
    ->addStatus($this
    ->t('Your payment has been completed successfully.'));
}