Skip to content

Commit

Permalink
NTR: PISHPW-229: add trustly payment method (#426)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitalij Mik <vitalij.mik@dasistweb.de>
  • Loading branch information
BlackScorp and Vitalij Mik authored Sep 9, 2024
1 parent 4c0325c commit f90c1a5
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions Components/Constants/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ class PaymentMethod
const RIVERTY = "riverty";
const PAYCONIQ = "payconiq";
const SATISPAY = "satispay";
const TRUSTLY = "trustly";
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static function getSupportedPaymentMethods()
PaymentMethod::RIVERTY,
PaymentMethod::PAYCONIQ,
PaymentMethod::SATISPAY,
PaymentMethod::TRUSTLY,
];
}

Expand Down
5 changes: 5 additions & 0 deletions Services/Mollie/Payments/PaymentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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\Trustly;
use MollieShopware\Services\Mollie\Payments\Requests\Twint;
use MollieShopware\Services\Mollie\Payments\Requests\Voucher;

Expand Down Expand Up @@ -124,8 +125,12 @@ public function createByPaymentName($paymentMethod)

case PaymentMethod::PAYCONIQ:
return new PayConiq();

case PaymentMethod::SATISPAY:
return new Satispay();

case PaymentMethod::TRUSTLY:
return new Trustly();
}

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

namespace MollieShopware\Services\Mollie\Payments\Requests;

use MollieShopware\Components\Constants\PaymentMethod;
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\PaymentInterface;

class Trustly extends AbstractPayment implements PaymentInterface
{

/**
*/
public function __construct()
{
parent::__construct(
new AddressConverter(),
new LineItemConverter(),
PaymentMethod::TRUSTLY
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function testSupportedPaymentMethods()
PaymentMethod::RIVERTY,
PaymentMethod::PAYCONIQ,
PaymentMethod::SATISPAY,
PaymentMethod::TRUSTLY,
];

$this->assertEquals($expected, PaymentMethodsInstaller::getSupportedPaymentMethods());
Expand Down
137 changes: 137 additions & 0 deletions Tests/PHPUnit/Services/Mollie/Payments/Requests/TrustlyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

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

use MollieShopware\Components\Constants\PaymentMethod;
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\Giropay;
use MollieShopware\Services\Mollie\Payments\Requests\Trustly;
use MollieShopware\Tests\Utils\Traits\PaymentTestTrait;
use PHPUnit\Framework\TestCase;

class TrustlyTest extends TestCase
{
use PaymentTestTrait;


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

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

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


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

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

$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],
'USD',
'de_DE',
'https://local/redirect',
'https://local/notify'
)
);
}

/**
* This test verifies that the Payments-API request
* for our payment is correct.
*/
public function testPaymentsAPI()
{
$expected = [
'method' => PaymentMethod::TRUSTLY,
'amount' => [
'currency' => 'USD',
'value' => '49.98',
],
'description' => 'Order 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' => PaymentMethod::TRUSTLY,
'amount' => [
'currency' => 'USD',
'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']);
}
}

0 comments on commit f90c1a5

Please sign in to comment.