From 770f8e558b01c905e51733254483c02b97ce319a Mon Sep 17 00:00:00 2001 From: Alex P Date: Wed, 15 Jun 2022 10:00:03 +0300 Subject: [PATCH] Do not send tax in each item Looks like it is not displayed anyway, and it causes mismatch + cannot send shipping/discount taxes as tax --- modules/ppcp-api-client/src/Entity/PurchaseUnit.php | 10 ++++++++-- modules/ppcp-api-client/src/Factory/ItemFactory.php | 12 ++++-------- tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php | 2 -- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/ppcp-api-client/src/Entity/PurchaseUnit.php b/modules/ppcp-api-client/src/Entity/PurchaseUnit.php index 82c02eeeb..4f4c8b785 100644 --- a/modules/ppcp-api-client/src/Entity/PurchaseUnit.php +++ b/modules/ppcp-api-client/src/Entity/PurchaseUnit.php @@ -343,8 +343,14 @@ class PurchaseUnit { } } - $tax_total = $breakdown->tax_total(); - if ( $tax_total ) { + $tax_total = $breakdown->tax_total(); + $items_with_tax = array_filter( + $this->items, + function ( Item $item ): bool { + return null !== $item->tax(); + } + ); + if ( $tax_total && ! empty( $items_with_tax ) ) { $remaining_tax_total = array_reduce( $items, function ( float $total, Item $item ): float { diff --git a/modules/ppcp-api-client/src/Factory/ItemFactory.php b/modules/ppcp-api-client/src/Factory/ItemFactory.php index ca21f4769..4ff0a47a8 100644 --- a/modules/ppcp-api-client/src/Factory/ItemFactory.php +++ b/modules/ppcp-api-client/src/Factory/ItemFactory.php @@ -56,14 +56,12 @@ class ItemFactory { $price = (float) wc_get_price_including_tax( $product ); $price_without_tax = (float) wc_get_price_excluding_tax( $product ); $price_without_tax_rounded = round( $price_without_tax, 2 ); - $tax = round( $price - $price_without_tax_rounded, 2 ); - $tax = new Money( $tax, $this->currency ); return new Item( mb_substr( $product->get_name(), 0, 127 ), new Money( $price_without_tax_rounded, $this->currency ), $quantity, substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ) ?: '', - $tax, + null, $product->get_sku(), ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS ); @@ -81,7 +79,7 @@ class ItemFactory { new Money( (float) $fee->amount, $this->currency ), 1, '', - new Money( (float) $fee->tax, $this->currency ) + null ); }, $fees_from_session @@ -130,15 +128,13 @@ class ItemFactory { $price = (float) $order->get_item_subtotal( $item, true ); $price_without_tax = (float) $order->get_item_subtotal( $item, false ); $price_without_tax_rounded = round( $price_without_tax, 2 ); - $tax = round( $price - $price_without_tax_rounded, 2 ); - $tax = new Money( $tax, $currency ); return new Item( mb_substr( $item->get_name(), 0, 127 ), new Money( $price_without_tax_rounded, $currency ), $quantity, substr( wp_strip_all_tags( $product instanceof WC_Product ? $product->get_description() : '' ), 0, 127 ) ?: '', - $tax, + null, $product instanceof WC_Product ? $product->get_sku() : '', ( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS ); @@ -158,7 +154,7 @@ class ItemFactory { new Money( (float) $item->get_amount(), $order->get_currency() ), $item->get_quantity(), '', - new Money( (float) $item->get_total_tax(), $order->get_currency() ) + null ); } diff --git a/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php b/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php index 6b380e50c..a67621bcf 100644 --- a/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php +++ b/tests/PHPUnit/ApiClient/Factory/ItemFactoryTest.php @@ -72,7 +72,6 @@ class ItemFactoryTest extends TestCase $this->assertEquals('name', $item->name()); $this->assertEquals('sku', $item->sku()); $this->assertEquals(1, $item->unit_amount()->value()); - $this->assertEquals(2, $item->tax()->value()); } public function testFromCartDigitalGood() @@ -185,7 +184,6 @@ class ItemFactoryTest extends TestCase $this->assertEquals(1, $item->quantity()); $this->assertEquals(Item::PHYSICAL_GOODS, $item->category()); $this->assertEquals(1, $item->unit_amount()->value()); - $this->assertEquals(2, $item->tax()->value()); } public function testFromWcOrderDigitalGood()