Skip to content

Commit

Permalink
Add shipping fee
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Jun 20, 2024
1 parent c0431b4 commit fddbf68
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/Client/Enum/OrderLineType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Webgriffe\SyliusKlarnaPlugin\Client\Enum;

enum OrderLineType: string
{
case Physical = 'physical';
case Discount = 'discount';
case ShippingFee = 'shipping_fee';
case SalesTax = 'sales_tax';
case Digital = 'digital';
case GiftCard = 'gift_card';
case StoreCredit = 'store_credit';
case Surcharge = 'surcharge';
}
7 changes: 4 additions & 3 deletions src/Client/ValueObject/OrderLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Webgriffe\SyliusKlarnaPlugin\Client\ValueObject;

use JsonSerializable;
use Webgriffe\SyliusKlarnaPlugin\Client\Enum\OrderLineType;

final readonly class OrderLine implements JsonSerializable
{
Expand All @@ -21,7 +22,7 @@ public function __construct(
private ?string $merchantData = null,
private ?string $quantityUnit = null,
private ?string $reference = null,
private ?string $type = null,
private ?OrderLineType $type = null,
) {
}

Expand Down Expand Up @@ -85,7 +86,7 @@ public function getReference(): ?string
return $this->reference;
}

public function getType(): ?string
public function getType(): ?OrderLineType
{
return $this->type;
}
Expand All @@ -104,7 +105,7 @@ public function jsonSerialize(): array
'image_url' => $this->getImageUrl(),
'merchant_data' => $this->getMerchantData(),
'quantity_unit' => $this->getQuantityUnit(),
'type' => $this->getType(),
'type' => $this->getType()->value,

Check failure on line 108 in src/Client/ValueObject/OrderLine.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 5.7

Cannot access property $value on Webgriffe\SyliusKlarnaPlugin\Client\Enum\OrderLineType|null.

Check failure on line 108 in src/Client/ValueObject/OrderLine.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 8.0

Cannot access property $value on Webgriffe\SyliusKlarnaPlugin\Client\Enum\OrderLineType|null.

Check failure on line 108 in src/Client/ValueObject/OrderLine.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 5.7

Cannot access property $value on Webgriffe\SyliusKlarnaPlugin\Client\Enum\OrderLineType|null.

Check failure on line 108 in src/Client/ValueObject/OrderLine.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 8.0

Cannot access property $value on Webgriffe\SyliusKlarnaPlugin\Client\Enum\OrderLineType|null.
], static fn ($value) => $value !== null);
}
}
26 changes: 24 additions & 2 deletions src/Converter/PaymentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

use LogicException;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\AdjustmentInterface;
use Sylius\Component\Core\Model\CustomerInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\OrderItemInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Webgriffe\SyliusKlarnaPlugin\Client\Enum\AcquiringChannel;
use Webgriffe\SyliusKlarnaPlugin\Client\Enum\Intent;
use Webgriffe\SyliusKlarnaPlugin\Client\Enum\OrderLineType;
use Webgriffe\SyliusKlarnaPlugin\Client\ValueObject\Address;
use Webgriffe\SyliusKlarnaPlugin\Client\ValueObject\Amount;
use Webgriffe\SyliusKlarnaPlugin\Client\ValueObject\Customer;
Expand Down Expand Up @@ -109,6 +111,26 @@ private function getOrderLines(OrderInterface $order): array
$lines[] = $this->createOrderLineFromOrderItem($orderItem);
}

$taxRate = 2200; #TODO
$totalAmount = $order->getShippingTotal();
$shippingTaxTotal = $totalAmount - (($totalAmount * 10000) / (10000 + $taxRate));

$lines[] = new OrderLine(
$order->getShipments()->first()?->getMethod()?->getName() ?? 'Shipping fee',

Check failure on line 119 in src/Converter/PaymentConverter.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 5.7

Cannot call method getMethod() on Sylius\Component\Core\Model\ShipmentInterface|false.

Check failure on line 119 in src/Converter/PaymentConverter.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 5.7

Using nullsafe method call on non-nullable type Sylius\Component\Core\Model\ShipmentInterface|false. Use -> instead.

Check failure on line 119 in src/Converter/PaymentConverter.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 8.0

Cannot call method getMethod() on Sylius\Component\Core\Model\ShipmentInterface|false.

Check failure on line 119 in src/Converter/PaymentConverter.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 8.0

Using nullsafe method call on non-nullable type Sylius\Component\Core\Model\ShipmentInterface|false. Use -> instead.

Check failure on line 119 in src/Converter/PaymentConverter.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 5.7

Cannot call method getMethod() on Sylius\Component\Core\Model\ShipmentInterface|false.

Check failure on line 119 in src/Converter/PaymentConverter.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 5.7

Using nullsafe method call on non-nullable type Sylius\Component\Core\Model\ShipmentInterface|false. Use -> instead.

Check failure on line 119 in src/Converter/PaymentConverter.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 8.0

Cannot call method getMethod() on Sylius\Component\Core\Model\ShipmentInterface|false.

Check failure on line 119 in src/Converter/PaymentConverter.php

View workflow job for this annotation

GitHub Actions / Sylius ~1.12.0, PHP 8.2, Symfony ^6.4, MySQL 8.0

Using nullsafe method call on non-nullable type Sylius\Component\Core\Model\ShipmentInterface|false. Use -> instead.
1,
$taxRate,
Amount::fromSyliusAmount($totalAmount),
Amount::fromSyliusAmount($order->getAdjustmentsTotalRecursively(AdjustmentInterface::ORDER_SHIPPING_PROMOTION_ADJUSTMENT)),
Amount::fromSyliusAmount((int) $shippingTaxTotal),
Amount::fromSyliusAmount($totalAmount),
null,
null,
null,
null,
null,
OrderLineType::ShippingFee,
);

return $lines;
}

Expand All @@ -117,7 +139,7 @@ private function createOrderLineFromOrderItem(OrderItemInterface $orderItem): Or
return new OrderLine(
(string) $orderItem->getProductName(),
$orderItem->getQuantity(),
2200,
2200, #TODO
Amount::fromSyliusAmount($orderItem->getTotal()),
Amount::fromSyliusAmount(0),
Amount::fromSyliusAmount($orderItem->getTaxTotal()),
Expand All @@ -127,7 +149,7 @@ private function createOrderLineFromOrderItem(OrderItemInterface $orderItem): Or
null,
'pcs',
$orderItem->getProduct()?->getCode(),
'physical',
OrderLineType::Physical,
);
}

Expand Down

0 comments on commit fddbf68

Please sign in to comment.