From 68e5e046bcaea75cfb925247f17c25b9d1c41c9f Mon Sep 17 00:00:00 2001
From: root
 <root@visualcommercestore.e2yfrfar2jdefo32kpgqwzckpg.bx.internal.cloudapp.net>
Date: Tue, 8 Oct 2024 06:21:30 +0000
Subject: [PATCH] Latest 2.4.12 with discount

---
 composer.json                                 |   2 +-
 composer.lock                                 |  14 +-
 vendor/composer/installed.json                |  14 +-
 vendor/composer/installed.php                 |  18 +--
 vendor/payuindia/payu/Helper/Payu.php         | 153 ++++++++++--------
 vendor/payuindia/payu/composer.json           |   2 +-
 vendor/payuindia/payu/etc/db_schema.xml       |   6 +
 vendor/payuindia/payu/etc/module.xml          |   2 +-
 .../payment/method-renderer/payu-method.js    |  10 +-
 9 files changed, 125 insertions(+), 96 deletions(-)
 create mode 100644 vendor/payuindia/payu/etc/db_schema.xml

diff --git a/composer.json b/composer.json
index 79b12b4..dbc5654 100644
--- a/composer.json
+++ b/composer.json
@@ -1,5 +1,5 @@
 {
     "require": {
-        "payuindia/payu": "2.4.6"
+        "payuindia/payu": "^2.4"
     }
 }
diff --git a/composer.lock b/composer.lock
index 4678c7f..3386fd2 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,20 +4,20 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "aece4cfa3f6fb384fe7fcbd2786c68c3",
+    "content-hash": "664527011e9fe38cbff5b1d489193e9f",
     "packages": [
         {
             "name": "payuindia/payu",
-            "version": "2.4.6",
+            "version": "2.4.12",
             "source": {
                 "type": "git",
                 "url": "https://github.com/payu-india/magento_v2.4.2.git",
-                "reference": "bc472bcebb29a8fba297498ba36c675cf0b78bbd"
+                "reference": "b372bf7cd6a1f2ede064993a36b2490ec0cae3ce"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/payu-india/magento_v2.4.2/zipball/bc472bcebb29a8fba297498ba36c675cf0b78bbd",
-                "reference": "bc472bcebb29a8fba297498ba36c675cf0b78bbd",
+                "url": "https://api.github.com/repos/payu-india/magento_v2.4.2/zipball/b372bf7cd6a1f2ede064993a36b2490ec0cae3ce",
+                "reference": "b372bf7cd6a1f2ede064993a36b2490ec0cae3ce",
                 "shasum": ""
             },
             "type": "magento2-module",
@@ -50,9 +50,9 @@
             ],
             "support": {
                 "issues": "https://github.com/payu-india/magento_v2.4.2/issues",
-                "source": "https://github.com/payu-india/magento_v2.4.2/tree/v2.4.6"
+                "source": "https://github.com/payu-india/magento_v2.4.2/tree/v2.4.12"
             },
-            "time": "2024-08-12T11:59:40+00:00"
+            "time": "2024-10-08T06:12:17+00:00"
         }
     ],
     "packages-dev": [],
diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json
index 73d688f..31bdf1e 100644
--- a/vendor/composer/installed.json
+++ b/vendor/composer/installed.json
@@ -2,20 +2,20 @@
     "packages": [
         {
             "name": "payuindia/payu",
-            "version": "2.4.6",
-            "version_normalized": "2.4.6.0",
+            "version": "2.4.12",
+            "version_normalized": "2.4.12.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/payu-india/magento_v2.4.2.git",
-                "reference": "bc472bcebb29a8fba297498ba36c675cf0b78bbd"
+                "reference": "b372bf7cd6a1f2ede064993a36b2490ec0cae3ce"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/payu-india/magento_v2.4.2/zipball/bc472bcebb29a8fba297498ba36c675cf0b78bbd",
-                "reference": "bc472bcebb29a8fba297498ba36c675cf0b78bbd",
+                "url": "https://api.github.com/repos/payu-india/magento_v2.4.2/zipball/b372bf7cd6a1f2ede064993a36b2490ec0cae3ce",
+                "reference": "b372bf7cd6a1f2ede064993a36b2490ec0cae3ce",
                 "shasum": ""
             },
-            "time": "2024-08-12T11:59:40+00:00",
+            "time": "2024-10-08T06:12:17+00:00",
             "type": "magento2-module",
             "installation-source": "dist",
             "autoload": {
@@ -47,7 +47,7 @@
             ],
             "support": {
                 "issues": "https://github.com/payu-india/magento_v2.4.2/issues",
-                "source": "https://github.com/payu-india/magento_v2.4.2/tree/v2.4.6"
+                "source": "https://github.com/payu-india/magento_v2.4.2/tree/v2.4.12"
             },
             "install-path": "../payuindia/payu"
         }
diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php
index 937ca99..52f12e2 100644
--- a/vendor/composer/installed.php
+++ b/vendor/composer/installed.php
@@ -1,9 +1,9 @@
 <?php return array(
     'root' => array(
         'name' => '__root__',
-        'pretty_version' => '1.0.0+no-version-set',
-        'version' => '1.0.0.0',
-        'reference' => null,
+        'pretty_version' => 'dev-master',
+        'version' => 'dev-master',
+        'reference' => 'f83a58e4e40f53614e1564b539a5fd66198bf03f',
         'type' => 'library',
         'install_path' => __DIR__ . '/../../',
         'aliases' => array(),
@@ -11,18 +11,18 @@
     ),
     'versions' => array(
         '__root__' => array(
-            'pretty_version' => '1.0.0+no-version-set',
-            'version' => '1.0.0.0',
-            'reference' => null,
+            'pretty_version' => 'dev-master',
+            'version' => 'dev-master',
+            'reference' => 'f83a58e4e40f53614e1564b539a5fd66198bf03f',
             'type' => 'library',
             'install_path' => __DIR__ . '/../../',
             'aliases' => array(),
             'dev_requirement' => false,
         ),
         'payuindia/payu' => array(
-            'pretty_version' => '2.4.6',
-            'version' => '2.4.6.0',
-            'reference' => 'bc472bcebb29a8fba297498ba36c675cf0b78bbd',
+            'pretty_version' => '2.4.12',
+            'version' => '2.4.12.0',
+            'reference' => 'b372bf7cd6a1f2ede064993a36b2490ec0cae3ce',
             'type' => 'magento2-module',
             'install_path' => __DIR__ . '/../payuindia/payu',
             'aliases' => array(),
diff --git a/vendor/payuindia/payu/Helper/Payu.php b/vendor/payuindia/payu/Helper/Payu.php
index 1a3dcd3..f72abd0 100644
--- a/vendor/payuindia/payu/Helper/Payu.php
+++ b/vendor/payuindia/payu/Helper/Payu.php
@@ -49,7 +49,7 @@ class Payu extends AbstractHelper
     ) {
         $this->session = $session;
         $this->quote = $quote;
-        $this->order =  $order;
+        $this->order = $order;
         $this->quoteManagement = $quoteManagement;
         $this->cartManagement = $cartManagement;
         $this->customerSession = $customerSession;
@@ -86,34 +86,34 @@ class Payu extends AbstractHelper
     {
         $quote = $this->session->getQuote();
 
-        if($this->scopeConfig->getValue('payment/payu/paymentaction')=='expresscheckout'){
+        if ($this->scopeConfig->getValue('payment/payu/paymentaction') == 'expresscheckout') {
             $store = $this->_storeManager->getStore();
             $websiteId = $this->_storeManager->getStore()->getWebsiteId();
-            $customer=$this->customerFactory->create();
+            $customer = $this->customerFactory->create();
             $customer->setWebsiteId($websiteId);
 
-            if($this->customerSession->isLoggedIn()) {
+            if ($this->customerSession->isLoggedIn()) {
                 $email = $this->customerSession->getCustomer()->getEmail();
                 $fname = $this->customerSession->getCustomer()->getFirstname() ?? '';
                 $lname = $this->customerSession->getCustomer()->getLastname() ?? '';
-            }else{
-                $email = 'guestuser'.uniqid().time().'@gmail.com';
+            } else {
+                $email = 'guestuser' . uniqid() . time() . '@gmail.com';
                 $fname = 'fname';
                 $lname = 'lname';
             }
             $customer->loadByEmail($email);
-            if(!$customer->getEntityId()){
+            if (!$customer->getEntityId()) {
                 $quote->setCustomerEmail($email);
                 $quote->setCustomerIsGuest(true);
-            }else{
-                $customer= $this->customerRepository->getById($customer->getEntityId());
+            } else {
+                $customer = $this->customerRepository->getById($customer->getEntityId());
                 $quote->assignCustomer($customer);
             }
-            $address=$quote->getBillingAddress()->getData();
+            $address = $quote->getBillingAddress()->getData();
 
-            $address  = [
-                'firstname'    => 'guest',
-                'lastname'     => 'user',
+            $address = [
+                'firstname' => 'guest',
+                'lastname' => 'user',
                 'street' => 'street',
                 'city' => 'city',
                 'country_id' => 'US',
@@ -131,7 +131,7 @@ class Payu extends AbstractHelper
 
             $activeCarriers = $this->getShippingMethods();
 
-            $shippingAddress=$quote->getShippingAddress();
+            $shippingAddress = $quote->getShippingAddress();
             $shippingAddress->setCollectShippingRates(true)
                 ->collectShippingRates()
                 ->setShippingMethod($activeCarriers[0]['value'][0]['value']);
@@ -141,8 +141,8 @@ class Payu extends AbstractHelper
             $quote->save();
             $quote->getPayment()->importData(['method' => 'payu']);
             $quote->collectTotals()->save();
-        }else{
-            if($this->customerSession->isLoggedIn()) {
+        } else {
+            if ($this->customerSession->isLoggedIn()) {
                 $email = $this->customerSession->getCustomer()->getEmail();
                 $fname = $this->customerSession->getCustomer()->getFirstname() ?? '';
                 $lname = $this->customerSession->getCustomer()->getLastname() ?? '';
@@ -165,18 +165,19 @@ class Payu extends AbstractHelper
         $m2order->setTxnid($txnId)->save();
         $increment_id = $m2order->getRealOrderId();
         $m2order->setCanSendNewEmailFlag(false)->save();
-        if($m2order->getEntityId()){
-            $result['order_id']= $m2order->getRealOrderId();
-        }else{
-            $result=['error'=>1,'msg'=>'erro message'];
+        if ($m2order->getEntityId()) {
+            $result['order_id'] = $m2order->getRealOrderId();
+        } else {
+            $result = ['error' => 1, 'msg' => 'erro message'];
         }
         return $result;
     }
 
-    public function getShippingMethods() {
+    public function getShippingMethods()
+    {
         $activeCarriers = $this->shipconfig->getActiveCarriers();
 
-        foreach($activeCarriers as $carrierCode => $carrierModel) {
+        foreach ($activeCarriers as $carrierCode => $carrierModel) {
             $options = array();
 
             if ($carrierMethods = $carrierModel->getAllowedMethods()) {
@@ -185,7 +186,7 @@ class Payu extends AbstractHelper
                     $options[] = array('value' => $code, 'label' => $method);
                 }
                 $carrierTitle = $this->scopeConfig
-                    ->getValue('carriers/'.$carrierCode.'/title');
+                    ->getValue('carriers/' . $carrierCode . '/title');
             }
 
             $methods[] = array('value' => $options, 'label' => $carrierTitle);
@@ -194,87 +195,109 @@ class Payu extends AbstractHelper
         return $methods;
     }
 
-    public function getConfigData($value)
+    public function getConfigData($value, $storeId = null)
     {
+        if ($storeId === null) {
+            $storeId = $this->_storeManager->getStore()->getId();
+        }
         return $this->scopeConfig->getValue(
             'payment/payu/' . $value,
-            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
+            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
+            $storeId
         );
     }
 
-    public function updateOrderFromResponse($order,$params)
+    public function updateOrderFromResponse($order, $params)
     {
-        if(isset($params['offer']) && isset($params['offer_availed']) && isset($params['transaction_offer']))
-        {
+        if (isset($params['offer']) && isset($params['offer_availed']) && isset($params['transaction_offer'])) {
             $offerArr = $params['transaction_offer'];
             if (!is_array($offerArr)) {
                 $offerArr = json_decode($offerArr, true);
             }
-            if(isset($offerArr['offer_data'])){
-                $des=$offerArr['offer_data'];
-                $description=$des[0];
-                $discountDescription="Title - ".$description['offer_title']." | Offer Key - ".$description['offer_key']." |  Type - ".$description['offer_type'];
-                $customDiscount=$offerArr['discount_data']['total_discount'];
-                if($customDiscount > 0){
-                    $this->setDiscount($order,$customDiscount,$discountDescription);
+            if (isset($offerArr['offer_data'])) {
+                $des = $offerArr['offer_data'];
+                $description = $des[0];
+                $discountDescription = "Title - " . $description['offer_title'] . " | Offer Key - " . $description['offer_key'] . " |  Type - " . $description['offer_type'];
+                $customDiscount = $offerArr['discount_data']['total_discount'];
+                if ($customDiscount > 0) {
+                    $this->setDiscount($order, $customDiscount, $discountDescription);
                 }
             }
 
-        }else{
-            if(isset($params['disc']) && $params['disc']>0)
-            {
-                $discountDescription='Payu Offer';
-                $customDiscount=$params['disc'];
-                $this->setDiscount($order,$customDiscount,$discountDescription);
+        } else {
+            if (isset($params['disc']) && $params['disc'] > 0) {
+                $discountDescription = 'Payu Offer';
+                $customDiscount = $params['disc'];
+                $this->setDiscount($order, $customDiscount, $discountDescription);
             }
         }
 
     }
 
-    public function setDiscount($order,$customDiscount,$discountDescription)
+    public function setDiscount($order, $customDiscount, $discountDescription)
     {
-        $total=$order->getBaseSubtotal();
-        $order->setDiscountAmount($customDiscount);
-        $order->setBaseDiscountAmount($customDiscount);
-        $order->setBaseGrandTotal($order->getBaseGrandTotal()-$customDiscount);
-        $order->setGrandTotal($order->getGrandTotal()-$customDiscount);
-        $order->setDiscountDescription($discountDescription);
+        $total = $order->getGrandTotal();
+        $existingDiscount = $order->getDiscountAmount();
+        $newDiscount = $existingDiscount - $customDiscount; // Adding the custom discount
+        $order->setDiscountAmount($newDiscount);
+        $order->setBaseDiscountAmount($newDiscount);
+        $order->setGrandTotal($order->getGrandTotal() - $customDiscount);
+        $order->setBaseGrandTotal($order->getBaseGrandTotal() - $customDiscount);
+        $order->setDiscountDescription($order->getDiscountDescription() . '|||payu :-' . $discountDescription);
         $shippingAddress = $order->getShippingAddress();
         if ($shippingAddress) {
             $shippingAddress->setDiscountAmount($customDiscount);
             $shippingAddress->setDiscountDescription($discountDescription);
             $shippingAddress->setBaseDiscountAmount($customDiscount);
         }
-        $orderBillingAddress = $order->getBillingAddress();
-        $orderBillingAddress->setDiscountAmount($customDiscount);
-        $orderBillingAddress->setDiscountDescription($discountDescription);
-        $orderBillingAddress->setBaseDiscountAmount($customDiscount);
+        // Apply discount to billing address if exists
+        $billingAddress = $order->getBillingAddress();
+        if ($billingAddress) {
+            $billingAddress->setDiscountAmount($customDiscount);
+            $billingAddress->setDiscountDescription($discountDescription);
+            $billingAddress->setBaseDiscountAmount($customDiscount);
+        }
 
         $order->setSubtotal((float) $order->getSubTotal());
         $order->setBaseSubtotal((float) $order->getBaseSubtotal());
-        $order->setGrandTotal((float)  $order->getGrandTotal());
+        $order->setGrandTotal((float) $order->getGrandTotal());
         $order->setBaseGrandTotal((float) $order->getBaseGrandTotal());
-        $order ->save();
-      
+        $order->addStatusHistoryComment('discount-' . $existingDiscount . 'payu discount-' . $customDiscount);
+        $order->save();
 
         $order->setBaseTotalInvoiced($order->getGrandTotal());
         $order->setTotalInvoiced($order->getGrandTotal());
-        $payment=$order->getpayment();
+
+        $payment = $order->getPayment();
         $payment->setBaseAmountPaid($order->getGrandTotal());
         $payment->setAmountPaid($order->getGrandTotal());
         $payment->setBaseAmountOrdered($order->getGrandTotal());
         $payment->setAmountOrdered($order->getGrandTotal());
         $payment->save();
-        foreach($order->getAllItems() as $item){
-            $rat=$item->getPriceInclTax()/$total;
-            $ratdisc=abs($customDiscount)*$rat;
-            $discountAmt=($item->getDiscountAmount()+$ratdisc) * $item->getQtyOrdered();
-            $base=($item->getBaseDiscountAmount()+$ratdisc) * $item->getQtyOrdered();
-            $item->setBaseDiscountAmount($base);
-            $item->setDiscountAmount($discountAmt);
-            $item->save();
+        $totalNew = 0;
+        $orderParent = [];
+        foreach ($order->getAllItems() as $item) {
+                $orderParent[$item->getItemId()] = $item;
+                if($item->getProductType() !="bundle" && $item->getProductType() !="soft") {
+                     if($item->getParentItemId() == null || ($orderParent[$item->getParentItemId()]->getProductType() =="bundle" || $orderParent[$item->getParentItemId()]->getProductType() =="soft"))
+                         $totalNew=$totalNew+($item->getPriceInclTax()*$item->getQtyOrdered())-$item->getDiscountAmount();
+               }
+        }
+        // Distribute the discount proportionally among items
+        foreach ($order->getAllItems() as $item) {
+             if($item->getProductType() !="bundle" && $item->getProductType() !="soft") {
+                     if($item->getParentItemId() == null || ($orderParent[$item->getParentItemId()]->getProductType() =="bundle" || $orderParent[$item->getParentItemId()]->getProductType() =="soft"))
+                     { 
+                        $itemPriceInclTax = ($item->getPriceInclTax() * $item->getQtyOrdered()) - $item->getDiscountAmount();
+                        $discountRatio = $itemPriceInclTax / $totalNew;
+                        $itemDiscountAmount = $discountRatio * abs($customDiscount);
+                        $item->setBaseDiscountAmount($item->getBaseDiscountAmount() + $itemDiscountAmount);
+                        $item->setDiscountAmount($item->getDiscountAmount() + $itemDiscountAmount);
+                        $item->save();
+                     }
+             }
         }
+
         $order->save();
     }
-
 }
diff --git a/vendor/payuindia/payu/composer.json b/vendor/payuindia/payu/composer.json
index de08177..bc699e1 100644
--- a/vendor/payuindia/payu/composer.json
+++ b/vendor/payuindia/payu/composer.json
@@ -2,7 +2,7 @@
   "name": "payuindia/payu",
   "description": "PayU Payment Module for Magento 2.4.x",
   "type": "magento2-module",
-  "version": "2.4.6",
+  "version": "2.4.12",
   "license": [
     "OSL-3.0",
     "AFL-3.0"
diff --git a/vendor/payuindia/payu/etc/db_schema.xml b/vendor/payuindia/payu/etc/db_schema.xml
new file mode 100644
index 0000000..840684f
--- /dev/null
+++ b/vendor/payuindia/payu/etc/db_schema.xml
@@ -0,0 +1,6 @@
+<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+                 xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
+    <table name="sales_order" resource="default" engine="innodb" comment="sales order">
+        <column xsi:type="varchar" name="txnid" nullable="true" length="255" comment="Txn Id"/>
+    </table>
+</schema>
diff --git a/vendor/payuindia/payu/etc/module.xml b/vendor/payuindia/payu/etc/module.xml
index 0ff0dba..8b97f63 100644
--- a/vendor/payuindia/payu/etc/module.xml
+++ b/vendor/payuindia/payu/etc/module.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
-    <module name="PayUIndia_Payu" setup_version="2.4.6">
+    <module name="PayUIndia_Payu" setup_version="2.4.12">
         <sequence>
             <module name="Magento_Sales" />
 			<module name="Magento_Quote"/>
diff --git a/vendor/payuindia/payu/view/frontend/web/js/view/payment/method-renderer/payu-method.js b/vendor/payuindia/payu/view/frontend/web/js/view/payment/method-renderer/payu-method.js
index cb7e0bd..5727d09 100644
--- a/vendor/payuindia/payu/view/frontend/web/js/view/payment/method-renderer/payu-method.js
+++ b/vendor/payuindia/payu/view/frontend/web/js/view/payment/method-renderer/payu-method.js
@@ -23,13 +23,13 @@ define([
       console.log(email);
       jQuery(function ($) {
         $.ajax({
-          url:
-            window.checkoutConfig.payment.payu.redirectUrl + "?email=" + email,
-          type: "get",
+          url: window.checkoutConfig.payment.payu.redirectUrl,
+          type: "POST",
           dataType: "json",
           cache: false,
-          processData: false, // Don't process the files
-          contentType: false, // Set content type to false as jQuery will tell the server its a query string request
+          data: {
+            email: email,
+          },
           showLoader: true,
           success: function (data) {
             if (data["error"]) {
-- 
2.34.1

