From a886f91581371834882a4957988cad4e8157c417 Mon Sep 17 00:00:00 2001 From: vinoth Date: Thu, 3 Oct 2024 18:15:49 +0530 Subject: [PATCH 1/3] bugfix/PP-12925: added product tax rate --- payrexx/config.xml | 2 +- payrexx/payrexx.php | 2 +- payrexx/src/Service/PayrexxApiService.php | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/payrexx/config.xml b/payrexx/config.xml index 08ee27d..fe21013 100644 --- a/payrexx/config.xml +++ b/payrexx/config.xml @@ -2,7 +2,7 @@ payrexx - + diff --git a/payrexx/payrexx.php b/payrexx/payrexx.php index 32bbd49..d975d09 100644 --- a/payrexx/payrexx.php +++ b/payrexx/payrexx.php @@ -31,7 +31,7 @@ public function __construct() $this->name = 'payrexx'; $this->tab = 'payments_gateways'; $this->module_key = '0c4dbfccbd85dd948fd9a13d5a4add90'; - $this->version = '1.5.1'; + $this->version = '1.5.2'; $this->author = 'Payrexx'; $this->is_eu_compatible = 1; $this->ps_versions_compliancy = ['min' => '1.7']; diff --git a/payrexx/src/Service/PayrexxApiService.php b/payrexx/src/Service/PayrexxApiService.php index c78eb8f..f2fd4f2 100644 --- a/payrexx/src/Service/PayrexxApiService.php +++ b/payrexx/src/Service/PayrexxApiService.php @@ -116,6 +116,7 @@ public function createPayrexxGateway( 'quantity' => $product['quantity'], 'amount' => $productPrice, 'sku' => $product['reference'], + 'vatRate' => $product['rate'], ]; $basketAmount += $productPrice * $product['quantity']; } @@ -124,6 +125,7 @@ public function createPayrexxGateway( $basket[] = [ 'name' => 'Shipping', 'amount' => $shippingAmount, + 'vatRate' => 0, ]; $basketAmount += $shippingAmount; } @@ -134,6 +136,7 @@ public function createPayrexxGateway( $basket[] = [ 'name' => 'Discount', 'amount' => -$discountAmount, + 'vatRate' => 0, ]; $basketAmount -= $discountAmount; } @@ -150,7 +153,6 @@ public function createPayrexxGateway( } $gateway->setAmount($total * 100); - $gateway->setVatRate($cart->getAverageProductsTaxRate() * 100); $gateway->setCurrency($currency); $gateway->setSuccessRedirectUrl($redirectUrls['success']); $gateway->setCancelRedirectUrl($redirectUrls['cancel']); From 234b6061a5467919ecd39ad4da72e6672b5ed1ae Mon Sep 17 00:00:00 2001 From: vinoth Date: Fri, 4 Oct 2024 15:31:51 +0530 Subject: [PATCH 2/3] bugfix/PP-12925: send vatRate to basket products --- payrexx/controllers/front/payrexx.php | 10 -- payrexx/src/Service/PayrexxApiService.php | 39 +------- payrexx/src/Util/BasketUtil.php | 109 ++++++++++++++++++++++ payrexx/src/Util/index.php | 17 ++++ 4 files changed, 130 insertions(+), 45 deletions(-) create mode 100644 payrexx/src/Util/BasketUtil.php create mode 100644 payrexx/src/Util/index.php diff --git a/payrexx/controllers/front/payrexx.php b/payrexx/controllers/front/payrexx.php index fe2343e..e4a5f19 100644 --- a/payrexx/controllers/front/payrexx.php +++ b/payrexx/controllers/front/payrexx.php @@ -33,13 +33,6 @@ public function postProcess() $context = Context::getContext(); $cart = $context->cart; - $productNames = []; - $products = $cart->getProducts(); - foreach ($products as $product) { - $quantity = $product['cart_quantity'] > 1 ? $product['cart_quantity'] . 'x ' : ''; - $productNames[] = $quantity . $product['name']; - } - $customer = $context->customer; $invoiceAddress = new Address($cart->id_address_invoice); @@ -75,15 +68,12 @@ public function postProcess() ]; $currencyIsoCode = !empty($currency) ? $currency : 'USD'; - $purpose = implode(', ', $productNames); - if ($gatewayId = $payrexxDbService->getCartGatewayId($cart->id)) { $payrexxApiService->deletePayrexxGateway($gatewayId); } $paymentMethod = Tools::getValue('payrexxPaymentMethod'); $pm = ($paymentMethod != 'payrexx') ? [$paymentMethod] : []; $gateway = $payrexxApiService->createPayrexxGateway( - $purpose, $total, $currencyIsoCode, $redirectUrls, diff --git a/payrexx/src/Service/PayrexxApiService.php b/payrexx/src/Service/PayrexxApiService.php index f2fd4f2..85780b5 100644 --- a/payrexx/src/Service/PayrexxApiService.php +++ b/payrexx/src/Service/PayrexxApiService.php @@ -19,6 +19,7 @@ use Payrexx\Models\Response\Gateway; use Payrexx\Models\Response\Transaction; use Payrexx\PayrexxException; +use Payrexx\PayrexxPaymentGateway\Util\BasketUtil; class PayrexxApiService { @@ -96,7 +97,6 @@ public function getPayrexxTransaction($transactionId): ?Transaction } public function createPayrexxGateway( - string $purpose, float $total, string $currency, array $redirectUrls, @@ -106,40 +106,9 @@ public function createPayrexxGateway( array $shippingAddress, array $pm ): ?Gateway { - $basket = []; - $basketAmount = 0; - foreach ($cart->getProducts() as $product) { - $productPrice = round($product['price_wt'] * 100, 0); - $basket[] = [ - 'name' => $product['name'], - 'description' => $product['description_short'], - 'quantity' => $product['quantity'], - 'amount' => $productPrice, - 'sku' => $product['reference'], - 'vatRate' => $product['rate'], - ]; - $basketAmount += $productPrice * $product['quantity']; - } - if ($cart->getPackageShippingCost()) { - $shippingAmount = round($cart->getPackageShippingCost() * 100, 0); - $basket[] = [ - 'name' => 'Shipping', - 'amount' => $shippingAmount, - 'vatRate' => 0, - ]; - $basketAmount += $shippingAmount; - } - - if ($cart->getDiscountSubtotalWithoutGifts()) { - $discountAmount = round($cart->getDiscountSubtotalWithoutGifts() * 100, 0); - - $basket[] = [ - 'name' => 'Discount', - 'amount' => -$discountAmount, - 'vatRate' => 0, - ]; - $basketAmount -= $discountAmount; - } + $basket = BasketUtil::createBasketByCart($cart); + $basketAmount = BasketUtil::getBasketAmount($basket); + $purpose = BasketUtil::createPurposeByCart($cart); $payrexx = $this->getInterface($this->instanceName, $this->apiKey, $this->platform); diff --git a/payrexx/src/Util/BasketUtil.php b/payrexx/src/Util/BasketUtil.php new file mode 100644 index 0000000..6799a1f --- /dev/null +++ b/payrexx/src/Util/BasketUtil.php @@ -0,0 +1,109 @@ + + * @copyright 2024 Payrexx + * @license MIT License + */ +namespace Payrexx\PayrexxPaymentGateway\Util; + +if (!defined('_PS_VERSION_')) { + exit; +} + +class BasketUtil +{ + /** + * @param Cart $cart + * @return array + */ + public static function createBasketByCart($cart): array + { + $basket = []; + // product + foreach ($cart->getProducts() as $product) { + $productPrice = round($product['price_wt'] * 100, 0); + $basket[] = [ + 'name' => $product['name'], + 'description' => $product['description_short'], + 'quantity' => $product['quantity'], + 'amount' => $productPrice, + 'sku' => $product['reference'], + 'vatRate' => $product['rate'], + ]; + } + + // shipping + if ($cart->getPackageShippingCost()) { + $shippingAmount = $cart->getPackageShippingCost() * 100; + + try { + $shippingVatRate = 0; + $shippingCostWithoutTax = $cart->getPackageShippingCost(null, false) * 100; + if ($shippingAmount !== $shippingCostWithoutTax && !empty($cart->id_carrier)) { + $carrier = new \Carrier($cart->id_carrier); + $address = \Address::initialize((int) $cart->id_address_delivery); + $shippingVatRate = $carrier->getTaxesRate($address); + } + } catch (\Exception $e) { + } + $basket[] = [ + 'name' => 'Shipping', + 'amount' => $shippingAmount, + 'vatRate' => $shippingVatRate, + ]; + } + + // Discount + if ($cart->getDiscountSubtotalWithoutGifts()) { + $discountAmount = $cart->getDiscountSubtotalWithoutGifts() * 100; + + try { + $discountVatRate = 0; + $discountWithoutTax = $cart->getDiscountSubtotalWithoutGifts(false) * 100; + if ($discountAmount !== $discountWithoutTax) { + $taxDifference = $discountAmount - $discountWithoutTax; + $discountVatRate = ($taxDifference / $discountWithoutTax) * 100; + $discountVatRate = number_format((float) $discountVatRate, 1, '.', ''); + } + } catch (\Exception $e) { + } + + $basket[] = [ + 'name' => 'Discount', + 'amount' => -$discountAmount, + 'vatRate' => $discountVatRate, + ]; + } + return $basket; + } + + /** + * @param array $basket + * @return int + */ + public static function getBasketAmount(array $basket): int + { + $basketAmount = 0; + foreach ($basket as $product) { + $basketAmount += ($product['quantity'] ?? 1) * $product['amount']; + } + return round($basketAmount); + } + + /** + * @param Cart $cart + * @return string + */ + public static function createPurposeByCart($cart): string + { + $productNames = []; + $products = $cart->getProducts(); + foreach ($products as $product) { + $quantity = $product['cart_quantity'] > 1 ? $product['cart_quantity'] . 'x ' : ''; + $productNames[] = $quantity . $product['name']; + } + return implode(', ', $productNames); + } +} diff --git a/payrexx/src/Util/index.php b/payrexx/src/Util/index.php new file mode 100644 index 0000000..f0af4fd --- /dev/null +++ b/payrexx/src/Util/index.php @@ -0,0 +1,17 @@ + + * @copyright 2024 Payrexx + * @license MIT License + */ +header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); +header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + +header('Cache-Control: no-store, no-cache, must-revalidate'); +header('Cache-Control: post-check=0, pre-check=0', false); +header('Pragma: no-cache'); + +header('Location: ../'); +exit; From cd0a4a89f89cf4546a3ef4e1490f5efc6d18ca9b Mon Sep 17 00:00:00 2001 From: vinoth Date: Fri, 4 Oct 2024 15:52:37 +0530 Subject: [PATCH 3/3] bugfix/PP-12925: improved the code --- payrexx/src/Util/BasketUtil.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/payrexx/src/Util/BasketUtil.php b/payrexx/src/Util/BasketUtil.php index 6799a1f..b56f6ca 100644 --- a/payrexx/src/Util/BasketUtil.php +++ b/payrexx/src/Util/BasketUtil.php @@ -24,14 +24,17 @@ public static function createBasketByCart($cart): array // product foreach ($cart->getProducts() as $product) { $productPrice = round($product['price_wt'] * 100, 0); - $basket[] = [ + $basketItem = [ 'name' => $product['name'], 'description' => $product['description_short'], 'quantity' => $product['quantity'], 'amount' => $productPrice, 'sku' => $product['reference'], - 'vatRate' => $product['rate'], ]; + if (isset($product['rate'])) { + $basketItem['vatRate'] = $product['rate']; + } + $basket[] = $basketItem; } // shipping