Skip to content

Commit

Permalink
added saveOrderTo Convertim
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik committed Oct 30, 2024
1 parent 5e67ca0 commit 623fb8a
Show file tree
Hide file tree
Showing 4 changed files with 372 additions and 1 deletion.
76 changes: 76 additions & 0 deletions src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
namespace Shopsys\ConvertimBundle\Controller;

use Convertim\Order\ConvertimOrderDataFactory;
use Convertim\Order\ConvertimSaveOrderResponse;
use Shopsys\ConvertimBundle\Model\Convertim\ConvertimConfigProvider;
use Shopsys\ConvertimBundle\Model\Convertim\ConvertimLogger;
use Shopsys\ConvertimBundle\Model\Convertim\Exception\ConvertimException;
use Shopsys\ConvertimBundle\Model\Order\OrderDetailFactory;
use Shopsys\ConvertimBundle\Model\Order\OrderFacade;
use Shopsys\ConvertimBundle\Model\Order\OrderValidator;
use Shopsys\FrameworkBundle\Component\Router\DomainRouterFactory;
use Shopsys\FrameworkBundle\Model\Order\OrderUrlGenerator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Throwable;

class OrderController extends AbstractConvertimController
Expand All @@ -22,12 +27,18 @@ class OrderController extends AbstractConvertimController
* @param \Shopsys\ConvertimBundle\Model\Order\OrderDetailFactory $orderDetailFactory
* @param \Shopsys\ConvertimBundle\Model\Convertim\ConvertimLogger $convertimLogger
* @param \Shopsys\ConvertimBundle\Model\Order\OrderValidator $orderValidator
* @param \Shopsys\ConvertimBundle\Model\Order\OrderFacade $orderFacade
* @param \Shopsys\FrameworkBundle\Component\Router\DomainRouterFactory $domainRouterFactory
* @param \Shopsys\FrameworkBundle\Model\Order\OrderUrlGenerator $orderUrlGenerator
* @param \Shopsys\ConvertimBundle\Model\Convertim\ConvertimConfigProvider $convertimConfigProvider
*/
public function __construct(
protected readonly OrderDetailFactory $orderDetailFactory,
protected readonly ConvertimLogger $convertimLogger,
protected readonly OrderValidator $orderValidator,
protected readonly OrderFacade $orderFacade,
protected readonly DomainRouterFactory $domainRouterFactory,
protected readonly OrderUrlGenerator $orderUrlGenerator,
ConvertimConfigProvider $convertimConfigProvider,
) {
parent::__construct($convertimConfigProvider);
Expand Down Expand Up @@ -75,4 +86,69 @@ public function checkOrder(Request $request): Response
return $this->convertimLogger->logGenericException($e);
}
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route('/save-order')]
public function saveOrder(Request $request): Response
{
if ($this->isProtectedRequest($request) === false) {
return $this->invalidAuthorizationResponse();
}

try {
$rawData = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$convertimOrderDataFactory = new ConvertimOrderDataFactory();
$convertimOrderData = $convertimOrderDataFactory->createConvertimOrderDataFromJsonArray($rawData);

$order = $this->orderFacade->saveOrder($convertimOrderData);

$router = $this->domainRouterFactory->getRouter($order->getDomainId());

$saveOrderResponse = new ConvertimSaveOrderResponse(
$order->getUuid(),
$order->getNumber(),
$this->orderUrlGenerator->getOrderDetailUrl($order),
$router->generate(
'front_order_paid',
[
'orderIdentifier' => $order->getUuid(),
'orderEmail' => $order->getEmail(),
'orderPaymentType' => $order->getPayment()->getType(),
'orderUrlHash' => $order->getUrlHash(),
'orderPaymentStatusPageValidityHash' => $order->getOrderPaymentStatusPageValidityHash(),
],
UrlGeneratorInterface::ABSOLUTE_URL,
),
$router->generate(
'front_order_paid',
[
'orderIdentifier' => $order->getUuid(),
'orderEmail' => $order->getEmail(),
'orderPaymentType' => $order->getPayment()->getType(),
'orderUrlHash' => $order->getUrlHash(),
'orderPaymentStatusPageValidityHash' => $order->getOrderPaymentStatusPageValidityHash(),
],
UrlGeneratorInterface::ABSOLUTE_URL,
),
$router->generate(
'front_order_sent',
[
'orderUuid' => $order->getUuid(),
'orderPaymentType' => $order->getPayment()->getType(),
],
UrlGeneratorInterface::ABSOLUTE_URL,
),
[],
);

return new JsonResponse($saveOrderResponse);
} catch (ConvertimException $e) {
return $this->convertimLogger->logConvertimException($e);
} catch (Throwable $e) {
return $this->convertimLogger->logGenericException($e);
}
}
}
2 changes: 1 addition & 1 deletion src/Model/Order/ConvertimOrderDataToCartMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected function applyConvertimPaymentToCart(ConvertimOrderData $convertimOrde
protected function applyConvertimTransportToCart(ConvertimOrderData $convertimOrderData, Cart $cart): void
{
$transport = $this->transportFacade->getByUuid($convertimOrderData->getTransportData()->getUuid());
$cartTransportData = $this->cartTransportDataFactory->create($cart, $transport->getUuid(), null);
$cartTransportData = $this->cartTransportDataFactory->create($cart, $transport->getUuid(), $convertimOrderData->getTransportData()->getExtra()?->getPickUpPointCode());
$cartTransportData->watchedPrice = Money::create($convertimOrderData->getTransportData()->getPriceWithVat());

$cart->editCartTransport($cartTransportData);
Expand Down
258 changes: 258 additions & 0 deletions src/Model/Order/ConvertimOrderDataToOrderDataMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
<?php

declare(strict_types=1);

namespace Shopsys\ConvertimBundle\Model\Order;

use Convertim\Order\ConvertimOrderData;
use Convertim\Order\ConvertimOrderPaymentData;
use Convertim\Order\ConvertimOrderTransportData;
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Component\Money\Money;
use Shopsys\FrameworkBundle\Model\Cart\CartFacade;
use Shopsys\FrameworkBundle\Model\Country\Country;
use Shopsys\FrameworkBundle\Model\Country\CountryFacade;
use Shopsys\FrameworkBundle\Model\Country\Exception\CountryNotFoundException;
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserFacade;
use Shopsys\FrameworkBundle\Model\Order\Item\OrderItemDataFactory;
use Shopsys\FrameworkBundle\Model\Order\Item\OrderItemTypeEnum;
use Shopsys\FrameworkBundle\Model\Order\OrderData;
use Shopsys\FrameworkBundle\Model\Order\OrderDataFactory;
use Shopsys\FrameworkBundle\Model\Payment\PaymentFacade;
use Shopsys\FrameworkBundle\Model\Pricing\Currency\CurrencyFacade;
use Shopsys\FrameworkBundle\Model\Pricing\Price;
use Shopsys\FrameworkBundle\Model\Product\ProductFacade;
use Shopsys\FrameworkBundle\Model\Store\Exception\StoreByUuidNotFoundException;
use Shopsys\FrameworkBundle\Model\Store\StoreFacade;
use Shopsys\FrameworkBundle\Model\Transport\TransportFacade;

class ConvertimOrderDataToOrderDataMapper
{
/**
* @param \Shopsys\ConvertimBundle\Model\Order\ConvertimOrderDataToCartMapper $convertimOrderDataToCartMapper
* @param \Shopsys\FrameworkBundle\Model\Order\OrderDataFactory $orderDataFactory
* @param \Shopsys\FrameworkBundle\Model\Pricing\Currency\CurrencyFacade $currencyFacade
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\Cart\CartFacade $cartFacade
* @param \Shopsys\FrameworkBundle\Model\Country\CountryFacade $countryFacade
* @param \Shopsys\FrameworkBundle\Model\Order\Item\OrderItemDataFactory $orderItemDataFactory
* @param \Shopsys\FrameworkBundle\Model\Product\ProductFacade $productFacade
* @param \Shopsys\FrameworkBundle\Model\Transport\TransportFacade $transportFacade
* @param \Shopsys\FrameworkBundle\Model\Payment\PaymentFacade $paymentFacade
* @param \Shopsys\FrameworkBundle\Model\Store\StoreFacade $storeFacade
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserFacade $customerUserFacade
*/
public function __construct(
protected readonly ConvertimOrderDataToCartMapper $convertimOrderDataToCartMapper,
protected readonly OrderDataFactory $orderDataFactory,
protected readonly CurrencyFacade $currencyFacade,
protected readonly Domain $domain,
protected readonly CartFacade $cartFacade,
protected readonly CountryFacade $countryFacade,
protected readonly OrderItemDataFactory $orderItemDataFactory,
protected readonly ProductFacade $productFacade,
protected readonly TransportFacade $transportFacade,
protected readonly PaymentFacade $paymentFacade,
protected readonly StoreFacade $storeFacade,
protected readonly CustomerUserFacade $customerUserFacade,
) {
}

/**
* @param \Convertim\Order\ConvertimOrderData $convertimOrderData
* @return \Shopsys\FrameworkBundle\Model\Order\OrderData
*/
public function mapConvertimOrderDataToOrderData(ConvertimOrderData $convertimOrderData): OrderData
{
$convertimCustomerData = $convertimOrderData->getCustomerData();

$orderData = $this->orderDataFactory->create();
$orderData->domainId = $this->domain->getId();
$orderData->currency = $this->currencyFacade->getDomainDefaultCurrencyByDomainId($this->domain->getId());
$orderData->firstName = $convertimCustomerData->getFirstName();
$orderData->lastName = $convertimCustomerData->getLastName();
$orderData->email = $convertimCustomerData->getEmail();
$orderData->telephone = $convertimCustomerData->getTelephoneNumberWithPrefix();

$convertimBillingAddressData = $convertimCustomerData->getConvertimCustomerBillingAddressData();
$convertimDeliveryAddressData = $convertimCustomerData->getConvertimCustomerDeliveryAddressData();

if ($convertimBillingAddressData !== null) {
$orderData->companyName = $convertimBillingAddressData->getCompanyName();
$orderData->companyNumber = $convertimBillingAddressData->getIdentificationNumber();
$orderData->companyTaxNumber = $convertimBillingAddressData->getVatNumber();
$orderData->isCompanyCustomer = $convertimBillingAddressData->getCompanyName() !== null && $convertimBillingAddressData->getIdentificationNumber() !== null;
$orderData->street = $convertimBillingAddressData->getStreet();
$orderData->city = $convertimBillingAddressData->getCity();
$orderData->postcode = $convertimBillingAddressData->getPostcode();
$orderData->country = $this->getCountryByCode($convertimBillingAddressData->getCountry());
} else {
$orderData->companyName = $convertimDeliveryAddressData->getCompanyName();
$orderData->street = $convertimDeliveryAddressData->getStreet();
$orderData->city = $convertimDeliveryAddressData->getCity();
$orderData->postcode = $convertimDeliveryAddressData->getPostcode();
$orderData->country = $this->getCountryByCode($convertimDeliveryAddressData->getCountry());
}

$orderData->deliveryAddressSameAsBillingAddress = $convertimOrderData->isBillingAddressSameAsDeliveryAddress();

if (!$convertimOrderData->isBillingAddressSameAsDeliveryAddress()) {
$orderData->deliveryFirstName = $convertimDeliveryAddressData->getName();
$orderData->deliveryLastName = $convertimDeliveryAddressData->getLastName();
$orderData->deliveryCompanyName = $convertimDeliveryAddressData->getCompanyName();
$orderData->deliveryTelephone = $convertimDeliveryAddressData->getCurrierTelephoneNumberWithPrefix();
$orderData->deliveryStreet = $convertimDeliveryAddressData->getStreet();
$orderData->deliveryCity = $convertimDeliveryAddressData->getCity();
$orderData->deliveryPostcode = $convertimDeliveryAddressData->getPostCode();
$orderData->deliveryCountry = $this->getCountryByCode($convertimDeliveryAddressData->getCountry());
}

$orderData->note = $convertimOrderData->getNote();
$orderData->heurekaAgreement = !$convertimOrderData->isDisallowHeurekaVerifiedByCustomers();
$orderData->origin = 'Convertim';

$this->mapProducts($convertimOrderData, $orderData);
$this->mapConvertimTransportDataToOrderItem($convertimOrderData->getTransportData(), $orderData);
$this->mapConvertimPaymentDataToOrderItem($convertimOrderData->getPaymentData(), $orderData);
$this->mapPromoCodes($convertimOrderData, $orderData);

if ($convertimCustomerData->getCustomerEshopUuid() !== null) {
$orderData->customerUser = $this->customerUserFacade->getByUuid($convertimCustomerData->getCustomerEshopUuid());
}

return $orderData;
}

/**
* @param string|null $countryCode
* @return \Shopsys\FrameworkBundle\Model\Country\Country
*/
protected function getCountryByCode(?string $countryCode): Country
{
$countryReturn = null;
$i = 1;

foreach ($this->countryFacade->getAllEnabledOnCurrentDomain() as $country) {
if ($i === 1) {
$countryReturn = $country;
}

if ($country->getCode() === $countryCode) {
return $country;
}

$i++;
}

if ($countryReturn === null) {
throw new CountryNotFoundException('No country has been found on current domain.');
}

return $countryReturn;
}

/**
* @param \Convertim\Order\ConvertimOrderPaymentData $convertimOrderPaymentData
* @param \Shopsys\FrameworkBundle\Model\Order\OrderData $orderData
*/
protected function mapConvertimPaymentDataToOrderItem(
ConvertimOrderPaymentData $convertimOrderPaymentData,
OrderData $orderData,
): void {
$payment = $this->paymentFacade->getByUuid($convertimOrderPaymentData->getUuid());
$paymentOrderItemData = $this->orderItemDataFactory->create(OrderItemTypeEnum::TYPE_PAYMENT);
$paymentOrderItemData->payment = $payment;
$paymentOrderItemData->name = $payment->getName();
$paymentOrderItemData->quantity = 1;
$paymentOrderItemData->totalPriceWithVat = Money::create($convertimOrderPaymentData->getPriceWithVat());
$paymentOrderItemData->totalPriceWithoutVat = Money::create($convertimOrderPaymentData->getPriceWithoutVat());
$paymentOrderItemData->unitPriceWithVat = Money::create($convertimOrderPaymentData->getPriceWithVat());
$paymentOrderItemData->unitPriceWithoutVat = Money::create($convertimOrderPaymentData->getPriceWithoutVat());
$paymentOrderItemData->vatPercent = $payment->getVatForDomain($this->domain->getId())->getPercent();
$paymentOrderItemData->usePriceCalculation = false;

$orderData->addItem($paymentOrderItemData);
$orderData->orderPayment = $paymentOrderItemData;
$orderData->payment = $payment;
$orderData->addTotalPrice(new Price($paymentOrderItemData->totalPriceWithoutVat, $paymentOrderItemData->totalPriceWithVat), OrderItemTypeEnum::TYPE_PAYMENT);
}

/**
* @param \Convertim\Order\ConvertimOrderTransportData $convertimOrderTransportData
* @param \Shopsys\FrameworkBundle\Model\Order\OrderData $orderData
*/
protected function mapConvertimTransportDataToOrderItem(
ConvertimOrderTransportData $convertimOrderTransportData,
OrderData $orderData,
): void {
$transport = $this->transportFacade->getByUuid($convertimOrderTransportData->getUuid());

$transportOrderItemData = $this->orderItemDataFactory->create(OrderItemTypeEnum::TYPE_TRANSPORT);
$transportOrderItemData->transport = $transport;
$transportOrderItemData->name = $transport->getName();

$pickupPlaceIdentifier = $convertimOrderTransportData->getExtra()?->getPickUpPointCode();

if ($transport->isPersonalPickup()) {
try {
$store = $this->storeFacade->getByUuidAndDomainId(
$pickupPlaceIdentifier,
$this->domain->getId(),
);

$transportOrderItemData->name .= ' ' . $store->getName();
} catch (StoreByUuidNotFoundException) {
}
}

$transportOrderItemData->quantity = 1;
$transportOrderItemData->totalPriceWithVat = Money::create($convertimOrderTransportData->getPriceWithVat());
$transportOrderItemData->totalPriceWithoutVat = Money::create($convertimOrderTransportData->getPriceWithoutVat());
$transportOrderItemData->unitPriceWithVat = Money::create($convertimOrderTransportData->getPriceWithVat());
$transportOrderItemData->unitPriceWithoutVat = Money::create($convertimOrderTransportData->getPriceWithoutVat());
$transportOrderItemData->vatPercent = $transport->getVatForDomain($this->domain->getId())->getPercent();
$transportOrderItemData->usePriceCalculation = false;

$orderData->addItem($transportOrderItemData);
$orderData->orderTransport = $transportOrderItemData;
$orderData->transport = $transport;
$orderData->pickupPlaceIdentifier = $pickupPlaceIdentifier;
$orderData->addTotalPrice(new Price($transportOrderItemData->totalPriceWithoutVat, $transportOrderItemData->totalPriceWithVat), OrderItemTypeEnum::TYPE_TRANSPORT);
}

/**
* @param \Convertim\Order\ConvertimOrderData $convertimOrderData
* @param \Shopsys\FrameworkBundle\Model\Order\OrderData $orderData
*/
protected function mapProducts(ConvertimOrderData $convertimOrderData, OrderData $orderData): void
{
foreach ($convertimOrderData->getOrderItemsData() as $convertimOrderItemData) {
$product = $this->productFacade->getByUuid($convertimOrderItemData->getProductId());

$orderItemData = $this->orderItemDataFactory->create(OrderItemTypeEnum::TYPE_PRODUCT);
$orderItemData->product = $product;
$orderItemData->catnum = $product->getCatnum();
$orderItemData->name = $product->getName();
$orderItemData->unitName = $product->getUnit()->getName();
$orderItemData->totalPriceWithVat = (Money::create($convertimOrderItemData->getPriceWithVat())->multiply($convertimOrderItemData->getQuantity()));
$orderItemData->totalPriceWithoutVat = (Money::create($convertimOrderItemData->getPriceWithoutVat())->multiply($convertimOrderItemData->getQuantity()));
$orderItemData->quantity = $convertimOrderItemData->getQuantity();
$orderItemData->unitPriceWithVat = Money::create($convertimOrderItemData->getPriceWithVat());
$orderItemData->unitPriceWithoutVat = Money::create($convertimOrderItemData->getPriceWithoutVat());
$orderItemData->vatPercent = $product->getVatForDomain($this->domain->getId())->getPercent();
$orderItemData->usePriceCalculation = false;

$orderData->addItem($orderItemData);
$orderData->addTotalPrice(new Price($orderItemData->totalPriceWithoutVat, $orderItemData->totalPriceWithVat), OrderItemTypeEnum::TYPE_PRODUCT);
}
}

/**
* @param \Convertim\Order\ConvertimOrderData $convertimOrderData
* @param \Shopsys\FrameworkBundle\Model\Order\OrderData $orderData
*/
protected function mapPromoCodes(ConvertimOrderData $convertimOrderData, OrderData $orderData): void
{
$orderData->promoCode = $convertimOrderData->getPromoCodes()[0]->getCode();
}
}
Loading

0 comments on commit 623fb8a

Please sign in to comment.