You are here

public function TwoCheckoutController::notification in Ubercart 8.4

React on INS messages from 2Checkout.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request of the page.

1 string reference to 'TwoCheckoutController::notification'
uc_2checkout.routing.yml in payment/uc_2checkout/uc_2checkout.routing.yml
payment/uc_2checkout/uc_2checkout.routing.yml

File

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

Class

TwoCheckoutController
Controller routines for uc_2checkout.

Namespace

Drupal\uc_2checkout\Controller

Code

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');
}