You are here

class TwoCheckoutController in Ubercart 8.4

Controller routines for uc_2checkout.

Hierarchy

Expanded class hierarchy of TwoCheckoutController

File

payment/uc_2checkout/src/Controller/TwoCheckoutController.php, line 16

Namespace

Drupal\uc_2checkout\Controller
View source
class TwoCheckoutController extends ControllerBase {

  /**
   * The cart manager.
   *
   * @var \Drupal\uc_cart\CartManager
   */
  protected $cartManager;

  /**
   * Constructs a TwoCheckoutController.
   *
   * @param \Drupal\uc_cart\CartManagerInterface $cart_manager
   *   The cart manager.
   */
  public function __construct(CartManagerInterface $cart_manager) {
    $this->cartManager = $cart_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('uc_cart.manager'));
  }

  /**
   * Finalizes 2Checkout transaction.
   *
   * @param int $cart_id
   *   The cart identifier.
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request of the page.
   */
  public function complete($cart_id = 0, Request $request) {
    $this
      ->getLogger('uc_2checkout')
      ->notice('Receiving new order notification for order @order_id.', [
      '@order_id' => SafeMarkup::checkPlain($request->request
        ->get('merchant_order_id')),
    ]);
    $order = Order::load($request->request
      ->get('merchant_order_id'));
    if (!$order || $order
      ->getStateId() != 'in_checkout') {
      return [
        '#plain_text' => $this
          ->t('An error has occurred during payment. Please contact us to ensure your order has submitted.'),
      ];
    }
    $plugin = \Drupal::service('plugin.manager.uc_payment.method')
      ->createFromOrder($order);
    if ($plugin
      ->getPluginId() != '2checkout') {
      throw new AccessDeniedHttpException();
    }
    $configuration = $plugin
      ->getConfiguration();
    $key = $request->request
      ->get('key');
    $order_number = $configuration['demo'] ? 1 : $request->request
      ->get('order_number');
    $valid = md5($configuration['secret_word'] . $request->request
      ->get('sid') . $order_number . $request->request
      ->get('total'));
    if (mb_strtolower($key) != mb_strtolower($valid)) {
      uc_order_comment_save($order
        ->id(), 0, $this
        ->t('Attempted unverified 2Checkout completion for this order.'), 'admin');
      throw new AccessDeniedHttpException();
    }
    if ($request->request
      ->get('demo') == 'Y' xor $configuration['demo']) {
      $this
        ->getLogger('uc_2checkout')
        ->error('The 2Checkout payment for order <a href=":order_url">@order_id</a> demo flag was set to %flag, but the module is set to %mode mode.', [
        ':order_url' => $order
          ->toUrl()
          ->toString(),
        '@order_id' => $order
          ->id(),
        '%flag' => $request->request
          ->get('demo') == 'Y' ? 'Y' : 'N',
        '%mode' => $configuration['demo'] ? 'Y' : 'N',
      ]);
      if (!$configuration['demo']) {
        throw new AccessDeniedHttpException();
      }
    }
    $address = $order
      ->getAddress('billing');
    $address
      ->setStreet1($request->request
      ->get('street_address'));
    $address
      ->setStreet2($request->request
      ->get('street_address2'));
    $address
      ->setCity($request->request
      ->get('city'));
    $address
      ->setPostalCode($request->request
      ->get('zip'));
    $address
      ->setPhone($request->request
      ->get('phone'));
    $address
      ->setZone($request->request
      ->get('state'));
    $address
      ->setCountry($request->request
      ->get('country'));
    $order
      ->setAddress('billing', $address);
    $order
      ->save();
    if (mb_strtolower($request->request
      ->get('email')) !== mb_strtolower($order
      ->getEmail())) {
      uc_order_comment_save($order
        ->id(), 0, $this
        ->t('Customer used a different e-mail address during payment: @email', [
        '@email' => $request->request
          ->get('email'),
      ]), 'admin');
    }
    if ($request->request
      ->get('credit_card_processes') == 'Y' && is_numeric($request->request
      ->get('total'))) {
      $comment = $this
        ->t('Paid by @type, 2Checkout.com order #@order.', [
        '@type' => $request->request
          ->get('pay_method') == 'CC' ? $this
          ->t('credit card') : $this
          ->t('echeck'),
        '@order' => $request->request
          ->get('order_number'),
      ]);
      uc_payment_enter($order
        ->id(), '2checkout', $request->request
        ->get('total'), 0, NULL, $comment);
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Your order will be processed as soon as your payment clears at 2Checkout.com.'));
      uc_order_comment_save($order
        ->id(), 0, $this
        ->t('@type payment is pending approval at 2Checkout.com.', [
        '@type' => $request->request
          ->get('pay_method') == 'CC' ? $this
          ->t('Credit card') : $this
          ->t('eCheck'),
      ]), 'admin');
    }

    // Add a comment to let sales team know this came in through the site.
    uc_order_comment_save($order
      ->id(), 0, $this
      ->t('Order created through website.'), 'admin');
    return $this->cartManager
      ->completeSale($order);
  }

  /**
   * React on INS messages from 2Checkout.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request of the page.
   */
  public function notification(Request $request) {
    $values = $request->request;
    $this
      ->getLogger('uc_2checkout')
      ->notice('Received 2Checkout notification with following data: @data', [
      '@data' => print_r($values
        ->all(), TRUE),
    ]);
    if ($values
      ->has('message_type') && $values
      ->has('md5_hash') && $values
      ->has('message_id')) {
      $order_id = $values
        ->get('vendor_order_id');
      $order = Order::load($order_id);
      $plugin = \Drupal::service('plugin.manager.uc_payment.method')
        ->createFromOrder($order);
      $configuration = $plugin
        ->getConfiguration();

      // Validate the hash.
      $secret_word = $configuration['secret_word'];
      $sid = $configuration['sid'];
      $twocheckout_order_id = $values
        ->get('sale_id');
      $twocheckout_invoice_id = $values
        ->get('invoice_id');
      $hash = strtoupper(md5($twocheckout_order_id . $sid . $twocheckout_invoice_id . $secret_word));
      if ($hash != $values
        ->get('md5_hash')) {
        $this
          ->getLogger('uc_2checkout')
          ->notice('2Checkout notification #@num had a wrong hash.', [
          '@num' => $values
            ->get('message_id'),
        ]);
        die('Hash Incorrect');
      }
      if ($values
        ->get('message_type') == 'FRAUD_STATUS_CHANGED') {
        switch ($values
          ->get('fraud_status')) {

          // @todo I think this still needs a lot of work, I don't see anywhere
          // that it validates the INS against an order in the DB then changes
          // order status if the payment was successful, like PayPal IPN does.
          case 'pass':
            break;
          case 'wait':
            break;
          case 'fail':

            // @todo uc_order_update_status($order_id, uc_order_state_default('canceled'));
            $order
              ->setStatusId('canceled')
              ->save();
            uc_order_comment_save($order_id, 0, $this
              ->t('Order have not passed 2Checkout fraud review.'));
            die('fraud');
        }
      }
      elseif ($values
        ->get('message_type') == 'REFUND_ISSUED') {

        // @todo uc_order_update_status($order_id, uc_order_state_default('canceled'));
        $order
          ->setStatusId('canceled')
          ->save();
        uc_order_comment_save($order_id, 0, $this
          ->t('Order have been refunded through 2Checkout.'));
        die('refund');
      }
    }
    die('ok');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TwoCheckoutController::$cartManager protected property The cart manager.
TwoCheckoutController::complete public function Finalizes 2Checkout transaction.
TwoCheckoutController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
TwoCheckoutController::notification public function React on INS messages from 2Checkout.
TwoCheckoutController::__construct public function Constructs a TwoCheckoutController.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.