From 831715f4a6935011d6cab3ecec27c968e44686e1 Mon Sep 17 00:00:00 2001 From: Bartosz Sosnowski Date: Wed, 20 Nov 2024 11:21:59 +0100 Subject: [PATCH] Fixed discounts handling in amount breakdown --- CHANGELOG.md | 1 - src/Core/PatchRequestFactory.php | 48 +++++++++---------------- src/Core/PayPalRequestAmountFactory.php | 9 +++-- src/Model/Order.php | 1 - 4 files changed, 21 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46d012b8..b2097afa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - [0007675](https://bugs.oxid-esales.com/view.php?id=7675): fix the possibility to finish order without redirect and login to Paypal - [0007676](https://bugs.oxid-esales.com/view.php?id=7676): If we have a corrupted generated_services.yaml and try to deactivate the module via the admin, we will display a more understandable error message about what happened. - introduce ActionHash to make the PayPal-Request-ID more unique -- [0007695](https://bugs.oxid-esales.com/view.php?id=7695): Fix: if DeliverySet is set in Frontend, then do not add any PseudoDeliveryCosts for PPExpress ### NEW - PayPal-Request-Id based on serialized body, no extra PayPal-Request-Id necessary anymore diff --git a/src/Core/PatchRequestFactory.php b/src/Core/PatchRequestFactory.php index 3bdad79d..028d8949 100644 --- a/src/Core/PatchRequestFactory.php +++ b/src/Core/PatchRequestFactory.php @@ -67,50 +67,36 @@ public function getRequest( protected function getShippingAddressPatch(): void { - $addressObj = false; - $address = new AddressPortable(); - $session = Registry::getSession(); + $deliveryId = Registry::getSession()->getVariable("deladrid"); + $deliveryAddress = oxNew(Address::class); - $deliveryId = $session->getVariable("deladrid"); + if ($deliveryId && $deliveryAddress->load($deliveryId)) { + $patch = new Patch(); + $patch->op = Patch::OP_REPLACE; + $patch->path = "/purchase_units/@reference_id=='" + . Constants::PAYPAL_ORDER_REFERENCE_ID + . "'/shipping/address"; - if ($deliveryId) { - $deliveryAddress = oxNew(Address::class); - if ($deliveryAddress->load($deliveryId)) { - $addressObj = $deliveryAddress; - } - } - else { - $user = $this->basket->getUser(); - if ($user) { - $addressObj = $user; - } - } + $address = new AddressPortable(); - if ($addressObj) { $state = oxNew(State::class); - $country = oxNew(Country::class); + $state->load($deliveryAddress->getFieldData('oxstateid')); - $state->load($addressObj->getFieldData('oxstateid')); - $country->load($addressObj->getFieldData('oxcountryid')); + $country = oxNew(Country::class); + $country->load($deliveryAddress->getFieldData('oxcountryid')); $addressLine = - $addressObj->getFieldData('oxstreet') . " " . $addressObj->getFieldData('oxstreetnr'); + $deliveryAddress->getFieldData('oxstreet') . " " . $deliveryAddress->getFieldData('oxstreetnr'); $address->address_line_1 = $addressLine; - $addinfoLine = $addressObj->getFieldData('oxcompany') . " " . - $addressObj->getFieldData('oxaddinfo'); + $addinfoLine = $deliveryAddress->getFieldData('oxcompany') . " " . + $deliveryAddress->getFieldData('oxaddinfo'); $address->address_line_2 = $addinfoLine; $address->admin_area_1 = $state->getFieldData('oxtitle'); - $address->admin_area_2 = $addressObj->getFieldData('oxcity'); + $address->admin_area_2 = $deliveryAddress->getFieldData('oxcity'); $address->country_code = $country->oxcountry__oxisoalpha2->value; - $address->postal_code = $addressObj->getFieldData('oxzip'); - - $patch = new Patch(); - $patch->op = Patch::OP_REPLACE; - $patch->path = "/purchase_units/@reference_id=='" - . Constants::PAYPAL_ORDER_REFERENCE_ID - . "'/shipping/address"; + $address->postal_code = $deliveryAddress->getFieldData('oxzip'); $patch->value = $address; diff --git a/src/Core/PayPalRequestAmountFactory.php b/src/Core/PayPalRequestAmountFactory.php index 5894ab38..dac1ce34 100644 --- a/src/Core/PayPalRequestAmountFactory.php +++ b/src/Core/PayPalRequestAmountFactory.php @@ -47,18 +47,16 @@ public function getAmount(Basket $basket): AmountWithBreakdown } if ($netMode) { - $total = $brutBasketTotal - $shipping; + $total = $itemTotal - $shipping; } else { $total = $itemTotal - $discount + $itemTotalAdditionalCosts; } - $total_with_shipping = $total + $shipping; $total = PriceToMoney::convert($total, $currency); - $total_with_shipping = PriceToMoney::convert($total_with_shipping, $currency); //Total amount $amount = new AmountWithBreakdown(); - $amount->value = $total_with_shipping->value; + $amount->value = $brutBasketTotal; $amount->currency_code = $total->currency_code; //Cost breakdown @@ -68,7 +66,8 @@ public function getAmount(Basket $basket): AmountWithBreakdown $breakdown->discount = PriceToMoney::convert($netMode ? $brutDiscountValue : $discount, $currency); } - $breakdown->item_total = PriceToMoney::convert($total->value, $currency); + $breakDownItemTotal = $netMode ? $total->value : $itemTotal; + $breakdown->item_total = PriceToMoney::convert($breakDownItemTotal, $currency); //Item tax sum - we use 0% and calculate with brutto to avoid rounding errors $breakdown->tax_total = PriceToMoney::convert(0, $currency); diff --git a/src/Model/Order.php b/src/Model/Order.php index 6be99c5f..49a43025 100644 --- a/src/Model/Order.php +++ b/src/Model/Order.php @@ -663,7 +663,6 @@ public function setPayPalTracking(string $trackingCarrier, string $trackingCode) 'oxtrackcode' => $trackingCode ] ); - $this->save(); $payPalOrder = $this->getPayPalRepository(); $payPalOrder->setTrackingCode($trackingCode); $payPalOrder->setTrackingCarrier($trackingCarrier);