Skip to content

Commit

Permalink
Merge pull request #18 from NordicWebTeam/main
Browse files Browse the repository at this point in the history
Composer 1.5.7
  • Loading branch information
louisewes authored Nov 5, 2024
2 parents 6cb1dd9 + e7b8743 commit df05852
Show file tree
Hide file tree
Showing 28 changed files with 592 additions and 27 deletions.
16 changes: 16 additions & 0 deletions Api/Data/LinkInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface LinkInterface
const FIELD_MESSAGE= 'message';
const FIELD_PLACED_AT = 'placed_at';
const FIELD_UNIFAUN_SHIPPING_AMOUNT = 'unifaun_shipping_amount';
const FIELD_INGRID_SHIPPING_AMOUNT = 'ingrid_shipping_amount';

/**
* Get ID
Expand Down Expand Up @@ -126,6 +127,13 @@ public function getMessage();
*/
public function getUnifaunShippingAmount();

/**
* Get ingrid shipping amount
*
* @return float
*/
public function getIngridShippingAmount();

/**
* Set ID
*
Expand Down Expand Up @@ -237,4 +245,12 @@ public function setMessage($value);
* @return $this
*/
public function setUnifaunShippingAmount($value);

/**
* Set ingrid shipping amount
*
* @var float $value
* @return $this
*/
public function setIngridShippingAmount($value);
}
11 changes: 11 additions & 0 deletions Api/Data/QliroOrderShippingMethodInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function getPriceIncVat();
*/
public function getPriceExVat();

/**
* @return float
*/
public function getOriginalPrice();

/**
* @return array
*/
Expand Down Expand Up @@ -87,6 +92,12 @@ public function setPriceIncVat($value);
*/
public function setPriceExVat($value);

/**
* @param float $value
* @return $this
*/
public function setOriginalPrice($value);

/**
* @param array $value
* @return $this
Expand Down
8 changes: 6 additions & 2 deletions Controller/Qliro/Ajax/UpdateShippingMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,16 @@ public function execute()
$data = $this->dataHelper->readPreparedPayload($request, 'AJAX:UPDATE_SHIPPING_METHOD');

try {
$secondaryOption = null;
if ($this->qliroConfig->isUnifaunEnabled($quote->getStoreId())) {
$shippingMethodCode = \Qliro\QliroOne\Model\Carrier\Unifaun::QLIRO_UNIFAUN_SHIPPING_CODE;
$secondaryOption = $data['secondaryOption'] ?? null;
} else if ($this->qliroConfig->isIngridEnabled($quote->getStoreId())) {
$shippingMethodCode = \Qliro\QliroOne\Model\Carrier\Ingrid::QLIRO_INGRID_SHIPPING_CODE;
$secondaryOption = $data['methodName'] ?? null;
} else {
$shippingMethodCode = $data['method'] ?? null;
$shippingMethodCode = $data['method_code'] . "_" .$data['carrier_code'] ?? null;
}
$secondaryOption = $data['secondaryOption'] ?? null;
$shippingPrice = $data['price'] ?? null;
$result = $this->qliroManagement->setQuote($quote)->updateShippingMethod($shippingMethodCode, $secondaryOption, $shippingPrice);
} catch (\Exception $exception) {
Expand Down
158 changes: 158 additions & 0 deletions Model/Carrier/Ingrid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
/**
* Copyright © Qliro AB. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace Qliro\QliroOne\Model\Carrier;

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 Ingrid extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
\Magento\Shipping\Model\Carrier\CarrierInterface
{
const QLIRO_INGRID_SHIPPING = 'qliroingrid';
const QLIRO_INGRID_SHIPPING_CODE = self::QLIRO_INGRID_SHIPPING . '_' . self::QLIRO_INGRID_SHIPPING;

/**
* @var string
*/
protected $_code = self::QLIRO_INGRID_SHIPPING;

/**
* @var \Magento\Shipping\Model\Rate\ResultFactory
*/
protected $_rateResultFactory;

/**
* @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
*/
protected $_rateMethodFactory;
/**
* @var LinkRepositoryInterface
*/
private $linkRepository;
/**
* @var Config
*/
private $qliroConfig;
/**
* @var CartRepositoryInterface
*/
private $quoteRepository;

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



/**
* Shipping constructor.
*
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
* @param \Psr\Log\LoggerInterface $logger
* @param \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory
* @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
* @param LinkRepositoryInterface $linkRepository
* @param Cart $name
* @param CartRepositoryInterface $quoteRepository
* @param array $data
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
\Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
LinkRepositoryInterface $linkRepository,
CartRepositoryInterface $quoteRepository,
Config $qliroConfig,
array $data = []
) {
$this->_rateResultFactory = $rateResultFactory;
$this->_rateMethodFactory = $rateMethodFactory;
parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
$this->linkRepository = $linkRepository;
$this->quoteRepository = $quoteRepository;
$this->qliroConfig = $qliroConfig;
}

/**
* get allowed methods
* @return array
*/
public function getAllowedMethods()
{
return [$this->_code => $this->getConfigData('name')];
}

/**
* @return float
*/
private function getShippingPrice()
{
$quoteId = $this->quoteId;
try {
$link = $this->linkRepository->getByQuoteId($quoteId);
if ($link->getIngridShippingAmount()) {
$shippingPrice = $link->getIngridShippingAmount();
} else {
$configPrice = $this->getConfigData('price');
$shippingPrice = $this->getFinalPriceWithHandlingFee($configPrice);
}
} catch (\Exception $exception) {
$configPrice = $this->getConfigData('price');
$shippingPrice = $this->getFinalPriceWithHandlingFee($configPrice);
}

return $shippingPrice;
}

/**
* @param RateRequest $request
* @return bool|Result
*/
public function collectRates(RateRequest $request)
{
if (!$this->getConfigFlag('active') ||
!$this->qliroConfig->isIngridEnabled($this->getStore())) {
return false;
}

/** @var \Magento\Shipping\Model\Rate\Result $result */
$result = $this->_rateResultFactory->create();

/** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
$method = $this->_rateMethodFactory->create();

$method->setCarrier($this->_code);
$method->setCarrierTitle($this->getConfigData('title'));

$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(), 'Ingrid - ') !== false) {
$shipingMethod = explode(' - ', $quote->getShippingAddress()->getShippingDescription());
$method->setCarrierTitle($shipingMethod[0]);
$method->setMethodTitle($shipingMethod[1]);
}
}

$amount = $this->getShippingPrice();

$method->setPrice($amount);
$method->setCost($amount);

$result->append($method);

return $result;
}
}
11 changes: 11 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class Config
const QLIROONE_UNIFAUN_CHECKOUT_ID = 'unifaun/checkout_id';
const QLIROONE_UNIFAUN_PARAMETERS = 'unifaun/parameters';

const QLIROONE_INGRID_ENABLED = 'ingrid/enable';

const QLIROONE_RECURRING_ENABLE = 'recurring_payments/enable';
const QLIROONE_RECURRING_FREQUENCY_OPTIONS = 'recurring_payments/frequency_options';

Expand Down Expand Up @@ -654,6 +656,15 @@ public function getUnifaunParameters($storeId = null)
return [];
}

/**
* @param int|null $storeId
* @return bool
*/
public function isIngridEnabled($storeId)
{
return (bool)$this->adapter->getConfigData(self::QLIROONE_INGRID_ENABLED, $storeId);
}

/**
* @param int|null $storeId
* @return bool
Expand Down
5 changes: 3 additions & 2 deletions Model/ContainerMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ private function iterateArray($data, $setterType = false)
$container = $this->fromArray($value, $className);
$value = $container;
}

$data[$key] = $value;
if(!is_null($value)){
$data[$key] = $value;
}
}

return $data;
Expand Down
16 changes: 16 additions & 0 deletions Model/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ public function getUnifaunShippingAmount()
return $this->getData(self::FIELD_UNIFAUN_SHIPPING_AMOUNT);
}

/**
* @inheritdoc
*/
public function getIngridShippingAmount()
{
return $this->getData(self::FIELD_INGRID_SHIPPING_AMOUNT);
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -240,4 +248,12 @@ public function setUnifaunShippingAmount($value)
{
return $this->setData(self::FIELD_UNIFAUN_SHIPPING_AMOUNT, $value);
}

/**
* @inheritdoc
*/
public function setIngridShippingAmount($value)
{
return $this->setData(self::FIELD_INGRID_SHIPPING_AMOUNT, $value);
}
}
34 changes: 28 additions & 6 deletions Model/Management/CheckoutStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ class CheckoutStatus extends AbstractManagement
*/
private $qliroOrder;

/**
* @var int|string|null
*/
private $qliroOrderId = null;

/**
* @var bool
*/
private $orderLocked = false;

/**
* Inject dependencies
*
Expand Down Expand Up @@ -108,11 +118,9 @@ public function update(CheckoutStatusInterface $checkoutStatus)
],
];

$this->orderLocked = false;
$this->qliroOrderId = $qliroOrderId;
try {
if (!$this->lock->lock($qliroOrderId)) {
throw new FailToLockException(__('Failed to aquire lock when placing order'));
}

try {
$link = $this->linkRepository->getByQliroOrderId($qliroOrderId);
} catch (NoSuchEntityException $exception) {
Expand Down Expand Up @@ -150,6 +158,11 @@ public function update(CheckoutStatusInterface $checkoutStatus)
}

if (!$tooEarly) {
if (!$this->lock->lock($qliroOrderId)) {
throw new FailToLockException(__('Failed to aquire lock when placing order'));
}

$this->orderLocked = true;
$responseContainer = $this->merchantApi->getOrder($qliroOrderId);
$this->placeOrder->execute($responseContainer);

Expand All @@ -175,6 +188,11 @@ public function update(CheckoutStatusInterface $checkoutStatus)
* Second major scenario:
* The order already exists; if the status is OnHold or Refused, Order status should be updated
*/
if (!$this->lock->lock($qliroOrderId)) {
throw new FailToLockException(__('Failed to aquire lock when updating order status'));
}

$this->orderLocked = true;
if ($this->placeOrder->applyQliroOrderStatus($this->orderRepository->get($orderId))) {
$response = $this->checkoutStatusRespond(CheckoutStatusResponseInterface::RESPONSE_RECEIVED);
} else {
Expand All @@ -187,7 +205,6 @@ public function update(CheckoutStatusInterface $checkoutStatus)
*/
$response = $this->checkoutStatusRespond(CheckoutStatusResponseInterface::RESPONSE_RECEIVED);
}
$this->lock->unlock($qliroOrderId);

} catch (NoSuchEntityException $exception) {
/* no more qliro pushes should be sent */
Expand All @@ -197,7 +214,7 @@ public function update(CheckoutStatusInterface $checkoutStatus)
/*
* Someone else is creating the order at the moment. Let Qliro try again in a few minutes.
*/
$this->logManager->info('Order is being created in another process', $logContext);
$this->logManager->info('Order is being created or updated in another process', $logContext);
$response = $this->checkoutStatusRespond(CheckoutStatusResponseInterface::RESPONSE_ORDER_PENDING);

} catch (\Exception $exception) {
Expand Down Expand Up @@ -254,6 +271,11 @@ private function checkoutStatusRespond($result, $code = 200)
$response = $this->checkoutStatusResponseFactory->create();
$response->setCallbackResponse($result);
$response->setCallbackResponseCode($code);
if ($this->orderLocked) {
$this->lock->unlock($this->qliroOrderId);
$this->orderLocked = false;
}
$this->qliroOrderId = null;
return $response;
}
}
Loading

0 comments on commit df05852

Please sign in to comment.