You are here

public function WorldpayRedirect::onNotify in Commerce Worldpay 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Commerce/PaymentGateway/WorldpayRedirect.php \Drupal\commerce_worldpay\Plugin\Commerce\PaymentGateway\WorldpayRedirect::onNotify()

Throws

\InvalidArgumentException

\Drupal\Core\Entity\EntityStorageException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

Overrides OffsitePaymentGatewayBase::onNotify

File

src/Plugin/Commerce/PaymentGateway/WorldpayRedirect.php, line 541

Class

WorldpayRedirect
Provides the Worldpay Redirect payment gateway.

Namespace

Drupal\commerce_worldpay\Plugin\Commerce\PaymentGateway

Code

public function onNotify(Request $request) {
  $content = $request
    ->getMethod() === 'POST' ? $request
    ->getContent() : FALSE;
  if (!$content) {
    $this->logger
      ->error('There is no response was received');
    throw new PaymentGatewayException();
  }

  // Hook to allow other modules to access the POST content from the WorldPay Payment response.
  $this->moduleHandler
    ->invokeAll('commerce_worldpay_payment_response', [
    $request,
  ]);
  if ($this->configuration['debug'] === 'log') {

    // Just development debug.
    $this->logger
      ->debug('<pre>' . $content . '</pre>');
    $this->logger
      ->debug('Transaction ID %transID Order ID %orderID', [
      '%transID' => $request->request
        ->get('transId'),
      '%orderID' => $request->request
        ->get('MC_orderId'),
    ]);
  }

  // Get and check the VendorTxCode.
  $txCode = $request->request
    ->get('transId') !== NULL ? $request->request
    ->get('transId') : FALSE;
  if (empty($txCode) || empty($request->request
    ->get('MC_orderId'))) {
    $this->logger
      ->error('No Transaction code have been returned.');
    throw new PaymentGatewayException();
  }
  $order = $this->entityTypeManager
    ->getStorage('commerce_order')
    ->load($request->request
    ->get('MC_orderId'));
  $build = [];
  if ($order instanceof OrderInterface && $request->request
    ->get('transStatus') === 'Y') {
    $payment = $this
      ->createPayment($request->request
      ->all(), $order);
    $payment->state = 'capture_completed';
    $payment
      ->save();
    $logLevel = 'info';
    $logMessage = 'OK Payment callback received from WorldPay for order %order_id with status code %transID';
    $logContext = [
      '%order_id' => $order
        ->id(),
      '%transID' => $request->request
        ->get('transId'),
    ];
    $this->logger
      ->log($logLevel, $logMessage, $logContext);
    $build += [
      '#theme' => 'commerce_worldpay_success',
      '#transaction_id' => $request->request
        ->get('transId'),
      '#order_id' => $order
        ->id(),
      '#return_url' => $this
        ->buildReturnUrl($order),
      '#cache' => [
        'max-age' => 0,
      ],
    ];
  }
  if ($order instanceof OrderInterface && $request->request
    ->get('transStatus') === 'C') {
    $logLevel = 'info';
    $logMessage = 'Cancel Payment callback received from WorldPay for order %order_id with status code %transID';
    $logContext = [
      '%order_id' => $order
        ->id(),
      '%transID' => $request->request
        ->get('transId'),
    ];
    $this->logger
      ->log($logLevel, $logMessage, $logContext);
    $build += [
      '#theme' => 'commerce_worldpay_cancel',
      '#transaction_id' => $request->request
        ->get('transId'),
      '#order_id' => $order
        ->id(),
      '#return_url' => $this
        ->buildCancelUrl($order),
      '#cache' => [
        'max-age' => 0,
      ],
    ];
  }
  $output = \Drupal::service('renderer')
    ->renderRoot($build);
  $response = new Response();
  $response
    ->setStatusCode(200);
  $response
    ->setContent($output);
  $response->headers
    ->set('Content-Type', 'text/html');
  return $response;
}