From 28edfcbebc082a05b870a655238a877f6acb82d4 Mon Sep 17 00:00:00 2001 From: dinamiko Date: Wed, 16 Mar 2022 16:29:22 +0100 Subject: [PATCH] Get tax rate from item --- modules/ppcp-api-client/src/Entity/Item.php | 30 ++++++++++++++++--- .../src/Factory/ItemFactory.php | 9 ++++-- .../Gateway/PayUponInvoice/OrderEndpoint.php | 1 - 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-api-client/src/Entity/Item.php b/modules/ppcp-api-client/src/Entity/Item.php index 218bf3fa3..dd661301e 100644 --- a/modules/ppcp-api-client/src/Entity/Item.php +++ b/modules/ppcp-api-client/src/Entity/Item.php @@ -14,7 +14,6 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Entity; */ class Item { - const PHYSICAL_GOODS = 'PHYSICAL_GOODS'; const DIGITAL_GOODS = 'DIGITAL_GOODS'; @@ -67,6 +66,13 @@ class Item { */ private $category; + /** + * The tax rate. + * + * @var float|int + */ + protected $tax_rate; + /** * Item constructor. * @@ -85,7 +91,8 @@ class Item { string $description = '', Money $tax = null, string $sku = '', - string $category = 'PHYSICAL_GOODS' + string $category = 'PHYSICAL_GOODS', + float $tax_rate = 0 ) { $this->name = $name; @@ -94,8 +101,9 @@ class Item { $this->description = $description; $this->tax = $tax; $this->sku = $sku; - $this->category = ( self::DIGITAL_GOODS === $category ) ? - self::DIGITAL_GOODS : self::PHYSICAL_GOODS; + $this->category = ( self::DIGITAL_GOODS === $category ) ? self::DIGITAL_GOODS : self::PHYSICAL_GOODS; + $this->category = $category; + $this->tax_rate = $tax_rate; } /** @@ -161,6 +169,16 @@ class Item { return $this->category; } + /** + * Returns the tax rate. + * + * @return float + */ + public function tax_rate():float + { + return round((float) $this->tax_rate, 2); + } + /** * Returns the object as array. * @@ -180,6 +198,10 @@ class Item { $item['tax'] = $this->tax()->to_array(); } + if ($this->tax_rate()) { + $item['tax_rate'] = (string) $this->tax_rate(); + } + return $item; } } diff --git a/modules/ppcp-api-client/src/Factory/ItemFactory.php b/modules/ppcp-api-client/src/Factory/ItemFactory.php index 10aa40ff8..a292d90bd 100644 --- a/modules/ppcp-api-client/src/Factory/ItemFactory.php +++ b/modules/ppcp-api-client/src/Factory/ItemFactory.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Factory; +use WC_Tax; use WooCommerce\PayPalCommerce\ApiClient\Entity\Item; use WooCommerce\PayPalCommerce\ApiClient\Entity\Money; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; @@ -57,6 +58,7 @@ class ItemFactory { $price_without_tax_rounded = round( $price_without_tax, 2 ); $tax = round( $price - $price_without_tax_rounded, 2 ); $tax = new Money( $tax, $this->currency ); + $tax_rates = WC_Tax::get_rates($product->get_tax_class()); return new Item( mb_substr( $product->get_name(), 0, 127 ), new Money( $price_without_tax_rounded, $this->currency ), @@ -64,7 +66,8 @@ class ItemFactory { mb_substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ), $tax, $product->get_sku(), - ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS + ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS, + reset($tax_rates)['rate'] ?? 0 ); }, $cart->get_cart_contents() @@ -138,6 +141,7 @@ class ItemFactory { $price_without_tax_rounded = round( $price_without_tax, 2 ); $tax = round( $price - $price_without_tax_rounded, 2 ); $tax = new Money( $tax, $currency ); + $tax_rates = WC_Tax::get_rates($product->get_tax_class()); return new Item( mb_substr( $product->get_name(), 0, 127 ), new Money( $price_without_tax_rounded, $currency ), @@ -145,7 +149,8 @@ class ItemFactory { mb_substr( wp_strip_all_tags( $product->get_description() ), 0, 127 ), $tax, $product->get_sku(), - ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS + ( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS, + reset($tax_rates)['rate'] ?? 0 ); } diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php index 2ad775049..1349d9f45 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/OrderEndpoint.php @@ -77,7 +77,6 @@ class OrderEndpoint { ), ); - $data['purchase_units'][0]['items'][0]['tax_rate'] = '19.00'; $data['purchase_units'][0]['shipping']['name']['full_name'] = 'John Doe'; $data['purchase_units'][0]['shipping']['address'] = array( 'address_line_1' => 'Taunusanlage 12',