From ba70e70d66f7db539634178f399abafe1c15ad3a Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 20 Nov 2023 11:58:20 +0100 Subject: [PATCH] Bugfix: Use the correct method code when using payment link #718 --- Helper/General.php | 74 +++---------------- Model/Client/Orders.php | 46 +++--------- Model/Client/Payments.php | 34 +++------ Service/Order/MethodCode.php | 43 +++++++++++ .../Service/Order/MethodCodeTest.php | 74 +++++++++++++++++++ 5 files changed, 144 insertions(+), 127 deletions(-) create mode 100644 Service/Order/MethodCode.php create mode 100644 Test/Integration/Service/Order/MethodCodeTest.php diff --git a/Helper/General.php b/Helper/General.php index 36683d2556e..faf42696d71 100755 --- a/Helper/General.php +++ b/Helper/General.php @@ -9,12 +9,9 @@ use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Framework\App\ProductMetadataInterface; -use Magento\Framework\Encryption\EncryptorInterface; use Magento\Framework\Locale\Resolver; -use Magento\Framework\Math\Random as MathRandom; use Magento\Framework\Module\ModuleListInterface; use Magento\Sales\Api\Data\OrderInterface; -use Magento\Sales\Api\OrderManagementInterface; use Magento\Sales\Model\Order; use Magento\Sales\Model\OrderRepository; use Magento\Store\Model\StoreManagerInterface; @@ -25,9 +22,8 @@ use Mollie\Payment\Logger\MollieLogger; use Magento\SalesRule\Model\Coupon; use Magento\SalesRule\Model\ResourceModel\Coupon\Usage as CouponUsage; -use Mollie\Payment\Service\Mollie\TransactionDescription; use Mollie\Payment\Service\Order\CancelOrder; -use Mollie\Payment\Service\Order\OrderCommentHistory; +use Mollie\Payment\Service\Order\MethodCode; use Mollie\Payment\Service\Order\Transaction; use Mollie\Payment\Service\Order\Uncancel; @@ -129,10 +125,6 @@ class General extends AbstractHelper * @var OrderRepository */ private $orderRepository; - /** - * @var MathRandom - */ - private $mathRandom; /** * @var Coupon */ @@ -141,14 +133,6 @@ class General extends AbstractHelper * @var CouponUsage */ private $couponUsage; - /** - * @var OrderCommentHistory - */ - private $orderCommentHistory; - /** - * @var OrderManagementInterface - */ - private $orderManagement; /** * @var Config */ @@ -162,45 +146,16 @@ class General extends AbstractHelper */ private $uncancel; - /** - * @var TransactionDescription - */ - private $transactionDescription; - /** * @var CancelOrder */ private $cancelOrder; /** - * @var EncryptorInterface + * @var MethodCode */ - private $encryptor; + private $methodCode; - /** - * General constructor. - * - * @param Context $context - * @param PaymentHelper $paymentHelper - * @param OrderRepository $orderRepository - * @param StoreManagerInterface $storeManager - * @param ResourceConfig $resourceConfig - * @param ModuleListInterface $moduleList - * @param ProductMetadataInterface $metadata - * @param Resolver $resolver - * @param MathRandom $mathRandom - * @param MollieLogger $logger - * @param Coupon $coupon - * @param CouponUsage $couponUsage - * @param OrderCommentHistory $orderCommentHistory - * @param OrderManagementInterface $orderManagement - * @param Config $config - * @param Transaction $transaction - * @param Uncancel $uncancel - * @param TransactionDescription $transactionDescription - * @param CancelOrder $cancelOrder - * @param EncryptorInterface $encryptor - */ public function __construct( Context $context, PaymentHelper $paymentHelper, @@ -210,18 +165,14 @@ public function __construct( ModuleListInterface $moduleList, ProductMetadataInterface $metadata, Resolver $resolver, - MathRandom $mathRandom, MollieLogger $logger, Coupon $coupon, CouponUsage $couponUsage, - OrderCommentHistory $orderCommentHistory, - OrderManagementInterface $orderManagement, Config $config, Transaction $transaction, Uncancel $uncancel, - TransactionDescription $transactionDescription, CancelOrder $cancelOrder, - EncryptorInterface $encryptor + MethodCode $methodCode ) { $this->paymentHelper = $paymentHelper; $this->storeManager = $storeManager; @@ -229,20 +180,16 @@ public function __construct( $this->orderRepository = $orderRepository; $this->urlBuilder = $context->getUrlBuilder(); $this->moduleList = $moduleList; - $this->mathRandom = $mathRandom; $this->metadata = $metadata; $this->resolver = $resolver; $this->logger = $logger; $this->coupon = $coupon; $this->couponUsage = $couponUsage; - $this->orderCommentHistory = $orderCommentHistory; - $this->orderManagement = $orderManagement; $this->config = $config; $this->transaction = $transaction; $this->uncancel = $uncancel; - $this->transactionDescription = $transactionDescription; $this->cancelOrder = $cancelOrder; - $this->encryptor = $encryptor; + $this->methodCode = $methodCode; parent::__construct($context); } @@ -418,16 +365,13 @@ public function disableExtension() * @param OrderInterface $order * * @return string + * + * @deprecated since v2.33.0 + * @see \Mollie\Payment\Service\Order\MethodCode */ public function getMethodCode($order): string { - $method = $order->getPayment()->getMethodInstance()->getCode(); - - if ($method == 'mollie_methods_paymentlink' || strstr($method, 'mollie_methods') === false) { - return ''; - } - - return str_replace('mollie_methods_', '', $method); + return $this->methodCode->execute($order); } /*** diff --git a/Model/Client/Orders.php b/Model/Client/Orders.php index fe796344c80..bdcf8d67fbe 100644 --- a/Model/Client/Orders.php +++ b/Model/Client/Orders.php @@ -36,11 +36,11 @@ 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\MethodCode; use Mollie\Payment\Service\Order\OrderCommentHistory; use Mollie\Payment\Service\Order\PartialInvoice; use Mollie\Payment\Service\Order\ProcessAdjustmentFee; use Mollie\Payment\Service\Order\Transaction; -use Mollie\Payment\Service\OrderLockService; use Mollie\Payment\Service\PaymentToken\PaymentTokenForOrder; /** @@ -148,43 +148,15 @@ class Orders extends AbstractModel */ private $linkTransactionToOrder; - /** - * @var OrderLockService - */ - private $orderLockService; /** * @var ShouldEmailInvoice */ private $shouldEmailInvoice; - /** - * Orders constructor. - * - * @param OrderLines $orderLines - * @param InvoiceSender $invoiceSender - * @param OrderRepository $orderRepository - * @param CheckoutSession $checkoutSession - * @param ManagerInterface $messageManager - * @param Registry $registry - * @param MollieHelper $mollieHelper - * @param ProcessAdjustmentFee $adjustmentFee - * @param OrderCommentHistory $orderCommentHistory - * @param PartialInvoice $partialInvoice - * @param StoreCredit $storeCredit - * @param RefundUsingPayment $refundUsingPayment - * @param Expires $expires - * @param State $orderState - * @param Transaction $transaction - * @param BuildTransaction $buildTransaction - * @param PaymentTokenForOrder $paymentTokenForOrder - * @param ProcessTransaction $processTransaction - * @param \Mollie\Payment\Service\Mollie\MollieApiClient $mollieApiClient - * @param Config $config - * @param EventManager $eventManager - * @param LinkTransactionToOrder $linkTransactionToOrder - * @param OrderLockService $orderLockService - * @param ShouldEmailInvoice $shouldEmailInvoice + * @var MethodCode */ + private $methodCode; + public function __construct( OrderLines $orderLines, InvoiceSender $invoiceSender, @@ -208,8 +180,8 @@ public function __construct( Config $config, EventManager $eventManager, LinkTransactionToOrder $linkTransactionToOrder, - OrderLockService $orderLockService, - ShouldEmailInvoice $shouldEmailInvoice + ShouldEmailInvoice $shouldEmailInvoice, + MethodCode $methodCode ) { $this->orderLines = $orderLines; $this->invoiceSender = $invoiceSender; @@ -233,8 +205,8 @@ public function __construct( $this->mollieApiClient = $mollieApiClient; $this->config = $config; $this->linkTransactionToOrder = $linkTransactionToOrder; - $this->orderLockService = $orderLockService; $this->shouldEmailInvoice = $shouldEmailInvoice; + $this->methodCode = $methodCode; } /** @@ -258,7 +230,7 @@ public function startTransaction(OrderInterface $order, $mollieApi) } $paymentToken = $this->paymentTokenForOrder->execute($order); - $method = $this->mollieHelper->getMethodCode($order); + $method = $this->methodCode->execute($order); $method = str_replace('_vault', '', $method); $orderData = [ 'amount' => $this->mollieHelper->getOrderAmountByOrder($order), @@ -673,7 +645,7 @@ public function createOrderRefund(Order\Creditmemo $creditmemo, Order $order) return $this; } - $methodCode = $this->mollieHelper->getMethodCode($order); + $methodCode = $this->methodCode->execute($order); $methods = ['klarna', 'klarnapaylater', 'klarnasliceit', 'klarnapaynow']; if (!$order->hasShipments() && (in_array($methodCode, $methods))) { $msg = __('Order can only be refunded after Klarna has been captured (after shipment)'); diff --git a/Model/Client/Payments.php b/Model/Client/Payments.php index 80f5a4fecd7..6cb8d125de4 100644 --- a/Model/Client/Payments.php +++ b/Model/Client/Payments.php @@ -24,6 +24,7 @@ use Mollie\Payment\Service\Mollie\TransactionDescription; use Mollie\Payment\Service\Mollie\ValidateMetadata; use Mollie\Payment\Service\Order\BuildTransaction; +use Mollie\Payment\Service\Order\MethodCode; use Mollie\Payment\Service\Order\OrderAmount; use Mollie\Payment\Service\Order\CancelOrder; use Mollie\Payment\Service\Order\OrderCommentHistory; @@ -137,31 +138,11 @@ class Payments extends AbstractModel * @var CanRegisterCaptureNotification */ private $canRegisterCaptureNotification; - /** - * Payments constructor. - * - * @param OrderRepository $orderRepository - * @param CheckoutSession $checkoutSession - * @param MollieHelper $mollieHelper - * @param OrderCommentHistory $orderCommentHistory - * @param BuildTransaction $buildTransaction - * @param DashboardUrl $dashboardUrl - * @param Transaction $transaction - * @param TransactionProcessor $transactionProcessor - * @param OrderAmount $orderAmount - * @param TransactionDescription $transactionDescription - * @param CancelOrder $cancelOrder - * @param PaymentTokenForOrder $paymentTokenForOrder - * @param SendOrderEmails $sendOrderEmails - * @param EventManager $eventManager - * @param LinkTransactionToOrder $linkTransactionToOrder - * @param ProcessTransaction $processTransaction - * @param ValidateMetadata $validateMetadata - * @param SaveAdditionalInformationDetails $saveAdditionalInformationDetails - * @param ExpiredOrderToTransaction $expiredOrderToTransaction - * @param CanRegisterCaptureNotification $canRegisterCaptureNotification + * @var MethodCode */ + private $methodCode; + public function __construct( OrderRepository $orderRepository, CheckoutSession $checkoutSession, @@ -182,7 +163,8 @@ public function __construct( ValidateMetadata $validateMetadata, SaveAdditionalInformationDetails $saveAdditionalInformationDetails, ExpiredOrderToTransaction $expiredOrderToTransaction, - CanRegisterCaptureNotification $canRegisterCaptureNotification + CanRegisterCaptureNotification $canRegisterCaptureNotification, + MethodCode $methodCode ) { $this->orderRepository = $orderRepository; $this->checkoutSession = $checkoutSession; @@ -204,6 +186,7 @@ public function __construct( $this->saveAdditionalInformationDetails = $saveAdditionalInformationDetails; $this->expiredOrderToTransaction = $expiredOrderToTransaction; $this->canRegisterCaptureNotification = $canRegisterCaptureNotification; + $this->methodCode = $methodCode; } /** @@ -225,7 +208,8 @@ public function startTransaction(OrderInterface $order, $mollieApi) } $paymentToken = $this->paymentTokenForOrder->execute($order); - $method = $this->mollieHelper->getMethodCode($order); + $method = $this->methodCode->execute($order); + $paymentData = [ 'amount' => $this->mollieHelper->getOrderAmountByOrder($order), 'description' => $this->transactionDescription->forRegularTransaction($order), diff --git a/Service/Order/MethodCode.php b/Service/Order/MethodCode.php new file mode 100644 index 00000000000..10b2b0b089f --- /dev/null +++ b/Service/Order/MethodCode.php @@ -0,0 +1,43 @@ +getPayment()->getMethodInstance()->getCode(); + + if ($method == 'mollie_methods_paymentlink') { + return $this->paymentLinkMethod($order); + } + + if ($method == 'mollie_methods_paymentlink' || strstr($method, 'mollie_methods') === false) { + return ''; + } + + return str_replace('mollie_methods_', '', $method); + } + + private function paymentLinkMethod(OrderInterface $order): string + { + $additionalInformation = $order->getPayment()->getAdditionalInformation(); + if (!$additionalInformation || !array_key_exists('limited_methods', $additionalInformation)) { + return ''; + } + + if (count($additionalInformation['limited_methods']) !== 1) { + return ''; + } + + return str_replace('mollie_methods_', '', $additionalInformation['limited_methods'][0]); + } +} diff --git a/Test/Integration/Service/Order/MethodCodeTest.php b/Test/Integration/Service/Order/MethodCodeTest.php new file mode 100644 index 00000000000..be04f8de50a --- /dev/null +++ b/Test/Integration/Service/Order/MethodCodeTest.php @@ -0,0 +1,74 @@ +loadOrderById('100000001'); + $order->getPayment()->setMethod('mollie_methods_ideal'); + + $instance = $this->objectManager->create(MethodCode::class); + + $result = $instance->execute($order); + + $this->assertEquals('ideal', $result); + } + + public function testReturnsNothingWhenItsNotAMollieMethod(): void + { + $order = $this->loadOrderById('100000001'); + $order->getPayment()->setMethod('wrong_method'); + + $instance = $this->objectManager->create(MethodCode::class); + + $result = $instance->execute($order); + + $this->assertEquals('', $result); + } + + public function testReturnsNothingWhenPaymentLinkHasMultipleMethods(): void + { + $order = $this->loadOrderById('100000001'); + $order->getPayment()->setMethod('mollie_methods_paymentlink'); + $order->getPayment()->setAdditionalInformation( + 'limited_methods', + ['mollie_methods_ideal', 'mollie_methods_eps'] + ); + + $instance = $this->objectManager->create(MethodCode::class); + + $result = $instance->execute($order); + + $this->assertEquals('', $result); + } + + public function testReturnsPaymentLinkReturnsTheSingleLimitedMethod(): void + { + $order = $this->loadOrderById('100000001'); + $order->getPayment()->setMethod('mollie_methods_paymentlink'); + $order->getPayment()->setAdditionalInformation( + 'limited_methods', + ['mollie_methods_ideal'] + ); + + $instance = $this->objectManager->create(MethodCode::class); + + $result = $instance->execute($order); + + $this->assertEquals('ideal', $result); + } +}