Skip to content
This repository was archived by the owner on Oct 24, 2023. It is now read-only.

Commit 95e5547

Browse files
author
Jens Schulze
committed
feat(Cart): support external tax amount
Closes #347
1 parent 34afcdd commit 95e5547

File tree

7 files changed

+315
-0
lines changed

7 files changed

+315
-0
lines changed

src/Core/Model/Cart/Cart.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class Cart extends Resource
8080
const TAX_MODE_PLATFORM = 'Platform';
8181
const TAX_MODE_EXTERNAL = 'External';
8282
const TAX_MODE_DISABLED = 'Disabled';
83+
const TAX_MODE_EXTERNAL_AMOUNT = 'ExternalAmount';
8384
const TAX_ROUNDING_MODE_HALF_EVEN = 'HalfEven';
8485
const TAX_ROUNDING_MODE_HALF_UP = 'HalfUp';
8586
const TAX_ROUNDING_MODE_HALF_DOWN = 'HalfDown';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Model\Cart;
7+
8+
use Commercetools\Core\Model\Common\JsonObject;
9+
use Commercetools\Core\Model\Common\Money;
10+
use Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft;
11+
12+
/**
13+
* @package Commercetools\Core\Model\Cart
14+
* @link http://dev.commercetools.com/http-api-projects-carts.html#externaltaxamountdraft
15+
* @method Money getTotalGross()
16+
* @method ExternalTaxAmountDraft setTotalGross(Money $totalGross = null)
17+
* @method ExternalTaxRateDraft getTaxRate()
18+
* @method ExternalTaxAmountDraft setTaxRate(ExternalTaxRateDraft $taxRate = null)
19+
*/
20+
class ExternalTaxAmountDraft extends JsonObject
21+
{
22+
public function fieldDefinitions()
23+
{
24+
return [
25+
'totalGross' => [static::TYPE => Money::class],
26+
'taxRate' => [static::TYPE => ExternalTaxRateDraft::class]
27+
];
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Carts\Command;
7+
8+
use Commercetools\Core\Model\Common\Context;
9+
use Commercetools\Core\Model\Common\Money;
10+
use Commercetools\Core\Model\Common\TaxPortionCollection;
11+
use Commercetools\Core\Request\AbstractAction;
12+
13+
/**
14+
* @package Commercetools\Core\Request\Carts\Command
15+
*
16+
* @method string getAction()
17+
* @method CartSetCartTotalTaxAction setAction(string $action = null)
18+
* @method Money getExternalTotalGross()
19+
* @method CartSetCartTotalTaxAction setExternalTotalGross(Money $externalTotalGross = null)
20+
* @method TaxPortionCollection getExternalTaxPortions()
21+
* @method CartSetCartTotalTaxAction setExternalTaxPortions(TaxPortionCollection $externalTaxPortions = null)
22+
*/
23+
class CartSetCartTotalTaxAction extends AbstractAction
24+
{
25+
public function fieldDefinitions()
26+
{
27+
return [
28+
'action' => [static::TYPE => 'string'],
29+
'externalTotalGross' => [static::TYPE => Money::class],
30+
'externalTaxPortions' => [static::TYPE => TaxPortionCollection::class],
31+
];
32+
}
33+
34+
/**
35+
* @param array $data
36+
* @param Context|callable $context
37+
*/
38+
public function __construct(array $data = [], $context = null)
39+
{
40+
parent::__construct($data, $context);
41+
$this->setAction('setCartTotalTax');
42+
}
43+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Carts\Command;
7+
8+
use Commercetools\Core\Model\Cart\ExternalTaxAmountDraft;
9+
use Commercetools\Core\Model\Common\Context;
10+
use Commercetools\Core\Request\AbstractAction;
11+
12+
/**
13+
* @package Commercetools\Core\Request\Carts\Command
14+
* @link http://dev.commercetools.com/http-api-projects-carts.html#set-customlineitem-taxamount
15+
* @method string getAction()
16+
* @method CartSetCustomLineItemTaxAmountAction setAction(string $action = null)
17+
* @method string getCustomLineItemId()
18+
* @method CartSetCustomLineItemTaxAmountAction setCustomLineItemId(string $customLineItemId = null)
19+
* @method ExternalTaxAmountDraft getExternalTaxAmount()
20+
* @method CartSetCustomLineItemTaxAmountAction setExternalTaxAmount(ExternalTaxAmountDraft $externalTaxAmount = null)
21+
*/
22+
class CartSetCustomLineItemTaxAmountAction extends AbstractAction
23+
{
24+
public function fieldDefinitions()
25+
{
26+
return [
27+
'action' => [static::TYPE => 'string'],
28+
'customLineItemId' => [static::TYPE => 'string'],
29+
'externalTaxAmount' => [static::TYPE => ExternalTaxAmountDraft::class],
30+
];
31+
}
32+
33+
/**
34+
* @param array $data
35+
* @param Context|callable $context
36+
*/
37+
public function __construct(array $data = [], $context = null)
38+
{
39+
parent::__construct($data, $context);
40+
$this->setAction('setCustomLineItemTaxAmount');
41+
}
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Carts\Command;
7+
8+
use Commercetools\Core\Model\Cart\ExternalTaxAmountDraft;
9+
use Commercetools\Core\Model\Common\Context;
10+
use Commercetools\Core\Request\AbstractAction;
11+
12+
/**
13+
* @package Commercetools\Core\Request\Carts\Command
14+
* @link http://dev.commercetools.com/http-api-projects-carts.html#set-lineitem-taxamount
15+
* @method string getAction()
16+
* @method CartSetLineItemTaxAmountAction setAction(string $action = null)
17+
* @method string getLineItemId()
18+
* @method CartSetLineItemTaxAmountAction setLineItemId(string $lineItemId = null)
19+
* @method ExternalTaxAmountDraft getExternalTaxAmount()
20+
* @method CartSetLineItemTaxAmountAction setExternalTaxAmount(ExternalTaxAmountDraft $externalTaxAmount = null)
21+
*/
22+
class CartSetLineItemTaxAmountAction extends AbstractAction
23+
{
24+
public function fieldDefinitions()
25+
{
26+
return [
27+
'action' => [static::TYPE => 'string'],
28+
'lineItemId' => [static::TYPE => 'string'],
29+
'externalTaxAmount' => [static::TYPE => ExternalTaxAmountDraft::class],
30+
];
31+
}
32+
33+
/**
34+
* @param array $data
35+
* @param Context|callable $context
36+
*/
37+
public function __construct(array $data = [], $context = null)
38+
{
39+
parent::__construct($data, $context);
40+
$this->setAction('setLineItemTaxAmount');
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* @author @jayS-de <jens.schulze@commercetools.de>
4+
*/
5+
6+
namespace Commercetools\Core\Request\Carts\Command;
7+
8+
use Commercetools\Core\Model\Cart\ExternalTaxAmountDraft;
9+
use Commercetools\Core\Model\Common\Context;
10+
use Commercetools\Core\Request\AbstractAction;
11+
12+
/**
13+
* @package Commercetools\Core\Request\Carts\Command
14+
* @link http://dev.commercetools.com/http-api-projects-carts.html#set-shippingmethod-taxamount
15+
* @method string getAction()
16+
* @method CartSetShippingMethodTaxAmountAction setAction(string $action = null)
17+
* @method ExternalTaxAmountDraft getExternalTaxAmount()
18+
* @method CartSetShippingMethodTaxAmountAction setExternalTaxAmount(ExternalTaxAmountDraft $externalTaxAmount = null)
19+
*/
20+
class CartSetShippingMethodTaxAmountAction extends AbstractAction
21+
{
22+
public function fieldDefinitions()
23+
{
24+
return [
25+
'action' => [static::TYPE => 'string'],
26+
'externalTaxAmount' => [static::TYPE => ExternalTaxAmountDraft::class],
27+
];
28+
}
29+
30+
/**
31+
* @param array $data
32+
* @param Context|callable $context
33+
*/
34+
public function __construct(array $data = [], $context = null)
35+
{
36+
parent::__construct($data, $context);
37+
$this->setAction('setShippingMethodTaxAmount');
38+
}
39+
}

tests/integration/Cart/CartUpdateRequestTest.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Commercetools\Core\Model\Cart\CustomLineItemDraft;
1212
use Commercetools\Core\Model\Cart\CustomLineItemDraftCollection;
1313
use Commercetools\Core\Model\Cart\ExternalLineItemTotalPrice;
14+
use Commercetools\Core\Model\Cart\ExternalTaxAmountDraft;
1415
use Commercetools\Core\Model\Cart\LineItem;
1516
use Commercetools\Core\Model\Cart\LineItemCollection;
1617
use Commercetools\Core\Model\Cart\LineItemDraft;
@@ -31,6 +32,7 @@
3132
use Commercetools\Core\Model\CustomField\CustomFieldObjectDraft;
3233
use Commercetools\Core\Model\CustomField\FieldContainer;
3334
use Commercetools\Core\Model\ShippingMethod\ShippingRate;
35+
use Commercetools\Core\Model\TaxCategory\ExternalTaxRateDraft;
3436
use Commercetools\Core\Request\CartDiscounts\CartDiscountCreateRequest;
3537
use Commercetools\Core\Request\Carts\CartByIdGetRequest;
3638
use Commercetools\Core\Request\Carts\CartCreateRequest;
@@ -52,21 +54,25 @@
5254
use Commercetools\Core\Request\Carts\Command\CartRemovePaymentAction;
5355
use Commercetools\Core\Request\Carts\Command\CartSetAnonymousIdAction;
5456
use Commercetools\Core\Request\Carts\Command\CartSetBillingAddressAction;
57+
use Commercetools\Core\Request\Carts\Command\CartSetCartTotalTaxAction;
5558
use Commercetools\Core\Request\Carts\Command\CartSetCountryAction;
5659
use Commercetools\Core\Request\Carts\Command\CartSetCustomerEmailAction;
5760
use Commercetools\Core\Request\Carts\Command\CartSetCustomerGroupAction;
5861
use Commercetools\Core\Request\Carts\Command\CartSetCustomerIdAction;
5962
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomFieldAction;
6063
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemCustomTypeAction;
64+
use Commercetools\Core\Request\Carts\Command\CartSetCustomLineItemTaxAmountAction;
6165
use Commercetools\Core\Request\Carts\Command\CartSetCustomShippingMethodAction;
6266
use Commercetools\Core\Request\Carts\Command\CartSetDeleteDaysAfterLastModificationAction;
6367
use Commercetools\Core\Request\Carts\Command\CartSetLineItemCustomFieldAction;
6468
use Commercetools\Core\Request\Carts\Command\CartSetLineItemCustomTypeAction;
6569
use Commercetools\Core\Request\Carts\Command\CartSetLineItemPriceAction;
70+
use Commercetools\Core\Request\Carts\Command\CartSetLineItemTaxAmountAction;
6671
use Commercetools\Core\Request\Carts\Command\CartSetLineItemTotalPriceAction;
6772
use Commercetools\Core\Request\Carts\Command\CartSetLocaleAction;
6873
use Commercetools\Core\Request\Carts\Command\CartSetShippingAddressAction;
6974
use Commercetools\Core\Request\Carts\Command\CartSetShippingMethodAction;
75+
use Commercetools\Core\Request\Carts\Command\CartSetShippingMethodTaxAmountAction;
7076
use Commercetools\Core\Request\Customers\CustomerLoginRequest;
7177
use Commercetools\Core\Request\CustomField\Command\SetCustomFieldAction;
7278
use Commercetools\Core\Request\CustomField\Command\SetCustomTypeAction;
@@ -1512,6 +1518,119 @@ public function testGiftLineItem()
15121518
$this->assertTrue($giftLineItemIncluded);
15131519
}
15141520

1521+
public function testSetLineItemTaxAmount()
1522+
{
1523+
$product = $this->getProduct();
1524+
$variant = $product->getMasterData()->getCurrent()->getMasterVariant();
1525+
1526+
$draft = $this->getDraft();
1527+
$draft->setLineItems(LineItemDraftCollection::of()->add(LineItemDraft::ofSku($variant->getSku())));
1528+
$draft->setTaxMode(Cart::TAX_MODE_EXTERNAL_AMOUNT);
1529+
$cart = $this->createCart($draft);
1530+
1531+
$taxAmount = mt_rand(1, 100000);
1532+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1533+
->addAction(
1534+
CartSetLineItemTaxAmountAction::of()
1535+
->setLineItemId($cart->getLineItems()->current()->getId())
1536+
->setExternalTaxAmount(
1537+
ExternalTaxAmountDraft::of()
1538+
->setTotalGross(Money::ofCurrencyAndAmount('EUR', $taxAmount))
1539+
->setTaxRate(ExternalTaxRateDraft::ofNameCountryAndAmount('test', 'DE', 1.0))
1540+
)
1541+
);
1542+
$response = $request->executeWithClient($this->getClient());
1543+
$cart = $request->mapResponse($response);
1544+
$this->deleteRequest->setVersion($cart->getVersion());
1545+
1546+
$this->assertSame($taxAmount, $cart->getLineItems()->current()->getTaxedPrice()->getTotalGross()->getCentAmount());
1547+
}
1548+
1549+
public function testSetCustomLineItemTaxAmount()
1550+
{
1551+
$draft = $this->getDraft();
1552+
$draft->setCustomLineItems(
1553+
CustomLineItemDraftCollection::of()
1554+
->add(
1555+
CustomLineItemDraft::of()
1556+
->setName(LocalizedString::ofLangAndText('en','test'))
1557+
->setQuantity(1)
1558+
->setMoney(Money::ofCurrencyAndAmount('EUR', 100))
1559+
->setSlug('test-124')
1560+
)
1561+
);
1562+
$draft->setTaxMode(Cart::TAX_MODE_EXTERNAL_AMOUNT);
1563+
$cart = $this->createCart($draft);
1564+
1565+
$taxAmount = mt_rand(1, 100000);
1566+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1567+
->addAction(
1568+
CartSetCustomLineItemTaxAmountAction::of()
1569+
->setCustomLineItemId($cart->getCustomLineItems()->current()->getId())
1570+
->setExternalTaxAmount(
1571+
ExternalTaxAmountDraft::of()
1572+
->setTotalGross(Money::ofCurrencyAndAmount('EUR', $taxAmount))
1573+
->setTaxRate(ExternalTaxRateDraft::ofNameCountryAndAmount('test', 'DE', 1.0))
1574+
)
1575+
);
1576+
$response = $request->executeWithClient($this->getClient());
1577+
$cart = $request->mapResponse($response);
1578+
$this->deleteRequest->setVersion($cart->getVersion());
1579+
1580+
$this->assertSame($taxAmount, $cart->getCustomLineItems()->current()->getTaxedPrice()->getTotalGross()->getCentAmount());
1581+
}
1582+
1583+
public function testSetShippingMethodTaxAmount()
1584+
{
1585+
$shippingMethod = $this->getShippingMethod();
1586+
$draft = $this->getDraft();
1587+
$draft->setShippingAddress(Address::of()->setCountry('DE')->setState($this->getRegion()));
1588+
$draft->setShippingMethod($shippingMethod->getReference());
1589+
$draft->setTaxMode(Cart::TAX_MODE_EXTERNAL_AMOUNT);
1590+
$cart = $this->createCart($draft);
1591+
1592+
$taxAmount = mt_rand(1, 100000);
1593+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1594+
->addAction(
1595+
CartSetShippingMethodTaxAmountAction::of()
1596+
->setExternalTaxAmount(
1597+
ExternalTaxAmountDraft::of()
1598+
->setTotalGross(Money::ofCurrencyAndAmount('EUR', $taxAmount))
1599+
->setTaxRate(ExternalTaxRateDraft::ofNameCountryAndAmount('test', 'DE', 1.0))
1600+
)
1601+
);
1602+
$response = $request->executeWithClient($this->getClient());
1603+
$cart = $request->mapResponse($response);
1604+
$this->deleteRequest->setVersion($cart->getVersion());
1605+
1606+
$this->assertSame($taxAmount, $cart->getShippingInfo()->getTaxedPrice()->getTotalGross()->getCentAmount());
1607+
}
1608+
1609+
public function testSetTotalTaxAmount()
1610+
{
1611+
$product = $this->getProduct();
1612+
$variant = $product->getMasterData()->getCurrent()->getMasterVariant();
1613+
1614+
$draft = $this->getDraft();
1615+
$draft->setLineItems(LineItemDraftCollection::of()->add(LineItemDraft::ofSku($variant->getSku())));
1616+
$draft->setTaxMode(Cart::TAX_MODE_EXTERNAL_AMOUNT);
1617+
$cart = $this->createCart($draft);
1618+
1619+
$taxAmount = mt_rand(1, 100000);
1620+
$request = CartUpdateRequest::ofIdAndVersion($cart->getId(), $cart->getVersion())
1621+
->addAction(
1622+
CartSetCartTotalTaxAction::of()
1623+
->setExternalTotalGross(
1624+
Money::ofCurrencyAndAmount('EUR', $taxAmount)
1625+
)
1626+
);
1627+
$response = $request->executeWithClient($this->getClient());
1628+
$cart = $request->mapResponse($response);
1629+
$this->deleteRequest->setVersion($cart->getVersion());
1630+
1631+
$this->assertSame($taxAmount, $cart->getTaxedPrice()->getTotalGross()->getCentAmount());
1632+
}
1633+
15151634
/**
15161635
* @return CartDraft
15171636
*/

0 commit comments

Comments
 (0)