Skip to content

Commit

Permalink
Merge pull request #194 from mollie/1.7.1
Browse files Browse the repository at this point in the history
1.7.1
  • Loading branch information
Marvin-Magmodules authored Aug 23, 2019
2 parents f070c67 + 4464a0c commit e00bcf3
Show file tree
Hide file tree
Showing 34 changed files with 646 additions and 91 deletions.
29 changes: 28 additions & 1 deletion Block/Form/Paymentlink.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,44 @@

namespace Mollie\Payment\Block\Form;

use Magento\Framework\View\Element\Template\Context;
use Mollie\Payment\Service\Mollie\Order\Transaction\Expires;

/**
* Class Paymentlink
*
* @package Mollie\Payment\Block\Form
*/
class Paymentlink extends \Magento\Payment\Block\Form
{

/**
* @var string
*/
protected $_template = 'Mollie_Payment::form/mollie_paymentlink.phtml';

/**
* @var Expires
*/
private $expires;

public function __construct(
Context $context,
Expires $expires,
array $data = []
) {
parent::__construct($context, $data);
$this->expires = $expires;
}

public function getExpiresAt()
{
$storeId = $this->getRequest()->getParam('store_id');

$days = $this->expires->getExpiresAtForMethod('paymentlink', $storeId);
if (!$days) {
return $days;
}

return $this->expires->atDateForMethod('paymentlink');
}
}
18 changes: 15 additions & 3 deletions Model/Client/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Mollie\Payment\Model\Adminhtml\Source\InvoiceMoment;
use Mollie\Payment\Model\OrderLines;
use Mollie\Payment\Service\Mollie\Order\RefundUsingPayment;
use Mollie\Payment\Service\Mollie\Order\Transaction\Expires;
use Mollie\Payment\Service\Order\Lines\StoreCredit;
use Mollie\Payment\Service\Order\OrderCommentHistory;
use Mollie\Payment\Service\Order\PartialInvoice;
Expand Down Expand Up @@ -101,6 +102,10 @@ class Orders extends AbstractModel
* @var PartialInvoice
*/
private $partialInvoice;
/**
* @var Expires
*/
private $expires;

/**
* Orders constructor.
Expand All @@ -116,10 +121,11 @@ class Orders extends AbstractModel
* @param Registry $registry
* @param MollieHelper $mollieHelper
* @param ProcessAdjustmentFee $adjustmentFee
* @param StoreCredit $storeCredit
* @param RefundUsingPayment $refundUsingPayment
* @param OrderCommentHistory $orderCommentHistory
* @param PartialInvoice $partialInvoice
* @param StoreCredit $storeCredit
* @param RefundUsingPayment $refundUsingPayment
* @param Expires $expires
*/
public function __construct(
OrderLines $orderLines,
Expand All @@ -136,7 +142,8 @@ public function __construct(
OrderCommentHistory $orderCommentHistory,
PartialInvoice $partialInvoice,
StoreCredit $storeCredit,
RefundUsingPayment $refundUsingPayment
RefundUsingPayment $refundUsingPayment,
Expires $expires
) {
$this->orderLines = $orderLines;
$this->orderSender = $orderSender;
Expand All @@ -153,6 +160,7 @@ public function __construct(
$this->refundUsingPayment = $refundUsingPayment;
$this->orderCommentHistory = $orderCommentHistory;
$this->partialInvoice = $partialInvoice;
$this->expires = $expires;
}

/**
Expand Down Expand Up @@ -210,6 +218,10 @@ public function startTransaction(Order $order, $mollieApi)
$orderData['method'] = $additionalData['limited_methods'];
}

if ($this->expires->availableForMethod($method, $storeId)) {
$orderData['expiresAt'] = $this->expires->atDateForMethod($method, $storeId);
}

$this->mollieHelper->addTolog('request', $orderData);
$mollieOrder = $mollieApi->orders->create($orderData);
$this->processResponse($order, $mollieOrder);
Expand Down
2 changes: 1 addition & 1 deletion Model/Mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
}

$activeMethods = $this->mollieHelper->getAllActiveMethods($quote->getStoreId());
if (!array_key_exists($this->_code, $activeMethods)) {
if ($this->_code != 'mollie_methods_paymentlink' && !array_key_exists($this->_code, $activeMethods)) {
return false;
}

Expand Down
17 changes: 10 additions & 7 deletions Model/MollieConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class MollieConfigProvider implements ConfigProviderInterface
*/
private $checkoutSession;

/**
* @var array|null
*/
private $methodData;

/**
* MollieConfigProvider constructor.
*
Expand Down Expand Up @@ -198,10 +203,8 @@ public function getConfig()
*/
public function getActiveMethods($mollieApi)
{
static $methodData = null;

if ($methodData !== null) {
return $methodData;
if ($this->methodData !== null) {
return $this->methodData;
}

try {
Expand All @@ -217,16 +220,16 @@ public function getActiveMethods($mollieApi)

foreach ($apiMethods as $method) {
$methodId = 'mollie_methods_' . $method->id;
$methodData[$methodId] = [
$this->methodData[$methodId] = [
'image' => $method->image->size2x
];
}
} catch (\Exception $e) {
$this->mollieHelper->addTolog('error', 'Function: getActiveMethods: ' . $e->getMessage());
$methodData = [];
$this->methodData = [];
}

return $methodData;
return $this->methodData;
}

/**
Expand Down
89 changes: 89 additions & 0 deletions Service/Mollie/Order/Transaction/Expires.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © 2019 Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Mollie\Payment\Service\Mollie\Order\Transaction;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

class Expires
{
/**
* @var ScopeConfigInterface
*/
private $config;

/**
* @var TimezoneInterface
*/
private $timezone;

/**
* @var RequestInterface
*/
private $request;

public function __construct(
ScopeConfigInterface $config,
TimezoneInterface $timezone,
RequestInterface $request
) {
$this->config = $config;
$this->timezone = $timezone;
$this->request = $request;
}

public function availableForMethod(string $method = null, $storeId = null)
{
$value = $this->getExpiresAtForMethod($method, $storeId);
return (bool)$value;
}

public function atDateForMethod(string $method = null, $storeId = null)
{
$days = $this->getExpiresAtForMethod($method, $storeId);

if (strtotime($days)) {
return $days;
}

$date = $this->timezone->scopeDate($storeId);
$date->add(new \DateInterval('P' . $days . 'D'));

return $date->format('Y-m-d');
}

/**
* @param string $method
* @param $storeId
* @return mixed
*/
public function getExpiresAtForMethod(string $method = null, $storeId = null)
{
if (!$method && $value = $this->getValueFromRequest()) {
return $value;
}

$path = sprintf('payment/mollie_methods_%s/days_before_expire', $method);

return $this->config->getValue($path, 'store', $storeId);
}

/**
* @return mixed
*/
private function getValueFromRequest()
{
$payment = $this->request->getParam('payment');

if (!$payment || !isset($payment['days_before_expire'])) {
return false;
}

return $payment['days_before_expire'];
}
}
7 changes: 3 additions & 4 deletions Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ public function upgrade(
$this->removeBitcoinConfiguration();
}

if (version_compare($context->getVersion(), '1.6.1', '<')) {
$this->upgradeActiveState();
}

if (version_compare($context->getVersion(), '1.6.2', '<')) {
$this->addIndexes($setup);
}

// This should run every time
$this->upgradeActiveState();

$setup->endSetup();
}

Expand Down
38 changes: 38 additions & 0 deletions Test/Integration/Block/Form/PaymentlinkTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © 2019 Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Mollie\Payment\Test\Integration\Block\Form;

use Mollie\Payment\Block\Form\Paymentlink;
use Mollie\Payment\Test\Integration\IntegrationTestCase;

class PaymentlinkTest extends IntegrationTestCase
{
/**
* @magentoConfigFixture current_store payment/mollie_methods_paymentlink/days_before_expire 10
*/
public function testReturnsTheCorrectDate()
{
/** @var Paymentlink $instance */
$instance = $this->objectManager->create(Paymentlink::class);

$now = new \DateTimeImmutable('now');
$expected = $now->add(new \DateInterval('P10D'));

$this->assertEquals($expected->format('Y-m-d'), $instance->getExpiresAt());
}

/**
* @magentoConfigFixture current_store payment/mollie_methods_paymentlink/days_before_expire
*/
public function testAnEmptyStringWhenNoConfigAvailable()
{
/** @var Paymentlink $instance */
$instance = $this->objectManager->create(Paymentlink::class);

$this->assertEmpty($instance->getExpiresAt());
}
}
36 changes: 36 additions & 0 deletions Test/Integration/Model/Client/OrdersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,40 @@ protected function mollieOrderMock($status, $currency)

return $mollieOrder;
}

/**
* @magentoDataFixture Magento/Sales/_files/order.php
* @magentoConfigFixture default_store payment/mollie_methods_ideal/days_before_expire 5
*
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Mollie\Api\Exceptions\ApiException
*/
public function testStartTransactionIncludesTheExpiresAtParameter()
{
$order = $this->loadOrder('100000001');
$order->getPayment()->setMethod('mollie_methods_ideal');

$mollieApiMock = $this->createMock(MollieApiClient::class);
$orderEndpointMock = $this->createMock(OrderEndpoint::class);
$orderEndpointMock->method('create')->with( $this->callback(function ($orderData) {
$this->assertArrayHasKey('expiresAt', $orderData);
$this->assertNotEmpty($orderData['expiresAt']);

$now = new \DateTimeImmutable('now');
$expected = $now->add(new \DateInterval('P5D'));

$this->assertEquals($expected->format('Y-m-d'), $orderData['expiresAt']);

return true;
}))->willReturn($this->createMock(\Mollie\Api\Resources\Order::class));

$mollieApiMock->orders = $orderEndpointMock;

/** @var Orders $instance */
$instance = $this->objectManager->create(Orders::class, [
'orderLines' => $this->createMock(\Mollie\Payment\Model\OrderLines::class),
]);

$instance->startTransaction($order, $mollieApiMock);
}
}
Loading

0 comments on commit e00bcf3

Please sign in to comment.