Skip to content

Commit

Permalink
Merge pull request #662 from mollie/release/2.28.0
Browse files Browse the repository at this point in the history
Release/2.28.0
  • Loading branch information
Marvin-Magmodules authored Jul 6, 2023
2 parents 9fce3c5 + 0573cde commit 81f6b20
Show file tree
Hide file tree
Showing 46 changed files with 1,260 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/end-2-end-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
CYPRESS_TESTRAIL_PASSWORD: ${{ secrets.TESTRAIL_PASSWORD }}
CYPRESS_TESTRAIL_PROJECT_ID: 5
CYPRESS_TESTRAIL_MILESTONE_ID: 37
CYPRESS_TESTRAIL_RUN_NAME: "Github Workflow __datetime__, ${{ github.event.head_commit.message }}, PHP version: ${{ matrix.PHP_VERSION }}, Magento version: ${{ matrix.MAGENTO_VERSION }}"
CYPRESS_TESTRAIL_RUN_NAME: "Github Workflow, ${{ github.head_ref || github.ref_name }}, PHP version: ${{ matrix.PHP_VERSION }}, Magento version: ${{ matrix.MAGENTO_VERSION }}"
CYPRESS_TESTRAIL_RUN_CLOSE: true
steps:
- uses: actions/checkout@v2
Expand Down
40 changes: 31 additions & 9 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Framework\Module\Manager;
use Magento\Store\Model\ScopeInterface;
use Mollie\Payment\Logger\MollieLogger;
Expand All @@ -17,6 +16,8 @@
class Config
{
const EXTENSION_CODE = 'Mollie_Payment';
const ADVANCED_INVOICE_MOMENT = 'payment/mollie_general/invoice_moment';
const ADVANCED_ENABLE_MANUAL_CAPTURE = 'payment/mollie_general/enable_manual_capture';
const GENERAL_ENABLED = 'payment/mollie_general/enabled';
const GENERAL_APIKEY_LIVE = 'payment/mollie_general/apikey_live';
const GENERAL_APIKEY_TEST = 'payment/mollie_general/apikey_test';
Expand All @@ -33,6 +34,7 @@ class Config
const GENERAL_ENCRYPT_PAYMENT_DETAILS = 'payment/mollie_general/encrypt_payment_details';
const GENERAL_INCLUDE_SHIPPING_IN_SURCHARGE = 'payment/mollie_general/include_shipping_in_surcharge';
const GENERAL_INVOICE_NOTIFY = 'payment/mollie_general/invoice_notify';
const GENERAL_INVOICE_NOTIFY_KLARNA = 'payment/mollie_general/invoice_notify_klarna';
const GENERAL_LOCALE = 'payment/mollie_general/locale';
const GENERAL_ORDER_STATUS_PENDING = 'payment/mollie_general/order_status_pending';
const GENERAL_PROFILEID = 'payment/mollie_general/profileid';
Expand Down Expand Up @@ -85,11 +87,6 @@ class Config
*/
private $moduleManager;

/**
* @var EncryptorInterface
*/
private $encryptor;

/**
* @var ProductMetadataInterface
*/
Expand All @@ -99,13 +96,11 @@ public function __construct(
ScopeConfigInterface $config,
MollieLogger $logger,
Manager $moduleManager,
EncryptorInterface $encryptor,
ProductMetadataInterface $productMetadata
) {
$this->config = $config;
$this->logger = $logger;
$this->moduleManager = $moduleManager;
$this->encryptor = $encryptor;
$this->productMetadata = $productMetadata;
}

Expand All @@ -126,7 +121,7 @@ private function getPath($path, $storeId, $scope = ScopeInterface::SCOPE_STORE)
* @param string $scope
* @return bool
*/
private function isSetFlag($path, $storeId, $scope = ScopeInterface::SCOPE_STORE)
private function isSetFlag($path, $storeId, string $scope = ScopeInterface::SCOPE_STORE): bool
{
return $this->config->isSetFlag($path, $scope, $storeId);
}
Expand Down Expand Up @@ -242,6 +237,24 @@ public function isDebugMode($storeId = null)
return $this->isSetFlag(static::GENERAL_DEBUG, $storeId);
}

/**
* @param null|int|string $storeId
* @return string|null
*/
public function getInvoiceMoment($storeId = null): ?string
{
return $this->getPath(static::ADVANCED_INVOICE_MOMENT, $storeId);
}

/**
* @param null|int|string $storeId
* @return bool
*/
public function useManualCapture($storeId): bool
{
return $this->isSetFlag(static::ADVANCED_ENABLE_MANUAL_CAPTURE, $storeId);
}

/**
* @param null|int|string $storeId
* @return bool
Expand All @@ -251,6 +264,15 @@ public function sendInvoiceEmail($storeId = null)
return $this->isSetFlag(static::GENERAL_INVOICE_NOTIFY, $storeId);
}

/**
* @param null|int|string $storeId
* @return bool
*/
public function sendInvoiceEmailForKlarna($storeId = null)
{
return $this->isSetFlag(static::GENERAL_INVOICE_NOTIFY_KLARNA, $storeId);
}

/**
* @param null|int|string $storeId
* @return string
Expand Down
19 changes: 15 additions & 4 deletions Model/Client/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Mollie\Payment\Service\Mollie\Order\RefundUsingPayment;
use Mollie\Payment\Service\Mollie\Order\Transaction\Expires;
use Mollie\Payment\Service\Order\BuildTransaction;
use Mollie\Payment\Service\Order\Invoice\ShouldEmailInvoice;
use Mollie\Payment\Service\Order\Lines\StoreCredit;
use Mollie\Payment\Service\Order\OrderCommentHistory;
use Mollie\Payment\Service\Order\PartialInvoice;
Expand Down Expand Up @@ -151,6 +152,10 @@ class Orders extends AbstractModel
* @var OrderLockService
*/
private $orderLockService;
/**
* @var ShouldEmailInvoice
*/
private $shouldEmailInvoice;

/**
* Orders constructor.
Expand Down Expand Up @@ -178,6 +183,7 @@ class Orders extends AbstractModel
* @param EventManager $eventManager
* @param LinkTransactionToOrder $linkTransactionToOrder
* @param OrderLockService $orderLockService
* @param ShouldEmailInvoice $shouldEmailInvoice
*/
public function __construct(
OrderLines $orderLines,
Expand All @@ -202,7 +208,8 @@ public function __construct(
Config $config,
EventManager $eventManager,
LinkTransactionToOrder $linkTransactionToOrder,
OrderLockService $orderLockService
OrderLockService $orderLockService,
ShouldEmailInvoice $shouldEmailInvoice
) {
$this->orderLines = $orderLines;
$this->invoiceSender = $invoiceSender;
Expand All @@ -227,6 +234,7 @@ public function __construct(
$this->config = $config;
$this->linkTransactionToOrder = $linkTransactionToOrder;
$this->orderLockService = $orderLockService;
$this->shouldEmailInvoice = $shouldEmailInvoice;
}

/**
Expand Down Expand Up @@ -557,7 +565,7 @@ public function createShipment(Order\Shipment $shipment, OrderInterface $order)

$this->orderRepository->save($order);

$sendInvoice = $this->mollieHelper->sendInvoice($order->getStoreId());
$sendInvoice = $this->shouldEmailInvoice->execute((int)$order->getStoreId(), $payment->getMethod());
if ($invoice && $invoice->getId() && !$invoice->getEmailSent() && $sendInvoice) {
$this->invoiceSender->send($invoice);
$message = __('Notified customer about invoice #%1', $invoice->getIncrementId());
Expand Down Expand Up @@ -689,9 +697,12 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)

/** @var int|float $remainderAmount */
$remainderAmount = $order->getPayment()->getAdditionalInformation('remainder_amount');
$grandTotal = $this->config->useBaseCurrency($storeId) ?
$creditmemo->getBaseGrandTotal() :
$creditmemo->getGrandTotal();
$maximumAmountToRefund = $order->getBaseGrandTotal() - $remainderAmount;
if ($remainderAmount) {
$amount = $creditmemo->getBaseGrandTotal() > $maximumAmountToRefund ? $maximumAmountToRefund : $creditmemo->getBaseGrandTotal();
$amount = $grandTotal > $maximumAmountToRefund ? $maximumAmountToRefund : $grandTotal;

$this->refundUsingPayment->execute(
$mollieApi,
Expand All @@ -710,7 +721,7 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order)
$mollieApi,
$transactionId,
$creditmemo->getOrderCurrencyCode(),
$creditmemo->getBaseGrandTotal()
$grandTotal
);

return $this;
Expand Down
4 changes: 4 additions & 0 deletions Model/Client/Orders/ProcessTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public function execute(
return $this->orderProcessors->process('expired', $order, $mollieOrder, $type, $defaultResponse);
}

if ($mollieOrder->isShipping()) {
return $this->orderProcessors->process('shipping', $order, $mollieOrder, $type, $defaultResponse);
}

throw new LocalizedException(__('Unable to process order %s', $order->getIncrementId()));
}
}
23 changes: 20 additions & 3 deletions Model/Client/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
use Magento\Framework\Model\AbstractModel;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Invoice;
use Magento\Sales\Model\OrderRepository;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Payment as MolliePayment;
use Mollie\Api\Types\PaymentStatus;
use Mollie\Payment\Helper\General as MollieHelper;
use Mollie\Payment\Model\Adminhtml\Source\InvoiceMoment;
use Mollie\Payment\Model\Client\Payments\ProcessTransaction;
use Mollie\Payment\Service\Mollie\DashboardUrl;
use Mollie\Payment\Service\Mollie\Order\CanRegisterCaptureNotification;
use Mollie\Payment\Service\Mollie\Order\LinkTransactionToOrder;
use Mollie\Payment\Service\Mollie\TransactionDescription;
use Mollie\Payment\Service\Mollie\ValidateMetadata;
Expand Down Expand Up @@ -132,6 +135,11 @@ class Payments extends AbstractModel
*/
private $expiredOrderToTransaction;

/**
* @var CanRegisterCaptureNotification
*/
private $canRegisterCaptureNotification;

/**
* Payments constructor.
*
Expand All @@ -154,6 +162,7 @@ class Payments extends AbstractModel
* @param ValidateMetadata $validateMetadata
* @param SaveAdditionalInformationDetails $saveAdditionalInformationDetails
* @param ExpiredOrderToTransaction $expiredOrderToTransaction
* @param CanRegisterCaptureNotification $canRegisterCaptureNotification
*/
public function __construct(
OrderRepository $orderRepository,
Expand All @@ -174,7 +183,8 @@ public function __construct(
ProcessTransaction $processTransaction,
ValidateMetadata $validateMetadata,
SaveAdditionalInformationDetails $saveAdditionalInformationDetails,
ExpiredOrderToTransaction $expiredOrderToTransaction
ExpiredOrderToTransaction $expiredOrderToTransaction,
CanRegisterCaptureNotification $canRegisterCaptureNotification
) {
$this->orderRepository = $orderRepository;
$this->checkoutSession = $checkoutSession;
Expand All @@ -195,6 +205,7 @@ public function __construct(
$this->validateMetadata = $validateMetadata;
$this->saveAdditionalInformationDetails = $saveAdditionalInformationDetails;
$this->expiredOrderToTransaction = $expiredOrderToTransaction;
$this->canRegisterCaptureNotification = $canRegisterCaptureNotification;
}

/**
Expand Down Expand Up @@ -341,7 +352,7 @@ public function processTransaction(Order $order, $mollieApi, $type = 'webhook',

$refunded = isset($paymentData->_links->refunds) ? true : false;

if ($status == 'paid' && !$refunded) {
if (in_array($status, ['paid', 'authorized']) && !$refunded) {
$amount = $paymentData->amount->value;
$currency = $paymentData->amount->currency;
$orderAmount = $this->orderAmount->getByTransactionId($transactionId);
Expand All @@ -367,7 +378,13 @@ public function processTransaction(Order $order, $mollieApi, $type = 'webhook',
$payment->setTransactionId($transactionId);
$payment->setCurrencyCode($order->getBaseCurrencyCode());
$payment->setIsTransactionClosed(true);
$payment->registerCaptureNotification($order->getBaseGrandTotal(), true);

if ($this->canRegisterCaptureNotification->execute($order) ||
$type != static::TRANSACTION_TYPE_WEBHOOK
) {
$payment->registerCaptureNotification($order->getBaseGrandTotal(), true);
}

$order->setState(Order::STATE_PROCESSING);
$this->transactionProcessor->process($order, null, $paymentData);

Expand Down
104 changes: 104 additions & 0 deletions Model/Client/Payments/CapturePayment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Mollie\Payment\Model\Client\Payments;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\ShipmentInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Mollie\Payment\Helper\General;
use Mollie\Payment\Service\Mollie\MollieApiClient;
use Mollie\Payment\Service\Order\Invoice\ShouldEmailInvoice;
use Mollie\Payment\Service\Order\OrderCommentHistory;
use Mollie\Payment\Service\Order\PartialInvoice;

class CapturePayment
{
/**
* @var MollieApiClient
*/
private $mollieApiClient;

/**
* @var PartialInvoice
*/
private $partialInvoice;

/**
* @var General
*/
private $mollieHelper;

/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* @var InvoiceSender
*/
private $invoiceSender;

/**
* @var OrderCommentHistory
*/
private $orderCommentHistory;
/**
* @var ShouldEmailInvoice
*/
private $shouldEmailInvoice;

public function __construct(
MollieApiClient $mollieApiClient,
PartialInvoice $partialInvoice,
General $mollieHelper,
OrderRepositoryInterface $orderRepository,
InvoiceSender $invoiceSender,
OrderCommentHistory $orderCommentHistory,
ShouldEmailInvoice $shouldEmailInvoice
) {
$this->mollieApiClient = $mollieApiClient;
$this->partialInvoice = $partialInvoice;
$this->mollieHelper = $mollieHelper;
$this->orderRepository = $orderRepository;
$this->invoiceSender = $invoiceSender;
$this->orderCommentHistory = $orderCommentHistory;
$this->shouldEmailInvoice = $shouldEmailInvoice;
}

public function execute(ShipmentInterface $shipment, OrderInterface $order): void
{
$payment = $order->getPayment();
$invoice = $this->partialInvoice->createFromShipment($shipment);
if (!$invoice) {
return;
}

$captureAmount = $invoice->getBaseGrandTotal();

$mollieTransactionId = $order->getMollieTransactionId();
$mollieApi = $this->mollieApiClient->loadByStore($order->getStoreId());

$data = [];
if ($captureAmount != $order->getBaseGrandTotal()) {
$data['amount'] = $this->mollieHelper->getAmountArray(
$order->getOrderCurrencyCode(),
$captureAmount
);
}

$capture = $mollieApi->paymentCaptures->createForId($mollieTransactionId, $data);

$payment->setTransactionId($capture->id);
$payment->registerCaptureNotification($captureAmount, true);

$this->orderRepository->save($order);

$sendInvoice = $this->shouldEmailInvoice->execute($order->getStoreId(), $payment->getMethod());
if ($invoice && $invoice->getId() && !$invoice->getEmailSent() && $sendInvoice) {
$this->invoiceSender->send($invoice);
$message = __('Notified customer about invoice #%1', $invoice->getIncrementId());
$this->orderCommentHistory->add($order, $message, true);
}
}
}
2 changes: 1 addition & 1 deletion Model/Client/Payments/ProcessTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function execute(
);

$refunded = isset($molliePayment->_links->refunds) ? true : false;
if ($status == 'paid' && !$refunded) {
if (in_array($status, ['paid', 'authorized']) && !$refunded) {
return $this->paymentProcessors->process(
'paid',
$magentoOrder,
Expand Down
Loading

0 comments on commit 81f6b20

Please sign in to comment.