Commit ab04cb6e authored by Ivan Ruzak's avatar Ivan Ruzak Committed by Jakub Piasecki
Browse files

Issue #3303055 by teodorakis: Implement validation callback handling

parent 010e6d86
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -17,6 +17,10 @@ services:
    tags:
      - { name: event_subscriber }

  commerce_svea.validation_handler:
    class: Drupal\commerce_svea\SveaValidationHandler
    arguments: ['@entity_type.manager', '@event_dispatcher', '@keyvalue']

  logger.channel.commerce_svea:
    class: Drupal\Core\Logger\LoggerChannel
    factory: logger.factory:get
+3 −0
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@ commerce_payment.commerce_payment_gateway.plugin.svea_checkout:
    terms_path:
      type: path
      label: 'Terms path'
    enable_order_validation:
      type: boolean
      label: 'Enable order validation'
    update_billing_profile:
      type: boolean
      label: 'Update billing profile'
+9 −0
Original line number Diff line number Diff line
@@ -25,4 +25,13 @@ final class SveaCheckoutEvents {
   */
  const UPDATE_ORDER_REQUEST = 'commerce_svea.update_order_request';

  /**
   * Name of the event fired when Svea sending callback validation request.
   *
   * @Event
   *
   * @see \Drupal\commerce_svea\Event\SveaOrderEvent
   */
  const ORDER_VALIDATION = 'commerce_svea.order_validation';

}
+42 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_svea\Event;

use Drupal\commerce_order\Entity\OrderInterface;
use Symfony\Component\EventDispatcher\Event;

/**
 * Represents a Svea order event.
 *
 * @see \Drupal\commerce_svea\Event\SveaCheckoutEvents
 */
class SveaOrderEvent extends Event {

  /**
   * The order.
   *
   * @var \Drupal\commerce_order\Entity\OrderInterface
   */
  protected $order;

  /**
   * Constructs a new SveaOrderEvent object.
   *
   * @param \Drupal\commerce_order\Entity\OrderInterface $order
   *   The order.
   */
  public function __construct(OrderInterface $order) {
    $this->order = $order;
  }

  /**
   * Gets the commerce order.
   *
   * @return \Drupal\commerce_order\Entity\OrderInterface
   *   The commerce order entity.
   */
  public function getOrder(): OrderInterface {
    return $this->order;
  }

}
+43 −4
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OffsitePaymentGateway
use Drupal\commerce_price\Calculator;
use Drupal\commerce_price\Price;
use Drupal\commerce_svea\SveaManagerFactoryInterface;
use Drupal\commerce_svea\SveaValidationHandlerInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
@@ -64,6 +65,13 @@ class SveaCheckout extends OffsitePaymentGatewayBase implements SveaCheckoutInte
   */
  protected $eventDispatcher;

  /**
   * The Svea validation handler.
   *
   * @var \Drupal\commerce_svea\SveaValidationHandlerInterface
   */
  protected $sveaValidationHandler;

  /**
   * Constructs a new SveaCheckout object.
   *
@@ -87,6 +95,8 @@ class SveaCheckout extends OffsitePaymentGatewayBase implements SveaCheckoutInte
   *   The logger.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\commerce_svea\SveaValidationHandlerInterface $svea_validation_handler
   *   The Svea validation handler.
   */
  public function __construct(
    array $configuration,
@@ -98,12 +108,14 @@ class SveaCheckout extends OffsitePaymentGatewayBase implements SveaCheckoutInte
    TimeInterface $time,
    SveaManagerFactoryInterface $svea_manager_factory,
    LoggerInterface $logger,
    ModuleHandlerInterface $module_handler
    ModuleHandlerInterface $module_handler,
    SveaValidationHandlerInterface $svea_validation_handler
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $payment_type_manager, $payment_method_type_manager, $time);
    $this->sveaManagerFactory = $svea_manager_factory;
    $this->logger = $logger;
    $this->moduleHandler = $module_handler;
    $this->sveaValidationHandler = $svea_validation_handler;
  }

  /**
@@ -120,7 +132,8 @@ class SveaCheckout extends OffsitePaymentGatewayBase implements SveaCheckoutInte
      $container->get('datetime.time'),
      $container->get('commerce_svea.manager_factory'),
      $container->get('logger.channel.commerce_svea'),
      $container->get('module_handler')
      $container->get('module_handler'),
      $container->get('commerce_svea.validation_handler')
    );
  }

@@ -134,6 +147,7 @@ class SveaCheckout extends OffsitePaymentGatewayBase implements SveaCheckoutInte
      'purchase_country' => '',
      'locale' => 'sv-SE',
      'terms_path' => '',
      'enable_order_validation' => TRUE,
      'update_billing_profile' => TRUE,
      'update_shipping_profile' => FALSE,
    ] + parent::defaultConfiguration();
@@ -190,6 +204,11 @@ class SveaCheckout extends OffsitePaymentGatewayBase implements SveaCheckoutInte
      '#default_value' => $this->configuration['terms_path'],
      '#required' => TRUE,
    ];
    $form['enable_order_validation'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Validate the order before it is completed'),
      '#default_value' => $this->configuration['enable_order_validation'],
    ];
    $form['update_billing_profile'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Update the billing customer profile with address information the customer enters at Svea.'),
@@ -217,6 +236,7 @@ class SveaCheckout extends OffsitePaymentGatewayBase implements SveaCheckoutInte
      $this->configuration['purchase_country'] = $values['purchase_country'];
      $this->configuration['locale'] = $values['locale'];
      $this->configuration['terms_path'] = $values['terms_path'];
      $this->configuration['enable_order_validation'] = $values['enable_order_validation'];
      $this->configuration['update_billing_profile'] = $values['update_billing_profile'];
      $this->configuration['update_shipping_profile'] = $values['update_shipping_profile'];
    }
@@ -228,11 +248,30 @@ class SveaCheckout extends OffsitePaymentGatewayBase implements SveaCheckoutInte
  public function getNotifyUrl(): Url {
    $notify_url = parent::getNotifyUrl();
    $notify_url->setOption('query', [
      'checkout_order_id' => '{checkout.order.uri}',
      'svea_order_id' => '{checkout.order.uri}',
    ]);
    return $notify_url;
  }

  /**
   * {@inheritdoc}
   */
  public function getValidationUrl(): Url {
    $validation_url = self::getNotifyUrl();
    $validation_url->mergeOptions(['query' => ['callback' => 'validation']]);
    return $validation_url;
  }

  /**
   * {@inheritdoc}
   */
  public function onNotify(Request $request) {
    if ($request->query->get('callback') == 'validation') {
      return $this->sveaValidationHandler->validateOrder($request);
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
@@ -323,7 +362,7 @@ class SveaCheckout extends OffsitePaymentGatewayBase implements SveaCheckoutInte
   */
  protected function populateProfile(ProfileInterface $profile, array $address) {
    $mapping = [
      // For B2B customers "fullName" field contains the company name.
      // For B2B customers "FullName" field contains the company name.
      'FullName' => 'organization',
      'FirstName' => 'given_name',
      'LastName' => 'family_name',
Loading