From 0a98d38891947b14db2dd0a426c984a40a3fde55 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Wed, 31 Aug 2022 12:17:20 +0200 Subject: [PATCH] Add decimal rounding fix for items total and tax total --- .../Endpoint/PayUponInvoiceOrderEndpoint.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/modules/ppcp-api-client/src/Endpoint/PayUponInvoiceOrderEndpoint.php b/modules/ppcp-api-client/src/Endpoint/PayUponInvoiceOrderEndpoint.php index 78af457f8..2b61fdd42 100644 --- a/modules/ppcp-api-client/src/Endpoint/PayUponInvoiceOrderEndpoint.php +++ b/modules/ppcp-api-client/src/Endpoint/PayUponInvoiceOrderEndpoint.php @@ -355,6 +355,24 @@ class PayUponInvoiceOrderEndpoint { $data['purchase_units'][0]['amount']['value'] = number_format( $total_breakdown, 2, '.', '' ); } + $items_total = 0; + foreach ($data['purchase_units'][0]['items'] as $item) { + $items_total += floatval($item['unit_amount']['value']) * $item['quantity']; + } + $diff = round($items_total - $item_total, 2); + if($diff === -0.01 || $diff === 0.01) { + $data['purchase_units'][0]['amount']['breakdown']['item_total']['value'] = number_format( $items_total, 2, '.', '' ); + } + + $items_tax_total = 0; + foreach ($data['purchase_units'][0]['items'] as $item) { + $items_tax_total += floatval($item['tax']['value']) * $item['quantity']; + } + $diff = round($tax_total - $items_tax_total, 2); + if($diff === -0.01 || $diff === 0.01) { + $data['purchase_units'][0]['amount']['breakdown']['tax_total']['value'] = number_format( $items_tax_total, 2, '.', '' ); + } + return $data; } }