Unverified Commit 0023d61b authored by tuutti's avatar tuutti Committed by GitHub
Browse files

Merge pull request #45 from tuutti/3316739

Issue #3316739: Support commerce shipping
parents 5e892e33 777307a6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ jobs:
        run: echo "DRUPAL_ROOT=$HOME/drupal" >> $GITHUB_ENV

      - name: Clone drupal
        run: git clone --depth 1 --branch "$DRUPAL_CORE_VERSION" http://git.drupal.org/project/drupal.git/ $DRUPAL_ROOT
        run: git clone --depth 1 --branch "$DRUPAL_CORE_VERSION" https://git.drupal.org/project/drupal.git/ $DRUPAL_ROOT

      - name: Install required composer dependencies
        run: |
+1 −1
Original line number Diff line number Diff line
@@ -6,4 +6,4 @@ package: Commerce (contrib)
dependencies:
  - commerce:commerce_payment
  - commerce:commerce_price
  - system (>=9.1.0)
  - system (>=9.3.0)
+3 −0
Original line number Diff line number Diff line
@@ -7,5 +7,8 @@
        "php": ">=8.0",
        "drupal/commerce": "^2",
        "tuutti/php-paytrail-payment-api": "^1.4 || ^2.0"
    },
    "require-dev": {
        "drupal/commerce_shipping": "*"
    }
}
+76 −0
Original line number Diff line number Diff line
<?php

declare(strict_types = 1);

namespace Drupal\commerce_paytrail\Commerce\Shipping;

use Drupal\commerce_paytrail\Event\ModelEvent;
use Drupal\commerce_price\Calculator;
use Drupal\commerce_price\MinorUnitsConverterInterface;
use Paytrail\Payment\Model\Item;
use Paytrail\Payment\Model\PaymentRequest;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Provides a shipping support.
 */
final class ShippingEventSubscriber implements EventSubscriberInterface {

  /**
   * Constructs a new instance.
   *
   * @param \Drupal\commerce_price\MinorUnitsConverterInterface $converter
   *   The minor unit converter.
   */
  public function __construct(
    private MinorUnitsConverterInterface $converter
  ) {
  }

  /**
   * Subscribes to model event.
   *
   * @param \Drupal\commerce_paytrail\Event\ModelEvent $event
   *   The event to subscribe to.
   */
  public function addShipping(ModelEvent $event) : void {
    if (!$event->model instanceof PaymentRequest || !$order = $event->order) {
      return;
    }

    if (!$order->hasField('shipments') || $order->get('shipments')->isEmpty()) {
      return;
    }
    /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
    $shipments = $order->get('shipments')->referencedEntities();

    $items = $event->model->getItems();

    foreach ($shipments as $shipment) {
      $item = (new Item())
        ->setUnitPrice($this->converter->toMinorUnits($shipment->getAdjustedAmount()))
        ->setProductCode($shipment->getShippingMethod()->getPlugin()->getPluginId())
        ->setUnits(1)
        ->setVatPercentage(0);

      if ($taxes = $shipment->getAdjustments(['tax'])) {
        $item->setVatPercentage((int) Calculator::multiply(
          reset($taxes)->getPercentage(),
          '100'
        ));
      }
      $items[] = $item;
    }
    $event->model->setItems($items);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    return [
      ModelEvent::class => ['addShipping'],
    ];
  }

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

declare(strict_types = 1);

namespace Drupal\commerce_paytrail;

use Drupal\commerce_paytrail\Commerce\Shipping\ShippingEventSubscriber;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Registers services for non-required modules.
 */
class CommercePaytrailServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritdoc}
   */
  public function register(ContainerBuilder $container) :void {
    // We cannot use the module handler as the container is not yet compiled.
    // @see \Drupal\Core\DrupalKernel::compileContainer()
    $modules = $container->getParameter('container.modules');

    if (isset($modules['commerce_shipping'])) {
      $container->register('commerce_paytrail.shipping_subscriber', ShippingEventSubscriber::class)
        ->addTag('event_subscriber')
        ->addArgument(new Reference('commerce_price.minor_units_converter'));
    }
  }

}
Loading