Skip to content

Commit

Permalink
Merge pull request #19 from yabhq/feat/tax-logistics
Browse files Browse the repository at this point in the history
Fix issue with conditional logic in checkout resource
  • Loading branch information
jimhlad authored Feb 24, 2021
2 parents 4513470 + 7765598 commit d204d32
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
29 changes: 22 additions & 7 deletions src/Http/Resources/CheckoutResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,30 @@ class CheckoutResource extends JsonResource
*/
public function toArray($request)
{
return [
$arr = [
'subtotal' => $this->getSubtotal(),
$this->mergeWhen($this->hasInfoNeededToCalculateTotal(), [
'shipping' => $this->getShipping(),
'discount' => $this->getDiscount(),
'taxes' => $this->getTaxes(),
'total' => $this->getTotal(),
]),
'cart' => $this->getCart(),
];

if ($this->hasInfoNeededToCalculateTotal()) {
$arr[] = $this->getCheckoutTotals();
}

return $arr;
}

/**
* Get the shipping, discount, taxes and total for the checkout.
*
* @return array
*/
private function getCheckoutTotals() : array
{
return [
'shipping' => $this->getShipping(),
'discount' => $this->getDiscount(),
'taxes' => $this->getTaxes(),
'total' => $this->getTotal(),
];
}
}
4 changes: 0 additions & 4 deletions tests/Feature/Api/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public function a_new_checkout_can_be_created_via_the_api()

$response->assertJson([
'subtotal' => 0,
'taxes' => 0,
'total' => 0,
'cart' => [
'id' => $cart->id,
],
Expand All @@ -52,8 +50,6 @@ public function an_existing_checkout_can_be_retrieved_via_the_api()

$response->assertJson([
'subtotal' => 14.95,
'taxes' => 2.69,
'total' => 17.64,
'cart' => [
'id' => $item->cart->id,
'items' => [
Expand Down

0 comments on commit d204d32

Please sign in to comment.