You are here

class WorldPayHelper in Commerce Worldpay 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Commerce/PaymentGateway/WorldPayHelper.php \Drupal\commerce_worldpay\Plugin\Commerce\PaymentGateway\WorldPayHelper

Class WorldPayHelper Helper class for collecting form data.

@package Drupal\commerce_worldpay\Plugin\Commerce\PaymentGateway

Hierarchy

  • class \Drupal\commerce_worldpay\Plugin\Commerce\PaymentGateway\WorldPayHelper

Expanded class hierarchy of WorldPayHelper

File

src/Plugin/Commerce/PaymentGateway/WorldPayHelper.php, line 16

Namespace

Drupal\commerce_worldpay\Plugin\Commerce\PaymentGateway
View source
class WorldPayHelper {

  /**
   * @var \Drupal\commerce_order\Entity\OrderInterface
   */
  private $order;

  /**
   * @var array
   */
  private $config;

  /**
   * @var array
   */
  private $data = [];

  /**
   * WorldPayHelper constructor.
   *
   * @param \Drupal\commerce_order\Entity\OrderInterface $order
   * @param $configuration
   */
  public function __construct(OrderInterface $order, $configuration) {
    $this->order = $order;
    $this->config = $configuration;
  }

  /**
   * @param array $addressData
   *
   * @throws \Drupal\Core\TypedData\Exception\MissingDataException
   */
  public function addShipmentAddress(array $addressData) {
    if (NULL === $addressData) {
      throw new MissingDataException('There is no address data provided!');
    }
    $address = $addressData['DeliveryAddress1'];
    $address .= PHP_EOL;
    $address .= $addressData['DeliveryAddress2'];
    $address .= PHP_EOL;
    $address .= $addressData['DeliveryCity'];
    $this->data += [
      'name' => $addressData['DeliveryFirstname'] . ' ' . $addressData['DeliverySurname'],
      'address' => $address,
      'postcode' => $addressData['DeliveryPostCode'],
      'country' => $addressData['DeliveryCountry'],
      'countryString' => $addressData['DeliveryCountryString'],
    ];
  }

  /**
   * @param array $addressData
   *
   * @throws \Drupal\Core\TypedData\Exception\MissingDataException
   */
  public function addAddress(array $addressData) {
    if (NULL === $addressData) {
      throw new MissingDataException('There is no address data provided!');
    }
    $address = $addressData['address1'];
    $address .= PHP_EOL;
    $address .= $addressData['address2'];
    $address .= PHP_EOL;
    $address .= $addressData['city'];
    $this->data += [
      'name' => $addressData['first_name'] . ' ' . $addressData['surname'],
      'address' => $address,
      'postcode' => $addressData['postCode'],
      'country' => $addressData['countryCode'],
      'countryString' => $addressData['country'],
      'email' => $addressData['email'],
    ];
  }

  /**
   * @return array
   */
  public function createData() {
    if ($this->config['payment_parameters']['test_mode']) {
      $this->data += [
        'testMode' => '100',
      ];
    }
    $this->data += [
      'instId' => $this->config['installation_id'],
      'amount' => $this->order
        ->getTotalPrice()
        ->getNumber(),
      'cartId' => $this->order
        ->uuid(),
      //This is need to clarify.
      'currency' => $this->order
        ->getTotalPrice()
        ->getCurrencyCode(),
      'MC_orderId' => $this->order
        ->id(),
      'M_http_host' => \Drupal::request()
        ->getSchemeAndHttpHost(),
      'signatureFields' => implode(':', WorldpayRedirect::md5signatureFields()),
      'signature' => $this
        ->buildMd5Hash([
        $this->config['installation_id'],
        $this->order
          ->getTotalPrice()
          ->getNumber(),
        $this->order
          ->getTotalPrice()
          ->getCurrencyCode(),
        $this->order
          ->uuid(),
        $this->order
          ->id(),
        $this
          ->getNotifyUrl(),
      ]),
      // The path WorldPay should send its Payment Response to.
      'MC_callback' => $this
        ->getNotifyUrl(),
      // Used in WorldPay custom pages.
      'C_siteTitle' => \Drupal::config('system.site')
        ->get('name'),
    ];
    return $this->data;
  }

  /**
   * Helper function for hashing of signature fields.
   *
   * @param array $signature_fields_values
   *
   * @return string
   */
  public function buildMd5Hash(array $signature_fields_values) {
    return md5($this->config['payment_security']['md5_salt'] . ':' . implode(':', $signature_fields_values));
  }

  /**
   * Provide a notification url.
   *
   * @return string
   *   Notification url.
   */
  public function getNotifyUrl() {
    return Url::fromRoute('commerce_payment.notify', [
      'commerce_payment_gateway' => $this->order
        ->get('payment_gateway')
        ->getString(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WorldPayHelper::$config private property
WorldPayHelper::$data private property
WorldPayHelper::$order private property
WorldPayHelper::addAddress public function
WorldPayHelper::addShipmentAddress public function
WorldPayHelper::buildMd5Hash public function Helper function for hashing of signature fields.
WorldPayHelper::createData public function
WorldPayHelper::getNotifyUrl public function Provide a notification url.
WorldPayHelper::__construct public function WorldPayHelper constructor.