Skip to content

Commit

Permalink
MOL-1250: Add TWINT payment method
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Sep 25, 2023
1 parent 72c3e1b commit c8da384
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 10 deletions.
32 changes: 32 additions & 0 deletions src/Handler/Method/TwintPayment.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 TwintPayment extends PaymentHandler
{
public const PAYMENT_METHOD_NAME = 'twint';
public const PAYMENT_METHOD_DESCRIPTION = 'TWINT';

/** @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;
}
}
7 changes: 7 additions & 0 deletions src/Resources/config/services/handlers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@
<tag name="shopware.payment.method.async"/>
</service>

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

<!-- Sofort PaymentHandler -->
<service id="Kiener\MolliePayments\Handler\Method\SofortPayment" class="Kiener\MolliePayments\Handler\Method\SofortPayment">
<argument type="service" id="mollie_payments.logger"/>
Expand Down
21 changes: 13 additions & 8 deletions src/Service/PaymentMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Kiener\MolliePayments\Handler\Method\PosPayment;
use Kiener\MolliePayments\Handler\Method\Przelewy24Payment;
use Kiener\MolliePayments\Handler\Method\SofortPayment;
use Kiener\MolliePayments\Handler\Method\TwintPayment;
use Kiener\MolliePayments\Handler\Method\VoucherPayment;
use Kiener\MolliePayments\MolliePayments;
use Kiener\MolliePayments\Repository\Media\MediaRepository;
Expand All @@ -44,23 +45,29 @@

class PaymentMethodService
{
/** @var MediaService */
/**
* @var MediaService
*/
private $mediaService;

/**
* @var PaymentMethodRepositoryInterface
*/
private $paymentRepository;

/** @var PluginIdProvider */
/**
* @var PluginIdProvider
*/
private $pluginIdProvider;

/**
* @var MediaRepositoryInterface
*/
private $mediaRepository;

/** @var HttpClientInterface */
/**
* @var HttpClientInterface
*/
private $httpClient;


Expand Down Expand Up @@ -267,11 +274,8 @@ public function disablePaymentMethod(string $handlerName, Context $context): voi
*
* @return EntityWrittenContainerEvent
*/
public function setPaymentMethodActivated(
string $paymentMethodId,
bool $active,
Context $context
): EntityWrittenContainerEvent {
public function setPaymentMethodActivated(string $paymentMethodId, bool $active, Context $context): EntityWrittenContainerEvent
{
return $this->paymentRepository->upsert(
[
[
Expand Down Expand Up @@ -385,6 +389,7 @@ public function getPaymentHandlers(): array
VoucherPayment::class,
In3Payment::class,
PosPayment::class,
TwintPayment::class,
// IngHomePayPayment::class, // not allowed anymore
// DirectDebitPayment::class, // only allowed when updating subsriptions, aka => not allowed anymore
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const device = devices.getFirstDevice();


const payments = [
{caseId: 'C1617016', key: 'twint', name: 'TWINT'},
{caseId: 'C4101', key: 'credit-card', name: 'Card'},
{caseId: 'C4111', key: 'paypal', name: 'PayPal'},
{caseId: 'C466903', key: 'billie', name: 'Billie'},
Expand Down
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\KlarnaPayLaterPayment;
use Kiener\MolliePayments\Handler\Method\TwintPayment;
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 TwintOrderBuilderTest extends AbstractMollieOrderBuilder
{
public function testOrderBuild(): void
{
$redirectWebhookUrl = 'https://foo';
$this->router->method('generate')->willReturn($redirectWebhookUrl);
$paymentMethod = TwintPayment::PAYMENT_METHOD_NAME;

$this->paymentHandler = new KlarnaPayLaterPayment(
$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);
}
}
5 changes: 3 additions & 2 deletions tests/PHPUnit/Service/PaymentMethodServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Kiener\MolliePayments\Handler\Method\PosPayment;
use Kiener\MolliePayments\Handler\Method\Przelewy24Payment;
use Kiener\MolliePayments\Handler\Method\SofortPayment;
use Kiener\MolliePayments\Handler\Method\TwintPayment;
use Kiener\MolliePayments\Handler\Method\VoucherPayment;
use Kiener\MolliePayments\Repository\Media\MediaRepositoryInterface;
use Kiener\MolliePayments\Repository\PaymentMethod\PaymentMethodRepositoryInterface;
Expand Down Expand Up @@ -70,8 +71,7 @@ protected function setUp(): void
$paymentMethod->setId('id-123');
$paymentMethod->setHandlerIdentifier('handler-id-123');

$this->context = $this->createMock(Context::class);
;
$this->context = $this->createMock(Context::class);;
$this->mediaRepository = new FakeMediaRepository(new MediaDefinition());
$this->paymentMethodRepository = new FakePaymentMethodRepository($paymentMethod);

Expand Down Expand Up @@ -113,6 +113,7 @@ public function testSupportedMethods(): void
VoucherPayment::class,
In3Payment::class,
PosPayment::class,
TwintPayment::class,
];

$handlers = $this->paymentMethodService->getPaymentHandlers();
Expand Down

0 comments on commit c8da384

Please sign in to comment.