Skip to content

Commit

Permalink
Merge pull request #14 from NordicWebTeam/main
Browse files Browse the repository at this point in the history
Composer 1.5.3
  • Loading branch information
JenniferGus authored Aug 23, 2024
2 parents 8ab4cc7 + eec1fb8 commit a178d2f
Show file tree
Hide file tree
Showing 10 changed files with 820 additions and 95 deletions.
53 changes: 27 additions & 26 deletions Model/Carrier/Unifaun.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/
namespace Qliro\QliroOne\Model\Carrier;

use Magento\Checkout\Model\Session;
use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Rate\Result;
use Qliro\QliroOne\Api\LinkRepositoryInterface;
use Qliro\QliroOne\Model\Config;
use Magento\Quote\Api\CartRepositoryInterface;

class Unifaun extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
\Magento\Shipping\Model\Carrier\CarrierInterface
Expand All @@ -31,10 +31,6 @@ class Unifaun extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
* @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
*/
protected $_rateMethodFactory;
/**
* @var Session
*/
private $checkoutSession;
/**
* @var LinkRepositoryInterface
*/
Expand All @@ -43,6 +39,17 @@ class Unifaun extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
* @var Config
*/
private $qliroConfig;
/**
* @var CartRepositoryInterface
*/
private $quoteRepository;

/**
* @var
*/
private $quoteId;



/**
* Shipping constructor.
Expand All @@ -52,9 +59,9 @@ class Unifaun extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory
* @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
* @param Session $checkoutSession
* @param LinkRepositoryInterface $linkRepository
* @param Config $qliroConfig
* @param Cart $name
* @param CartRepositoryInterface $quoteRepository
* @param array $data
*/
public function __construct(
Expand All @@ -63,16 +70,16 @@ public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
Session $checkoutSession,
LinkRepositoryInterface $linkRepository,
CartRepositoryInterface $quoteRepository,
Config $qliroConfig,
array $data = []
) {
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
$this->checkoutSession = $checkoutSession;
$this->linkRepository = $linkRepository;
$this->quoteRepository = $quoteRepository;
$this->qliroConfig = $qliroConfig;
}

Expand All @@ -90,9 +97,9 @@ public function getAllowedMethods()
*/
private function getShippingPrice()
{
$quote = $this->getQuote();
$quoteId = $this->quoteId;
try {
$link = $this->linkRepository->getByQuoteId($quote->getId());
$link = $this->linkRepository->getByQuoteId($quoteId);
if ($link->getUnifaunShippingAmount()) {
$shippingPrice = $link->getUnifaunShippingAmount();
} else {
Expand Down Expand Up @@ -129,29 +136,23 @@ public function collectRates(RateRequest $request)

$method->setMethod($this->_code);
$method->setMethodTitle($this->getConfigData('name'));
if(count($request->getAllItems())){
$this->quoteId = $request->getAllItems()[0]->getQuoteId();
$quote = $this->quoteRepository->get($this->quoteId);
if($quote->getShippingAddress()->getShippingDescription() && strpos($quote->getShippingAddress()->getShippingDescription(), 'Unifaun -') !== false) {
$shipingMethod = explode(' - ', $quote->getShippingAddress()->getShippingDescription());
$method->setCarrierTitle($shipingMethod[0]);
$method->setMethodTitle($shipingMethod[1]);
}
}

$amount = $this->getShippingPrice();

$method->setPrice($amount);
$method->setCost($amount);
if($this->getQuote()->getShippingDescription() && strpos($this->getQuote()->getShippingDescription(), 'Unifaun -') !== false) {
$shipingMethod = explode(' - ', $this->getQuote()->getShippingDescription());
$method->setCarrierTitle($shipingMethod[0]);
$method->setMethodTitle($shipingMethod[1]);
}

$result->append($method);

return $result;
}

/**
* Get current quote from checkout session
*
* @return \Magento\Quote\Model\Quote
*/
private function getQuote()
{
return $this->checkoutSession->getQuote();
}
}
69 changes: 58 additions & 11 deletions Model/Management/CreateMerchantPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Qliro\QliroOne\Model\Logger\Manager;
use Qliro\QliroOne\Model\Api\Client\OrderManagement;
use Qliro\QliroOne\Service\General\LinkService;
use Qliro\QliroOne\Model\Management\PlaceRecurringOrder;

class CreateMerchantPayment extends AbstractManagement
{
Expand Down Expand Up @@ -49,14 +50,22 @@ class CreateMerchantPayment extends AbstractManagement
*/
private Manager $logManager;

/**
* @var \Qliro\QliroOne\Model\Management\PlaceRecurringOrder
*/
private $placeOrder;

private $order;

public function __construct(
LinkService $linkService,
CreateRequestBuilder $createRequestBuilder,
LinkInterfaceFactory $linkFactory,
LinkRepositoryInterface $linkRepository,
CartManagementInterface $quoteManagement,
OrderManagement $qliroOrderManagement,
Manager $logManager
Manager $logManager,
PlaceRecurringOrder $placeOrder
) {
$this->linkService = $linkService;
$this->createRequestBuilder = $createRequestBuilder;
Expand All @@ -65,6 +74,7 @@ public function __construct(
$this->logManager = $logManager;
$this->quoteManagement = $quoteManagement;
$this->qliroOrderManagement = $qliroOrderManagement;
$this->placeOrder = $placeOrder;
}

/**
Expand All @@ -75,14 +85,15 @@ public function __construct(
*/
public function execute(): void
{
$order = $this->getOrder();
$quote = $this->getQuote();
$quoteId = $quote->getEntityId();

$orderReference = $this->linkService->generateOrderReference($quote);
$this->logManager->setMerchantReference($orderReference);
$this->logManager->setMark('CREATE MERCHANT PAYMENT');

$request = $this->createRequestBuilder->setQuote($quote)->create();
$request = $this->createRequestBuilder->setQuote($quote)->setOrder($order)->create();
$request->setMerchantReference($orderReference);

$merchantPaymentResponse = null;
Expand All @@ -92,23 +103,59 @@ public function execute(): void
$request,
(int)$quote->getStoreId()
);
$newOrderId = $this->quoteManagement->placeOrder($quote->getId());

$link = $this->linkFactory->create();

// A real Quote Snapshot is not needed here but the value is required
$link->setQuoteSnapshot('merchantPayment');
$qliroOrderId = $merchantPaymentResponse->getOrderId();
$paymentTransactions = $merchantPaymentResponse->getPaymentTransactions();
$state = $paymentTransactions[0]->getStatus();
$qliroOrder = $this->qliroOrderManagement->getOrder($qliroOrderId);

$link = $this->linkFactory->create();
$link->setQuoteSnapshot('merchantPayment');// A real Quote Snapshot is not needed here but the value is required
$link->setQuoteId($quoteId);
$link->setReference($orderReference);
$link->setQliroOrderId($merchantPaymentResponse->getOrderId());
$link->setOrderId($newOrderId);
$link->setReference($qliroOrder->getMerchantReference());
$link->setQliroOrderId($qliroOrderId);
$link->setQliroOrderStatus(self::DEFAULT_QLIRO_STATUS);
$link->setIsActive(1); // The convention is setting the link as Active if the order is placed without errors
$orderItems = $qliroOrder->getOrderItemActions();
foreach ($orderItems as $orderItem) {
if($orderItem->getType() == 'Shipping')
$link->setUnifaunShippingAmount($orderItem->getPricePerItemIncVat());
}
$this->linkRepository->save($link);


$this->placeOrder->setQuote($quote);
$magentoOrder = $this->placeOrder->execute($qliroOrder, $state);
} catch (\Exception $exception) {
$this->logManager->critical($exception->getMessage());
return;
}
}

/**
* Get the order from the management class
*
* @return \Magento\Sales\Model\Order
*/
public function getOrder()
{
if (!($this->order instanceof \Magento\Sales\Model\Order)) {
throw new \LogicException('Order must be set before it is fetched.');
}

return $this->order;
}

/**
* Set the order in the management class
*
* @param \Magento\Sales\Model\Order $order
* @return $this
*/
public function setOrder($order)
{
$order->setFirstCaptureFlag(true);
$this->order = $order;

return $this;
}
}
Loading

0 comments on commit a178d2f

Please sign in to comment.