From 36fc99ec7f67106a7fc93440351aad8bb2db3cba Mon Sep 17 00:00:00 2001
From: andrii-onufriichuk
 <117644505+andrii-onufriichuk@users.noreply.github.com>
Date: Mon, 1 Jul 2024 14:39:55 +0300
Subject: [PATCH] Supporting changes to allow paypal express in cart on Hyva
 themes (#140)

---
 Model/ConfigProvider.php                      |  3 +++
 Model/ProcessOrder/Cancel.php                 | 22 ++++++++++++++-----
 composer.json                                 |  2 +-
 .../payment/method-renderer/rvvup-method.js   |  6 ++---
 4 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/Model/ConfigProvider.php b/Model/ConfigProvider.php
index 9c3b8c90..a0e3e9c6 100644
--- a/Model/ConfigProvider.php
+++ b/Model/ConfigProvider.php
@@ -140,6 +140,9 @@ public function getConfig()
 
                 $items[Method::PAYMENT_TITLE_PREFIX . $method['name']]['use_place_order_styling'] = (bool)
                     $this->config->getPaypalBlockStyling(ConfigInterface::XML_PATH_USE_PLACE_ORDER_STYLING);
+
+                $items[Method::PAYMENT_TITLE_PREFIX . $method['name']]['is_express'] =
+                    $this->isExpressPaymentCart($quote);
             }
         }
 
diff --git a/Model/ProcessOrder/Cancel.php b/Model/ProcessOrder/Cancel.php
index 8dd4cc32..91ade6ac 100644
--- a/Model/ProcessOrder/Cancel.php
+++ b/Model/ProcessOrder/Cancel.php
@@ -5,6 +5,7 @@
 namespace Rvvup\Payments\Model\ProcessOrder;
 
 use Magento\Framework\Event\ManagerInterface as EventManager;
+use Magento\Framework\Exception\LocalizedException;
 use Magento\Sales\Api\Data\OrderInterface;
 use Magento\Sales\Api\OrderManagementInterface;
 use Psr\Log\LoggerInterface;
@@ -86,7 +87,8 @@ public function __construct(
     public function execute(OrderInterface $order, array $rvvupData, string $origin): ProcessOrderResultInterface
     {
         $this->validateIdExists($rvvupData);
-        $this->validateStatusAllowed($rvvupData['payments'][0], $this->allowedStatuses);
+        $payment = $rvvupData['payments'][0];
+        $this->validateStatusAllowed($payment, $this->allowedStatuses);
         $this->validateOrderPayment($order);
 
         if (!$order->canCancel()) {
@@ -116,8 +118,8 @@ public function execute(OrderInterface $order, array $rvvupData, string $origin)
             'payment_process_type' => self::TYPE,
             'payment_process_result' => $result,
             'event_message' => $result
-                ? 'Rvvup Payment has status ' . $rvvupData['payments'][0]['status']. '.'
-                : 'Rvvup Payment has status ' . $rvvupData['payments'][0]['status']
+                ? 'Rvvup Payment has status ' . $payment['status']. '.'
+                : 'Rvvup Payment has status ' . $payment['status']
                 . ' but failed to cancel the Magento order.',
             'order_id' => $order->getEntityId(),
             'rvvup_id' => $rvvupData['id']
@@ -128,9 +130,19 @@ public function execute(OrderInterface $order, array $rvvupData, string $origin)
         $processOrderResult = $this->processOrderResultFactory->create();
         $processOrderResult->setResultType(ProcessOrderResultInterface::RESULT_TYPE_ERROR);
         $processOrderResult->setRedirectPath(In::FAILURE);
-        $message = 'Payment ' . ucfirst(mb_strtolower($rvvupData['payments'][0]['status']));
-        $processOrderResult->setCustomerMessage($message);
+        if ($order->getPayment()->getMethod() === Method::PAYMENT_TITLE_PREFIX . 'PAYPAL') {
+            if ($payment['status'] == 'DECLINED') {
+                if ($payment['declineReason'] == 'INVALID_AUTHORIZATION') {
+                    $message = 'Total order value exceeds amount authorised on PayPal, ' . PHP_EOL;
+                    $message .= 'please re-authorise with PayPal or choose another payment method';
+                    $processOrderResult->setCustomerMessage($message);
+                    return $processOrderResult;
+                }
+            }
+        }
 
+        $message = 'Payment ' . ucfirst(mb_strtolower($payment['status']));
+        $processOrderResult->setCustomerMessage($message);
         return $processOrderResult;
     }
 
diff --git a/composer.json b/composer.json
index c4a5d20e..e3627f6e 100644
--- a/composer.json
+++ b/composer.json
@@ -11,7 +11,7 @@
         "guzzlehttp/guzzle": ">=6",
         "magento/module-catalog": "^103.0 || ^104.0",
         "magento/module-grouped-product": ">=100.1",
-        "rvvup/sdk": "1.1.0",
+        "rvvup/sdk": "1.2.3",
         "ext-json": "*",
         "php": "^7.3 || ^8.0"
     },
diff --git a/view/frontend/web/js/view/payment/method-renderer/rvvup-method.js b/view/frontend/web/js/view/payment/method-renderer/rvvup-method.js
index 51d5735a..d55e272f 100644
--- a/view/frontend/web/js/view/payment/method-renderer/rvvup-method.js
+++ b/view/frontend/web/js/view/payment/method-renderer/rvvup-method.js
@@ -137,7 +137,7 @@ define([
                 $(document).on('click', 'a#' + this.getCancelExpressPaymentLinkId(), (e) => {
                     e.preventDefault();
 
-                    if (!rvvupMethodProperties.getIsExpressPaymentCheckout()) {
+                    if (!window.checkoutConfig.payment[this.index].is_express) {
                         return;
                     }
 
@@ -467,7 +467,7 @@ define([
              * @return {boolean}
              */
             shouldDisplayPayPalButton() {
-                return this.isPayPalComponent() && !rvvupMethodProperties.getIsExpressPaymentCheckout();
+                return this.isPayPalComponent() && !window.checkoutConfig.payment[this.index].is_express;
             },
 
             /**
@@ -476,7 +476,7 @@ define([
              * @return {false}
              */
             shouldDisplayCancelExpressPaymentLink() {
-                return rvvupMethodProperties.getIsExpressPaymentCheckout();
+                return window.checkoutConfig.payment[this.index].is_express;
             },
 
             /**