Hi,

If you checkout with a different shipping and billing address, the shipping address is set to the billing address in the 'Klarna merchant Portal. The different addresses are reported correctly in Drupal Commerce.

Thanks for a great module,
Thomas

Comments

vanlindholm created an issue. See original summary.

tuutti’s picture

Commerce shipping is not supported at the moment, so it's probably Klarna copying the billing address by default.

You can do something like this:

yourmodule.services.yml:

services:
  yourmodule.klarna_session_subscriber:
    class: Drupal\yourmodule\EventSubscriber\Commerce\Payment\Klarna\SessionSubscriber
    tags:
      - { name: event_subscriber }

src/EventSubscriber/SessionSubscriber.php:

<?php

namespace Drupal\yourmodule\EventSubscriber\Commerce\Payment\Klarna;

use Drupal\commerce_klarna_payments\Event\RequestEvent;
use Drupal\commerce_klarna_payments\Klarna\Request\Address;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\address\AddressInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Gathers the session data for Klarna order.
 */
final class SessionSubscriber implements EventSubscriberInterface {

  private function getShippingAddress(OrderInterface $order) : AddressInterface {
    // Replace this with your custom fetching logic.
  }

  /**
   * Responds to RequestEvent.
   *
   * @param \Drupal\commerce_klarna_payments\Event\RequestEvent $event
   *   The event to respond to.
   */
  public function onSessionChange(RequestEvent $event) : void {
    $order = $event->getOrder();
    /** @var \Drupal\commerce_klarna_payments\Klarna\Request\Payment\Request $request */
    $request = $event->getRequest();

      try {
        $shippingProfile = $this->getShippingAddress($order);

        $shippingAddress = (new Address())
          ->setCity($shippingProfile->getLocality())
          ->setEmail($order->getEmail())
          ->setGivenName($shippingProfile->getGivenName())
          ->setCountry($shippingProfile->getCountryCode())
          ->setFamilyName($shippingProfile->getFamilyName())
          ->setStreetAddress($shippingProfile->getAddressLine1())
          ->setPostalCode($shippingProfile->getPostalCode());

        $request->setShippingAddress($shippingAddress);
      }
      catch (\InvalidArgumentException $e) {
      }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    // Both of these must be run after commerce_klarna_payment module.
    $events['commerce_klarna_payments.session_create'] = ['onSessionChange', -100];
    $events['commerce_klarna_payments.order_create'] = ['onSessionChange', -100];
    return $events;
  }

}
agoradesign’s picture

Supporting the shipping address is part of my proposed patch in #3013645-2: Respect the VAT on shipping fee order items

vanlindholm’s picture

Thank you both! I've applied the patch from agoradesign and it all appears to work as expected.

tuutti’s picture

tuutti’s picture

Status: Active » Fixed

This should be fixed in 2.x.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.