Skip to content

Commit

Permalink
NTR: Merge branch 'master' into users/vm/PISHPS-243-bound-saleschannel
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Mar 21, 2024
2 parents 3d3ae1f + edbe0dd commit ec7de09
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 15 deletions.
32 changes: 32 additions & 0 deletions src/Handler/Method/KlarnaOnePayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Kiener\MolliePayments\Handler\Method;

use Kiener\MolliePayments\Handler\PaymentHandler;
use Mollie\Api\Types\PaymentMethod;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\System\Locale\LocaleEntity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

class KlarnaOnePayment extends PaymentHandler
{
public const PAYMENT_METHOD_NAME = PaymentMethod::KLARNA_ONE;
public const PAYMENT_METHOD_DESCRIPTION = 'Klarna';

/** @var string */
protected $paymentMethod = self::PAYMENT_METHOD_NAME;


/**
* @param array<mixed> $orderData
* @param OrderEntity $orderEntity
* @param SalesChannelContext $salesChannelContext
* @param CustomerEntity $customer
* @return array<mixed>
*/
public function processPaymentMethodSpecificParameters(array $orderData, OrderEntity $orderEntity, SalesChannelContext $salesChannelContext, CustomerEntity $customer): array
{
return $orderData;
}
}
8 changes: 8 additions & 0 deletions src/Resources/config/services/handlers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@
<tag name="shopware.payment.method.async"/>
</service>

<!-- KlanraOne PaymentHandler-->
<service id="Kiener\MolliePayments\Handler\Method\KlarnaOnePayment">
<argument type="service" id="mollie_payments.logger"/>
<argument type="service" id="service_container"/>
<tag name="shopware.payment.method.async"/>
</service>



</services>
</container>
2 changes: 2 additions & 0 deletions src/Service/PaymentMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Kiener\MolliePayments\Handler\Method\In3Payment;
use Kiener\MolliePayments\Handler\Method\IngHomePayPayment;
use Kiener\MolliePayments\Handler\Method\KbcPayment;
use Kiener\MolliePayments\Handler\Method\KlarnaOnePayment;
use Kiener\MolliePayments\Handler\Method\KlarnaPayLaterPayment;
use Kiener\MolliePayments\Handler\Method\KlarnaPayNowPayment;
use Kiener\MolliePayments\Handler\Method\KlarnaSliceItPayment;
Expand Down Expand Up @@ -430,6 +431,7 @@ public function getPaymentHandlers(): array
KlarnaPayLaterPayment::class,
KlarnaPayNowPayment::class,
KlarnaSliceItPayment::class,
KlarnaOnePayment::class,
PayPalPayment::class,
PaySafeCardPayment::class,
Przelewy24Payment::class,
Expand Down
27 changes: 12 additions & 15 deletions src/Service/ShippingMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Kiener\MolliePayments\Service;

use Shopware\Core\Checkout\Shipping\SalesChannel\AbstractShippingMethodRoute;
use Shopware\Core\Checkout\Shipping\ShippingMethodCollection;
use Shopware\Core\Checkout\Shipping\ShippingMethodEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;

class ShippingMethodService
{
Expand All @@ -17,14 +17,20 @@ class ShippingMethodService
*/
private $shippingMethodRepository;

/**
* @var AbstractShippingMethodRoute
*/
private $shippingMethodRoute;

/**
* Creates a new instance of the shipping method repository.
*
* @param EntityRepository $shippingMethodRepository
*/
public function __construct($shippingMethodRepository)
public function __construct($shippingMethodRepository, AbstractShippingMethodRoute $shippingMethodRoute)
{
$this->shippingMethodRepository = $shippingMethodRepository;
$this->shippingMethodRoute = $shippingMethodRoute;
}

/**
Expand Down Expand Up @@ -52,18 +58,9 @@ public function getShippingMethodById(string $shippingMethodId, SalesChannelCont
*/
public function getActiveShippingMethods(SalesChannelContext $salesChannelContext): ShippingMethodCollection
{
$criteria = (new Criteria())
->addFilter(new EqualsFilter('active', true))
->addFilter(new EqualsFilter('salesChannels.id', $salesChannelContext->getSalesChannel()->getId()))
->addFilter(new EqualsAnyFilter('availabilityRuleId', $salesChannelContext->getRuleIds()))
->addAssociation('prices')
->addAssociation('salesChannels');

/** @var ShippingMethodCollection $shippingMethods */
$shippingMethods = $this->shippingMethodRepository
->search($criteria, $salesChannelContext->getContext())
->getEntities();
$request = new Request();
$request->query->set('onlyAvailable', '1');

return $shippingMethods->filterByActiveRules($salesChannelContext);
return $this->shippingMethodRoute->load($request, $salesChannelContext, new Criteria())->getShippingMethods();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);

namespace MolliePayments\Tests\Service\MollieApi\Builder\Payments;

use DateTime;
use DateTimeZone;
use Faker\Extension\Container;
use Kiener\MolliePayments\Handler\Method\KlarnaOnePayment;
use Kiener\MolliePayments\Handler\Method\KlarnaPayLaterPayment;
use Kiener\MolliePayments\Service\MollieApi\Builder\MollieOrderPriceBuilder;
use Mollie\Api\Types\PaymentMethod;
use MolliePayments\Tests\Fakes\FakeContainer;
use MolliePayments\Tests\Service\MollieApi\Builder\AbstractMollieOrderBuilder;
use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\Currency\CurrencyEntity;

class KlarnaOneOrderBuilderTest extends AbstractMollieOrderBuilder
{
public function testOrderBuild(): void
{
$redirectWebhookUrl = 'https://foo';
$this->router->method('generate')->willReturn($redirectWebhookUrl);
$paymentMethod = PaymentMethod::KLARNA_PAY_LATER;

$this->paymentHandler = new KlarnaOnePayment(
$this->loggerService,
new FakeContainer()
);

$transactionId = Uuid::randomHex();
$amountTotal = 27.0;
$taxStatus = CartPrice::TAX_STATE_GROSS;
$currencyISO = 'EUR';

$currency = new CurrencyEntity();
$currency->setId(Uuid::randomHex());
$currency->setIsoCode($currencyISO);

$orderNumber = 'foo number';
$lineItems = $this->getDummyLineItems();

$order = $this->getOrderEntity($amountTotal, $taxStatus, $currencyISO, $lineItems, $orderNumber);

$actual = $this->builder->buildOrderPayload($order, $transactionId, $paymentMethod, $this->salesChannelContext, $this->paymentHandler, []);

$expectedOrderLifeTime = (new DateTime())->setTimezone(new DateTimeZone('UTC'))
->modify(sprintf('+%d day', $this->expiresAt))
->format('Y-m-d');

$expected = [
'amount' => (new MollieOrderPriceBuilder())->build($amountTotal, $currencyISO),
'locale' => $this->localeCode,
'method' => $paymentMethod,
'orderNumber' => $orderNumber,
'payment' => ['webhookUrl' => $redirectWebhookUrl],
'redirectUrl' => $redirectWebhookUrl,
'webhookUrl' => $redirectWebhookUrl,
'lines' => $this->getExpectedLineItems($taxStatus, $lineItems, $currency),
'billingAddress' => $this->getExpectedTestAddress($this->address, $this->email),
'shippingAddress' => $this->getExpectedTestAddress($this->address, $this->email),
'expiresAt' => $expectedOrderLifeTime
];

self::assertSame($expected, $actual);
}
}
2 changes: 2 additions & 0 deletions tests/PHPUnit/Service/PaymentMethodServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Kiener\MolliePayments\Handler\Method\iDealPayment;
use Kiener\MolliePayments\Handler\Method\In3Payment;
use Kiener\MolliePayments\Handler\Method\KbcPayment;
use Kiener\MolliePayments\Handler\Method\KlarnaOnePayment;
use Kiener\MolliePayments\Handler\Method\KlarnaPayLaterPayment;
use Kiener\MolliePayments\Handler\Method\KlarnaPayNowPayment;
use Kiener\MolliePayments\Handler\Method\KlarnaSliceItPayment;
Expand Down Expand Up @@ -120,6 +121,7 @@ public function testSupportedMethods(): void
KlarnaPayLaterPayment::class,
KlarnaPayNowPayment::class,
KlarnaSliceItPayment::class,
KlarnaOnePayment::class,
PayPalPayment::class,
PaySafeCardPayment::class,
Przelewy24Payment::class,
Expand Down

0 comments on commit ec7de09

Please sign in to comment.