diff --git a/Api/Data/OrderInterface.php b/Api/Data/OrderInterface.php index a9c7072..3f8e190 100644 --- a/Api/Data/OrderInterface.php +++ b/Api/Data/OrderInterface.php @@ -8,6 +8,7 @@ interface OrderInterface extends ExtensibleDataInterface { const ID = 'id'; const PAYMENT_ORDER_ID = 'payment_order_id'; + const PAYMENT_ORDER_ID_PATH = 'payment_order_id_path'; const DESCRIPTION = 'description'; const OPERATION = 'operation'; const INTENT = 'intent'; @@ -45,6 +46,17 @@ public function getPaymentOrderId(); */ public function setPaymentOrderId($paymentOrderId); + /** + * @return string|null + */ + public function getPaymentOrderIdPath(); + + /** + * @param string $paymentIdPath + * @return void + */ + public function setPaymentOrderIdPath($paymentIdPath); + /** * @return string */ diff --git a/Api/Data/QuoteInterface.php b/Api/Data/QuoteInterface.php index 1293a73..a9172b7 100644 --- a/Api/Data/QuoteInterface.php +++ b/Api/Data/QuoteInterface.php @@ -8,6 +8,7 @@ interface QuoteInterface extends ExtensibleDataInterface { const ID = 'id'; const PAYMENT_ORDER_ID = 'payment_order_id'; + const PAYMENT_ORDER_ID_PATH = 'payment_order_id_path'; const DESCRIPTION = 'description'; const OPERATION = 'operation'; const STATE = 'state'; @@ -45,6 +46,17 @@ public function getPaymentOrderId(); */ public function setPaymentOrderId($paymentOrderId); + /** + * @return string|null + */ + public function getPaymentOrderIdPath(); + + /** + * @param string $paymentIdPath + * @return void + */ + public function setPaymentOrderIdPath($paymentIdPath); + /** * @return string */ diff --git a/Gateway/Command/Cancel.php b/Gateway/Command/Cancel.php index 80e05de..44ef228 100644 --- a/Gateway/Command/Cancel.php +++ b/Gateway/Command/Cancel.php @@ -104,10 +104,12 @@ public function execute(array $commandSubject) $transactionObject = new TransactionObject(); $transactionObject->setTransaction($transaction); - $cancelRequest = $this->getRequestService('Paymentorder/Transaction', 'TransactionCancel', $transactionObject); - $cancelRequest->setRequestEndpointVars( - $this->getSwedbankPayPaymentResourceId($paymentOrder->getPaymentOrderId()) + $cancelRequest = $this->getRequestService( + 'Paymentorder/Transaction', + 'TransactionCancel', + $transactionObject ); + $cancelRequest->setPaymentOrderId($paymentOrder->getPaymentOrderIdPath()); /** @var ResponseInterface $cancelResponse */ $cancelResponse = $cancelRequest->send(); diff --git a/Gateway/Command/Capture.php b/Gateway/Command/Capture.php index d1247f4..fe18933 100644 --- a/Gateway/Command/Capture.php +++ b/Gateway/Command/Capture.php @@ -175,9 +175,7 @@ public function execute(array $commandSubject) 'TransactionCapture', $transactionObject ); - $captureRequest->setRequestEndpointVars( - $this->getSwedbankPayPaymentResourceId($paymentOrder->getPaymentOrderId()) - ); + $captureRequest->setPaymentOrderId($paymentOrder->getPaymentOrderIdPath()); /** @var ResponseInterface $captureResponse */ $captureResponse = $captureRequest->send(); diff --git a/Gateway/Command/Refund.php b/Gateway/Command/Refund.php index 1b94ef1..2bc5949 100644 --- a/Gateway/Command/Refund.php +++ b/Gateway/Command/Refund.php @@ -116,9 +116,7 @@ public function execute(array $commandSubject) 'TransactionReversal', $transactionObject ); - $reversalRequest->setRequestEndpointVars( - $this->getSwedbankPayPaymentResourceId($paymentOrder->getPaymentOrderId()) - ); + $reversalRequest->setPaymentOrderId($paymentOrder->getPaymentOrderIdPath()); /** @var ResponseInterface $reversalResponse */ $reversalResponse = $reversalRequest->send(); diff --git a/Helper/Factory/PayerFactory.php b/Helper/Factory/PayerFactory.php index 2757ca1..7c7c4c7 100644 --- a/Helper/Factory/PayerFactory.php +++ b/Helper/Factory/PayerFactory.php @@ -34,7 +34,6 @@ public function create(Quote $quote) $payer = new PaymentorderPayer(); $payer->setEmail($quote->getShippingAddress()->getEmail()) - ->setMsisdn($quote->getShippingAddress()->getTelephone()) ->setShippingAddress($shippingAddress); return $payer; diff --git a/Helper/PaymentData.php b/Helper/PaymentData.php index 9c8829c..6800dc9 100644 --- a/Helper/PaymentData.php +++ b/Helper/PaymentData.php @@ -93,7 +93,10 @@ public function getByPaymentOrderId($paymentOrderId) try { $paymentData = $this->paymentOrderRepo->getByPaymentOrderId($paymentOrderId); } catch (NoSuchEntityException $e) { - $this->logger->debug(sprintf('SwedbankPay Order not found with ID # %s', $paymentOrderId)); + $this->logger->debug(sprintf( + 'No SwedbankPay Order record has been created yet with Payment Order ID: %s', + $paymentOrderId + )); } if ($paymentData instanceof PaymentOrderInterface) { @@ -103,14 +106,20 @@ public function getByPaymentOrderId($paymentOrderId) try { $paymentData = $this->paymentQuoteRepo->getByPaymentOrderId($paymentOrderId); } catch (NoSuchEntityException $e) { - $this->logger->debug(sprintf('SwedbankPay Quote not found with ID # %s', $paymentOrderId)); + $this->logger->debug(sprintf( + 'SwedbankPay Quote not found with Payment Order ID: %s', + $paymentOrderId + )); } if ($paymentData instanceof PaymentQuoteInterface) { return $paymentData; } - $errorMessage = sprintf("Unable to find a SwedbankPay payment matching Payment ID:\n%s", $paymentOrderId); + $errorMessage = sprintf( + "Unable to find a SwedbankPay payment matching Payment Order ID:\n%s", + $paymentOrderId + ); $this->logger->error( $errorMessage @@ -151,18 +160,20 @@ public function update($payment) */ public function updateRemainingAmounts($command, $amount, $order) { + $amountInSubUnit = (int) round($amount * 100); + switch ($command) { case 'capture': - $order->setRemainingCapturingAmount($order->getRemainingCapturingAmount() - ($amount * 100)); + $order->setRemainingCapturingAmount($order->getRemainingCapturingAmount() - $amountInSubUnit); $order->setRemainingCancellationAmount($order->getRemainingCapturingAmount()); - $order->setRemainingReversalAmount($order->getRemainingReversalAmount() + ($amount * 100)); + $order->setRemainingReversalAmount($order->getRemainingReversalAmount() + $amountInSubUnit); break; case 'cancel': - $order->setRemainingCancellationAmount($order->getRemainingCancellationAmount() - ($amount * 100)); + $order->setRemainingCancellationAmount($order->getRemainingCancellationAmount() - $amountInSubUnit); $order->setRemainingCapturingAmount($order->getRemainingCancellationAmount()); break; case 'refund': - $order->setRemainingReversalAmount($order->getRemainingReversalAmount() - ($amount * 100)); + $order->setRemainingReversalAmount($order->getRemainingReversalAmount() - $amountInSubUnit); break; } diff --git a/Helper/Paymentorder.php b/Helper/Paymentorder.php index bc71c84..81180fd 100644 --- a/Helper/Paymentorder.php +++ b/Helper/Paymentorder.php @@ -437,6 +437,7 @@ public function saveQuoteToDB($response) } $quote->setPaymentOrderId($this->getSwedbankPayPaymentorderId($response['payment_order']['id'])); + $quote->setPaymentOrderIdPath($response['payment_order']['id']); $quote->setDescription($response['payment_order']['description']); $quote->setOperation($response['payment_order']['operation']); $quote->setState($response['payment_order']['state']); diff --git a/Model/Order.php b/Model/Order.php index 180df87..ef55c91 100644 --- a/Model/Order.php +++ b/Model/Order.php @@ -53,6 +53,23 @@ public function setPaymentOrderId($paymentOrderId) $this->setData(self::PAYMENT_ORDER_ID, $paymentOrderId); } + /** + * @return string|null + */ + public function getPaymentOrderIdPath() + { + return $this->_getData(self::PAYMENT_ORDER_ID_PATH); + } + + /** + * @param string $paymentIdPath + * @return void + */ + public function setPaymentOrderIdPath($paymentIdPath) + { + $this->setData(self::PAYMENT_ORDER_ID_PATH, $paymentIdPath); + } + /** * @return string */ diff --git a/Model/Quote.php b/Model/Quote.php index 5697676..fb9e02a 100644 --- a/Model/Quote.php +++ b/Model/Quote.php @@ -53,6 +53,23 @@ public function setPaymentOrderId($paymentOrderId) $this->setData(self::PAYMENT_ORDER_ID, $paymentOrderId); } + /** + * @return string|null + */ + public function getPaymentOrderIdPath() + { + return $this->_getData(self::PAYMENT_ORDER_ID_PATH); + } + + /** + * @param string $paymentIdPath + * @return void + */ + public function setPaymentOrderIdPath($paymentIdPath) + { + $this->setData(self::PAYMENT_ORDER_ID_PATH, $paymentIdPath); + } + /** * @return string */ diff --git a/Observer/AfterCheckoutSubmitObserver.php b/Observer/AfterCheckoutSubmitObserver.php index 69fb839..6ca81da 100644 --- a/Observer/AfterCheckoutSubmitObserver.php +++ b/Observer/AfterCheckoutSubmitObserver.php @@ -3,10 +3,10 @@ namespace SwedbankPay\Checkout\Observer; use Exception; -use Magento\Framework\Exception\InputException; -use Magento\Framework\Exception\AlreadyExistsException; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; +use Magento\Framework\Exception\AlreadyExistsException; +use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Api\Data\OrderPaymentInterface; @@ -17,6 +17,7 @@ use SwedbankPay\Checkout\Api\Data\OrderInterface as PaymentOrderInterface; use SwedbankPay\Checkout\Helper\Config as ConfigHelper; use SwedbankPay\Checkout\Helper\PaymentData; +use SwedbankPay\Checkout\Model\ConsumerSession; use SwedbankPay\Checkout\Model\Ui\ConfigProvider; use SwedbankPay\Core\Helper\Order as OrderHelper; use SwedbankPay\Core\Logger\Logger; @@ -42,6 +43,11 @@ class AfterCheckoutSubmitObserver implements ObserverInterface */ protected $clientService; + /** + * @var ConsumerSession + */ + protected $consumerSession; + /** * @var OrderHelper */ @@ -61,6 +67,7 @@ public function __construct( ConfigHelper $configHelper, PaymentData $paymentData, ClientService $clientService, + ConsumerSession $consumerSession, OrderHelper $orderHelper, OrderRepository $orderRepository, Logger $logger @@ -68,6 +75,7 @@ public function __construct( $this->configHelper = $configHelper; $this->paymentData = $paymentData; $this->clientService = $clientService; + $this->consumerSession = $consumerSession; $this->orderHelper = $orderHelper; $this->orderRepository = $orderRepository; $this->logger = $logger; @@ -111,13 +119,16 @@ public function execute(Observer $observer) /** @var GetCurrentPayment $currentPaymentRequest */ $currentPaymentRequest = $this->clientService->init('Paymentorder', 'GetCurrentPayment'); - $currentPaymentRequest->setRequestEndpointVars($paymentData->getPaymentOrderId()); + $this->logger->debug('Payment Order ID: '. $paymentData->getPaymentOrderIdPath()); + $currentPaymentRequest->setPaymentOrderId($paymentData->getPaymentOrderIdPath()); /** @var GetCurrentPaymentInterface $currentPayment */ $currentPayment = $currentPaymentRequest->send()->getResponseResource(); $paymentData->setIntent($currentPayment->getPayment()->getIntent()); $this->paymentData->update($paymentData); + $this->consumerSession->setViewOperation(false); + if ($paymentData->getIntent() == 'Sale') { $this->saveTransactionNumber($order, $currentPayment); diff --git a/Plugin/OrderRepositoryPlugin.php b/Plugin/OrderRepositoryPlugin.php index b344e98..add51a9 100644 --- a/Plugin/OrderRepositoryPlugin.php +++ b/Plugin/OrderRepositoryPlugin.php @@ -96,7 +96,11 @@ public function afterSave( return $mageOrder; } } catch (Exception $e) { - $this->logger->debug(sprintf('SwedbankPay Order not found with ID # %s', $mageOrder->getEntityId())); + $this->logger->debug(sprintf( + 'No SwedbankPay Order record has been created yet with ID # %s', + $mageOrder->getEntityId() + )); + $this->logger->debug('Creating new SwedbankPay Order...'); } /** @var \SwedbankPay\Checkout\Model\Order $swedbankPayOrder */ @@ -104,6 +108,7 @@ public function afterSave( $swedbankPayOrder->setState($swedbankPayQuote->getState()); $swedbankPayOrder->setPaymentOrderId($swedbankPayQuote->getPaymentOrderId()); + $swedbankPayOrder->setPaymentOrderIdPath($swedbankPayQuote->getPaymentOrderIdPath()); $swedbankPayOrder->setCreatedAt($swedbankPayQuote->getCreatedAt()); $swedbankPayOrder->setUpdatedAt($swedbankPayQuote->getUpdatedAt()); $swedbankPayOrder->setOperation($swedbankPayQuote->getOperation()); diff --git a/Plugin/QuoteRepositoryPlugin.php b/Plugin/QuoteRepositoryPlugin.php index 5e57d1f..877fb9c 100644 --- a/Plugin/QuoteRepositoryPlugin.php +++ b/Plugin/QuoteRepositoryPlugin.php @@ -115,9 +115,13 @@ public function aroundSave( $swedbankPayQuote->setRemainingReversalAmount(0); $this->quoteRepository->save($swedbankPayQuote); - } catch (NoSuchEntityException $e) { - $this->logger->debug(sprintf('SwedbankPay Quote not found with ID # %s', $quote->getId())); + $this->logger->debug(sprintf( + 'No SwedbankPay Quote record has been created yet with ID # %s', + $quote->getId() + )); + + $this->logger->debug(sprintf('SwedbankPay Quote update skipped!')); } return $returnValue; @@ -130,11 +134,13 @@ public function aroundSave( */ public function updatePaymentorder(MageQuote $mageQuote, SwedbankPayQuote $swedbankPayQuote) { + $quoteTotal = (int) round($mageQuote->getGrandTotal() * 100); + $this->logger->debug('UpdateOrder request is called'); $this->logger->debug('SwedbankPayQuote Total: ' . $swedbankPayQuote->getAmount()); - $this->logger->debug('Quote Grand Total: ' . $mageQuote->getGrandTotal() * 100); + $this->logger->debug('Quote Grand Total: ' . $quoteTotal); - if ($swedbankPayQuote->getAmount() == ($mageQuote->getGrandTotal() * 100)) { + if ($swedbankPayQuote->getAmount() == $quoteTotal) { $this->logger->debug('UpdateOrder operation is skipped as the amount is unchanged'); return; } @@ -147,7 +153,7 @@ public function updatePaymentorder(MageQuote $mageQuote, SwedbankPayQuote $swedb $paymentOrderObject = $this->paymentorder->createPaymentorderUpdateObject($mageQuote); $updateRequest = $this->service->init('Paymentorder', 'updateOrder', $paymentOrderObject); - $updateRequest->setRequestEndpointVars($swedbankPayQuote->getPaymentOrderId()); + $updateRequest->setPaymentOrderId($swedbankPayQuote->getPaymentOrderIdPath()); $updateRequest->send(); } diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 20421cc..7e69a8b 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -49,6 +49,35 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con ); } + $swedbankPayQuoteTable = 'swedbank_pay_quotes'; + $swedbankPayOrderTable = 'swedbank_pay_orders'; + + if (version_compare($context->getVersion(), "1.3.0", "<")) { + // Swedbank Pay Quotes table + $setup->getConnection() + ->addColumn( + $setup->getTable($swedbankPayQuoteTable), + 'payment_order_id_path', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 255, + 'comment' =>'SwedbankPay Payment Order ID Path' + ] + ); + + // Swedbank Pay Orders table + $setup->getConnection() + ->addColumn( + $setup->getTable($swedbankPayOrderTable), + 'payment_order_id_path', + [ + 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, + 'length' => 255, + 'comment' =>'SwedbankPay Payment Order ID Path' + ] + ); + } + $setup->endSetup(); } } diff --git a/Test/Unit/PaymentDataTest.php b/Test/Unit/PaymentDataTest.php index e2bb923..c2ecff4 100644 --- a/Test/Unit/PaymentDataTest.php +++ b/Test/Unit/PaymentDataTest.php @@ -67,7 +67,7 @@ public function testGetByPaymentOrderIdThrowsException() $this->expectException(NoSuchEntityException::class); $this->expectExceptionMessage( - sprintf("Unable to find a SwedbankPay payment matching Payment ID:\n%s", $paymentOrderId) + sprintf("Unable to find a SwedbankPay payment matching Payment Order ID:\n%s", $paymentOrderId) ); $this->paymentData->getByPaymentOrderId($paymentOrderId); diff --git a/Test/Unit/QuoteRepositoryPluginTest.php b/Test/Unit/QuoteRepositoryPluginTest.php index 4b84264..0bbc7f2 100644 --- a/Test/Unit/QuoteRepositoryPluginTest.php +++ b/Test/Unit/QuoteRepositoryPluginTest.php @@ -178,7 +178,7 @@ public function testUpdateOrderRequestWasSent() ->willReturn($paymentOrderId); $this->service->expects($this->once())->method('init')->willReturn($this->updateRequest); - $this->updateRequest->expects($this->once())->method('setRequestEndpointVars'); + $this->updateRequest->expects($this->once())->method('setPaymentOrderId'); $this->updateRequest->expects($this->once())->method('send'); $this->quoteRepositoryPlugin->updatePaymentorder($this->mageQuote, $this->swedbankPayQuote); diff --git a/composer.json b/composer.json index bd232f7..97b2b49 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "magento/module-customer": "^100.1||^101.0||^102.0", "magento/module-sales": "^100.1||^101.0||^102.0", "magento/module-tax": "^100.0", - "swedbank-pay/magento2-core": "^1.2" + "swedbank-pay/magento2-core": "^1.5" }, "repositories": { "magento": { diff --git a/etc/module.xml b/etc/module.xml index dc8b07d..e70cf09 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/view/frontend/web/js/view/payment/method-renderer/payment-menu.js b/view/frontend/web/js/view/payment/method-renderer/payment-menu.js index 13a149e..23933fa 100644 --- a/view/frontend/web/js/view/payment/method-renderer/payment-menu.js +++ b/view/frontend/web/js/view/payment/method-renderer/payment-menu.js @@ -116,6 +116,7 @@ define([ script.onload = function(){ if(self.paymentScript == scriptSrc) { self.swedbankPaySetupHostedView(); + $('#swedbank_pay_checkout').click(); } };