Skip to content

Commit

Permalink
Merge pull request #639 from mollie/release/2.25.0
Browse files Browse the repository at this point in the history
Release/2.25.0
  • Loading branch information
Marvin-Magmodules authored May 9, 2023
2 parents fec00e2 + 06f0e4d commit 0a8e772
Show file tree
Hide file tree
Showing 40 changed files with 867 additions and 30 deletions.
12 changes: 12 additions & 0 deletions Api/Data/TransactionToOrderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface TransactionToOrderInterface extends ExtensibleDataInterface
{
public const TRANSACTION_ID = 'transaction_id';
public const ORDER_ID = 'order_id';
public const SKIPPED = 'skipped';
public const CREATED_AT = 'created_at';

/**
Expand Down Expand Up @@ -45,6 +46,17 @@ public function getCreatedAt(): ?string;
*/
public function setCreatedAt(string $created_at): \Mollie\Payment\Api\Data\TransactionToOrderInterface;

/**
* @return string|null
*/
public function getSkipped(): ?int;

/**
* @param int $skipped
* @return \Mollie\Payment\Api\Data\TransactionToOrderInterface
*/
public function setSkipped(int $skipped): \Mollie\Payment\Api\Data\TransactionToOrderInterface;

/**
* @return \Mollie\Payment\Api\Data\TransactionToOrderExtensionInterface|null
*/
Expand Down
10 changes: 10 additions & 0 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Config
const GENERAL_REDIRECT_WHEN_TRANSACTION_FAILS_TO = 'payment/mollie_general/redirect_when_transaction_fails_to';
const GENERAL_SECOND_CHANCE_EMAIL_TEMPLATE = 'payment/mollie_general/second_chance_email_template';
const GENERAL_SECOND_CHANCE_DELAY = 'payment/mollie_general/second_chance_email_delay';
const GENERAL_SECOND_CHANCE_SEND_BCC_TO = 'payment/mollie_general/second_chance_send_bcc_to';
const GENERAL_SECOND_CHANCE_USE_PAYMENT_METHOD = 'payment/mollie_general/second_chance_use_payment_method';
const GENERAL_TYPE = 'payment/mollie_general/type';
const GENERAL_USE_BASE_CURRENCY = 'payment/mollie_general/currency';
Expand Down Expand Up @@ -308,6 +309,15 @@ public function secondChanceEmailDelay($storeId = null)
return $this->getPath(static::GENERAL_SECOND_CHANCE_DELAY, $storeId);
}

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

/**
* @param null|int|string $storeId
* @return string|null
Expand Down
6 changes: 3 additions & 3 deletions Controller/Checkout/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function execute()
return $this->_redirect($redirect->getData('path'));
} catch (\Exception $e) {
$this->mollieHelper->addTolog('error', $e->getMessage());
$this->messageManager->addErrorMessage(__('Something went wrong.'));
$this->messageManager->addErrorMessage(__('Transaction failed. Please verify your billing information and payment method, and try again.'));
return $this->_redirect($this->redirectOnError->getUrl());
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ protected function handleNonSuccessResult(array $result, array $orderIds): Respo
protected function addResultMessage(array $result)
{
if (!isset($result['status'])) {
$this->messageManager->addErrorMessage(__('Something went wrong.'));
$this->messageManager->addErrorMessage(__('Transaction failed. Please verify your billing information and payment method, and try again.'));
return;
}

Expand All @@ -171,7 +171,7 @@ protected function addResultMessage(array $result)
return;
}

$this->messageManager->addErrorMessage(__('Something went wrong.'));
$this->messageManager->addErrorMessage(__('Transaction failed. Please verify your billing information and payment method, and try again.'));
}

/**
Expand Down
66 changes: 66 additions & 0 deletions Model/Client/Orders/Processors/ExpiredProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Mollie\Payment\Model\Client\Orders\Processors;

use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Api\Resources\Order;
use Mollie\Payment\Model\Client\OrderProcessorInterface;
use Mollie\Payment\Model\Client\ProcessTransactionResponse;
use Mollie\Payment\Model\Client\ProcessTransactionResponseFactory;
use Mollie\Payment\Service\Order\ExpiredOrderToTransaction;

class ExpiredProcessor implements OrderProcessorInterface
{
/**
* @var CancelledProcessor
*/
private $cancelledProcessor;

/**
* @var ProcessTransactionResponseFactory
*/
private $processTransactionResponseFactory;

/**
* @var ExpiredOrderToTransaction
*/
private $expiredOrderToTransaction;

public function __construct(
CancelledProcessor $cancelledProcessor,
ExpiredOrderToTransaction $expiredOrderToTransaction,
ProcessTransactionResponseFactory $processTransactionResponseFactory
) {
$this->cancelledProcessor = $cancelledProcessor;
$this->processTransactionResponseFactory = $processTransactionResponseFactory;
$this->expiredOrderToTransaction = $expiredOrderToTransaction;
}

public function process(
OrderInterface $magentoOrder,
Order $mollieOrder,
string $type,
ProcessTransactionResponse $response
): ?ProcessTransactionResponse {
if ($this->shouldCancelProcessing($magentoOrder)) {
return $this->processTransactionResponseFactory->create([
'success' => false,
'status' => $mollieOrder->status,
'order_id' => $magentoOrder->getEntityId(),
'type' => $type
]);
}

return $this->cancelledProcessor->process($magentoOrder, $mollieOrder, $type, $response);
}

private function shouldCancelProcessing(OrderInterface $order): bool
{
if (!$this->expiredOrderToTransaction->hasMultipleTransactions($order)) {
return false;
}

$this->expiredOrderToTransaction->markTransactionAsSkipped($order->getMollieTransactionId());
return true;
}
}
19 changes: 18 additions & 1 deletion Model/Client/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Mollie\Payment\Service\Order\OrderAmount;
use Mollie\Payment\Service\Order\CancelOrder;
use Mollie\Payment\Service\Order\OrderCommentHistory;
use Mollie\Payment\Service\Order\ExpiredOrderToTransaction;
use Mollie\Payment\Service\Order\SaveAdditionalInformationDetails;
use Mollie\Payment\Service\Order\SendOrderEmails;
use Mollie\Payment\Service\Order\Transaction;
Expand Down Expand Up @@ -125,6 +126,11 @@ class Payments extends AbstractModel
*/
private $saveAdditionalInformationDetails;

/**
* @var ExpiredOrderToTransaction
*/
private $expiredOrderToTransaction;

/**
* Payments constructor.
*
Expand All @@ -146,6 +152,7 @@ class Payments extends AbstractModel
* @param ProcessTransaction $processTransaction
* @param ValidateMetadata $validateMetadata
* @param SaveAdditionalInformationDetails $saveAdditionalInformationDetails
* @param ExpiredOrderToTransaction $expiredOrderToTransaction
*/
public function __construct(
OrderRepository $orderRepository,
Expand All @@ -165,7 +172,8 @@ public function __construct(
LinkTransactionToOrder $linkTransactionToOrder,
ProcessTransaction $processTransaction,
ValidateMetadata $validateMetadata,
SaveAdditionalInformationDetails $saveAdditionalInformationDetails
SaveAdditionalInformationDetails $saveAdditionalInformationDetails,
ExpiredOrderToTransaction $expiredOrderToTransaction
) {
$this->orderRepository = $orderRepository;
$this->checkoutSession = $checkoutSession;
Expand All @@ -185,6 +193,7 @@ public function __construct(
$this->processTransaction = $processTransaction;
$this->validateMetadata = $validateMetadata;
$this->saveAdditionalInformationDetails = $saveAdditionalInformationDetails;
$this->expiredOrderToTransaction = $expiredOrderToTransaction;
}

/**
Expand Down Expand Up @@ -422,6 +431,14 @@ public function processTransaction(Order $order, $mollieApi, $type = 'webhook',
$this->mollieHelper->addTolog('success', $msg);
return $msg;
}
if ($status == 'expired') {
if ($this->expiredOrderToTransaction->hasMultipleTransactions($order)) {
$this->expiredOrderToTransaction->markTransactionAsSkipped($transactionId);
$msg = ['success' => false, 'status' => $status, 'order_id' => $orderId, 'type' => $type];
$this->mollieHelper->addTolog('success', $msg);
return $msg;
}
}
if ($status == 'canceled' || $status == 'failed' || $status == 'expired') {
if ($type == 'webhook') {
$this->cancelOrder->execute($order, $status);
Expand Down
10 changes: 10 additions & 0 deletions Model/Client/Payments/ProcessTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ public function execute(
);
}

if ($status == 'expired') {
return $this->paymentProcessors->process(
'expired',
$magentoOrder,
$molliePayment,
$type,
$defaultResponse
);
}

if ($status == 'canceled' || $status == 'failed' || $status == 'expired') {
return $this->paymentProcessors->process(
'failed',
Expand Down
64 changes: 64 additions & 0 deletions Model/Client/Payments/Processors/ExpiredStatusProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Mollie\Payment\Model\Client\Payments\Processors;

use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Api\Resources\Payment;
use Mollie\Payment\Model\Client\PaymentProcessorInterface;
use Mollie\Payment\Model\Client\ProcessTransactionResponse;
use Mollie\Payment\Model\Client\ProcessTransactionResponseFactory;
use Mollie\Payment\Service\Order\ExpiredOrderToTransaction;

class ExpiredStatusProcessor implements PaymentProcessorInterface
{
/**
* @var ExpiredOrderToTransaction
*/
private $expiredOrderToTransaction;
/**
* @var FailedStatusProcessor
*/
private $failedStatusProcessor;
/**
* @var ProcessTransactionResponseFactory
*/
private $processTransactionResponseFactory;

public function __construct(
ExpiredOrderToTransaction $expiredOrderToTransaction,
FailedStatusProcessor $failedStatusProcessor,
ProcessTransactionResponseFactory $processTransactionResponseFactory
) {
$this->expiredOrderToTransaction = $expiredOrderToTransaction;
$this->failedStatusProcessor = $failedStatusProcessor;
$this->processTransactionResponseFactory = $processTransactionResponseFactory;
}

public function process(
OrderInterface $magentoOrder,
Payment $molliePayment,
string $type,
ProcessTransactionResponse $response
): ?ProcessTransactionResponse {
if ($this->shouldCancelProcessing($magentoOrder)) {
return $this->processTransactionResponseFactory->create([
'success' => false,
'status' => $molliePayment->status,
'order_id' => $magentoOrder->getEntityId(),
'type' => $type
]);
}

return $this->failedStatusProcessor->process($magentoOrder, $molliePayment, $type, $response);
}

private function shouldCancelProcessing(OrderInterface $order): bool
{
if (!$this->expiredOrderToTransaction->hasMultipleTransactions($order)) {
return false;
}

$this->expiredOrderToTransaction->markTransactionAsSkipped($order->getMollieTransactionId());
return true;
}
}
19 changes: 19 additions & 0 deletions Model/TransactionToOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,25 @@ public function setCreatedAt(string $created_at): TransactionToOrderInterface
return $this->setData(self::CREATED_AT, $created_at);
}

/**
* Get skipped
* @return int|null
*/
public function getSkipped(): ?int
{
return (int)$this->getData(self::SKIPPED);
}

/**
* Set skipped
* @param int $skipped
* @return TransactionToOrderInterface
*/
public function setSkipped(int $skipped): TransactionToOrderInterface
{
return $this->setData(self::SKIPPED, $skipped);
}

/**
* Retrieve existing extension attributes object or create a new one.
* @return TransactionToOrderExtensionInterface|null
Expand Down
23 changes: 22 additions & 1 deletion Service/Mollie/Order/RedirectUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Mollie\Payment\Service\Mollie\Order;

use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\UrlInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Mollie\Payment\Config;
Expand All @@ -22,12 +23,19 @@ class RedirectUrl
*/
private $url;

/**
* @var ManagerInterface
*/
private $messageManager;

public function __construct(
Config $config,
UrlInterface $url
UrlInterface $url,
ManagerInterface $messageManager
) {
$this->config = $config;
$this->url = $url;
$this->messageManager = $messageManager;
}

public function execute(Mollie $methodInstance, OrderInterface $order): string
Expand All @@ -46,6 +54,19 @@ public function execute(Mollie $methodInstance, OrderInterface $order): string
return $this->url->getUrl('checkout/onepage/success/');
}

if (!$redirectUrl) {
$this->config->addToLog(
'error',
'RedirectUrl: No redirect url found for order ' . $order->getIncrementId()
);

$this->messageManager->addErrorMessage(
__('Something went wrong while trying to redirect you to Mollie. Please try again later.')
);

return $this->url->getUrl('checkout/cart');
}

return $redirectUrl;
}
}
16 changes: 15 additions & 1 deletion Service/Mollie/ValidateMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,31 @@ class ValidateMetadata
*/
private $config;

/**
* @var bool
*/
private $skipValidation = false;

public function __construct(
Config $config
) {
$this->config = $config;
}

public function skipValidation(): void
{
$this->skipValidation = true;
}

/**
* @throws LocalizedException
*/
public function execute(\stdClass $metadata, OrderInterface $order): void
public function execute(\stdClass $metadata = null, OrderInterface $order): void
{
if ($this->skipValidation) {
return;
}

if (isset($metadata->order_id)) {
$this->validateSingleOrder($metadata, $order);
return;
Expand Down
Loading

0 comments on commit 0a8e772

Please sign in to comment.