Skip to content

Commit

Permalink
MOL-1281: Add TWINT payment method (#388)
Browse files Browse the repository at this point in the history
* add twint integration

* fix an issue with getting all active methods

* do not request twint explicit as order

* do not block twint for payments api

* fix test for twint

* Update PaymentMethodType.php
  • Loading branch information
resslinger authored Dec 8, 2023
1 parent 9c79a3e commit c6f6a90
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 12 deletions.
1 change: 1 addition & 0 deletions Components/Constants/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ class PaymentMethod
const APPLEPAY_DIRECT = "applepaydirect";
const VOUCHERS = "voucher";
const IN3 = "in3";
const TWINT = "twint";
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public static function getSupportedPaymentMethods()
PaymentMethod::SOFORT,
PaymentMethod::VOUCHERS,
PaymentMethod::IN3,
PaymentMethod::TWINT,
];
}

Expand Down
18 changes: 6 additions & 12 deletions Components/Payment/Provider/ActivePaymentMethodsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,12 @@ private function getActivePaymentMethodsForShop(Shop $shop, array $parameters =
{
$mollieApiClient = $this->mollieApiFactory->create($shop->getId());

# adds resource to parameters if not in array
if (!in_array('resource', $parameters, true)) {
$parameters['resource'] = 'orders';
}

# adds wallets to parameters if not in array
if (!in_array('includeWallets', $parameters, true)) {
$parameters['includeWallets'] = 'applepay';
}

return $mollieApiClient->methods
->allActive($parameters)
$methods = $mollieApiClient->methods
->allAvailable($parameters)
->getArrayCopy();

return array_filter($methods, function ($method) {
return $method->status == 'activated';
});
}
}
4 changes: 4 additions & 0 deletions Services/Mollie/Payments/PaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use MollieShopware\Services\Mollie\Payments\Requests\SepaDirectDebit;
use MollieShopware\Services\Mollie\Payments\Requests\SliceIt;
use MollieShopware\Services\Mollie\Payments\Requests\Sofort;
use MollieShopware\Services\Mollie\Payments\Requests\Twint;
use MollieShopware\Services\Mollie\Payments\Requests\Voucher;

class PaymentFactory
Expand Down Expand Up @@ -99,6 +100,9 @@ public function createByPaymentName($paymentMethod)

case PaymentMethod::IN3:
return new In3();

case PaymentMethod::TWINT:
return new Twint();
}

throw new \Exception('Payment handler not found for: ' . $paymentMethod);
Expand Down
24 changes: 24 additions & 0 deletions Services/Mollie/Payments/Requests/Twint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace MollieShopware\Services\Mollie\Payments\Requests;

use MollieShopware\Services\Mollie\Payments\AbstractPayment;
use MollieShopware\Services\Mollie\Payments\Converters\AddressConverter;
use MollieShopware\Services\Mollie\Payments\Converters\LineItemConverter;
use MollieShopware\Services\Mollie\Payments\Exceptions\ApiNotSupportedException;
use MollieShopware\Services\Mollie\Payments\PaymentInterface;

class Twint extends AbstractPayment implements PaymentInterface
{

/**
*/
public function __construct()
{
parent::__construct(
new AddressConverter(),
new LineItemConverter(),
'twint'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ const configs = [
];

const payments = [
{caseId: 'C532050', key: 'twint', name: 'Twint', paymentsAPI: true, createOrderBeforePayment: true},
{caseId: 'C532051', key: 'twint', name: 'Twint', paymentsAPI: true, createOrderBeforePayment: false},
{caseId: 'C532052', key: 'twint', name: 'Twint', paymentsAPI: false, createOrderBeforePayment: true},
{caseId: 'C532053', key: 'twint', name: 'Twint', paymentsAPI: false, createOrderBeforePayment: false},

{caseId: 'C532046', key: 'billie', name: 'Billie', paymentsAPI: true, createOrderBeforePayment: true},
{caseId: 'C532047', key: 'billie', name: 'Billie', paymentsAPI: true, createOrderBeforePayment: false},
{caseId: 'C532048', key: 'billie', name: 'Billie', paymentsAPI: false, createOrderBeforePayment: true},
Expand Down Expand Up @@ -207,6 +212,10 @@ configs.forEach(config => {

molliePaymentScreen.selectAuthorized();

} else if (payment.key === 'twint') {

molliePaymentScreen.selectAuthorized();

} else if (payment.key === 'giftcard') {

mollieGiftCardsScreen.selectBeautyCards();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function getPaymentMethods()
[false, PaymentMethod::KLARNA_PAY_LATER],
[false, PaymentMethod::KLARNA_PAY_NOW],
[false, PaymentMethod::KLARNA_SLICE_IT],
[false, PaymentMethod::TWINT],
[false, PaymentMethod::VOUCHERS],
# ----------------------------------------------
[true, PaymentMethod::PAYPAL],
Expand Down
136 changes: 136 additions & 0 deletions Tests/PHPUnit/Services/Mollie/Payments/Requests/TwintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace MollieShopware\Tests\PHPUnit\Services\Mollie\Payments\Requests;

use MollieShopware\Services\Mollie\Payments\Models\Payment;
use MollieShopware\Services\Mollie\Payments\Models\PaymentAddress;
use MollieShopware\Services\Mollie\Payments\Models\PaymentLineItem;
use MollieShopware\Services\Mollie\Payments\Requests\Twint;
use MollieShopware\Tests\Utils\Traits\PaymentTestTrait;
use PHPUnit\Framework\TestCase;

class TwintTest extends TestCase
{
use PaymentTestTrait;


/**
* @var Twint
*/
private $payment;

/**
* @var PaymentAddress
*/
private $addressInvoice;

/**
* @var PaymentAddress
*/
private $addressShipping;

/**
* @var PaymentLineItem
*/
private $lineItem;

/**
*
*/
public function setUp(): void
{
$this->payment = new Twint();

$this->addressInvoice = $this->getAddressFixture1();
$this->addressShipping = $this->getAddressFixture2();
$this->lineItem = $this->getLineItemFixture();

$this->payment->setPayment(
new Payment(
'UUID-123',
'Order UUID-123',
'20004',
$this->addressInvoice,
$this->addressShipping,
49.98,
[$this->lineItem],
'CHF',
'de_DE',
'https://local/redirect',
'https://local/notify'
)
);
}

/**
* This test verifies that the Payments-API request
* for our payment is correct.
* Please note, Twint does not work with the payments API
* so this is just an empty array
*/
public function testPaymentsAPI()
{
$expected = [
'method' => 'twint',
'amount' => [
'currency' => 'CHF',
'value' => '49.98',
],
'description' => 'Payment UUID-123',
'redirectUrl' => 'https://local/redirect',
'webhookUrl' => 'https://local/notify',
'locale' => 'de_DE',
];

$requestBody = $this->payment->buildBodyPaymentsAPI();

$this->assertEquals($expected, $requestBody);
}

/**
* This test verifies that the Orders-API request
* for our payment is correct.
*/
public function testOrdersAPI()
{
$expected = [
'method' => 'twint',
'amount' => [
'currency' => 'CHF',
'value' => '49.98',
],
'redirectUrl' => 'https://local/redirect',
'webhookUrl' => 'https://local/notify',
'locale' => 'de_DE',
'orderNumber' => '20004',
'payment' => [
'webhookUrl' => 'https://local/notify',
],
'billingAddress' => $this->getExpectedAddressStructure($this->addressInvoice),
'shippingAddress' => $this->getExpectedAddressStructure($this->addressShipping),
'lines' => [
$this->getExpectedLineItemStructure($this->lineItem),
],
'metadata' => [],
];

$requestBody = $this->payment->buildBodyOrdersAPI();

$this->assertSame($expected, $requestBody);
}

/**
* This test verifies that we can set a custom expiration date
* for our Orders API request.
*/
public function testExpirationDate()
{
$dueInDays = 5;
$expectedDueDate = date('Y-m-d', strtotime(' + ' . $dueInDays . ' day'));

$this->payment->setExpirationDays($dueInDays);
$request = $this->payment->buildBodyOrdersAPI();

$this->assertEquals($expectedDueDate, $request['expiresAt']);
}
}
8 changes: 8 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
https://github.com/mollie/Shopware/wiki
</description>

<changelog version="2.5.0">
<changes lang="de">
- Neue Zahlungsmethode "Twint" ist nun verfügbar.
</changes>
<changes lang="en">
- New payment method "Twint" is now available.
</changes>
</changelog>

<changelog version="2.4.0">
<changes lang="de">
Expand Down

0 comments on commit c6f6a90

Please sign in to comment.