You are here

public function WorldpayRedirect::buildFormData in Commerce Worldpay 8

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

Throws

\Drupal\Core\TypedData\Exception\MissingDataException

\Drupal\Core\Entity\EntityStorageException

Overrides WorldpayRedirectInterface::buildFormData

File

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

Class

WorldpayRedirect
Provides the Worldpay Redirect payment gateway.

Namespace

Drupal\commerce_worldpay\Plugin\Commerce\PaymentGateway

Code

public function buildFormData(PaymentInterface $payment) {

  /** @var OrderInterface $order */
  $order = $payment
    ->getOrder();
  $worldPayFormApi = $this
    ->getWorldPayApi($order);
  try {
    $worldPayFormApi
      ->addAddress($this
      ->getBillingAddress($order));
  } catch (MissingDataException $exception) {
    $this->logger
      ->error($exception
      ->getMessage());
    return FALSE;
  }
  if ($this->moduleHandler
    ->moduleExists('commerce_shipping') && $order
    ->hasField('shipments')) {

    /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
    $shipments = $order
      ->get('shipments')
      ->referencedEntities();
    if (!empty($shipments) && ($shippingAddress = $this
      ->getShippingAddress(reset($shipments)))) {
      $worldPayFormApi
        ->addShipmentAddress($shippingAddress);
    }
  }
  $data = $worldPayFormApi
    ->createData();
  $order
    ->setData('worldpay_form', [
    'request' => $data,
    'return_url' => $this
      ->getNotifyUrl()
      ->toString(),
  ]);
  $order
    ->save();
  return $data;
}