Skip to content

Commit

Permalink
Merge pull request #53 from Paazl/1.4.1
Browse files Browse the repository at this point in the history
1.4.1
  • Loading branch information
Marvin-Magmodules authored Mar 1, 2021
2 parents 6ab69a9 + 94ddb31 commit fd6bc8e
Show file tree
Hide file tree
Showing 19 changed files with 743 additions and 38 deletions.
40 changes: 20 additions & 20 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
name: Unit Tests
name: Unit Tests

on: pull_request
on: pull_request

jobs:
build:
strategy:
matrix:
PHP_VERSION: [php71-fpm, php72-fpm]
MAGENTO_VERSION: [2.2.11, 2.3.4]
exclude:
- PHP_VERSION: php72-fpm
MAGENTO_VERSION: 2.2.11
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
jobs:
build:
strategy:
matrix:
PHP_VERSION: [php71-fpm, php72-fpm]
MAGENTO_VERSION: [2.2.11, 2.3.4]
exclude:
- PHP_VERSION: php72-fpm
MAGENTO_VERSION: 2.2.11
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: Start Docker
run: PHP_VERSION=${{ matrix.PHP_VERSION }} MAGENTO_VERSION=magento${{ matrix.MAGENTO_VERSION }} docker-compose -f .github/workflows/templates/docker-compose.yml up -d
- name: Start Docker
run: PHP_VERSION=${{ matrix.PHP_VERSION }} MAGENTO_VERSION=magento${{ matrix.MAGENTO_VERSION }} docker-compose -f .github/workflows/templates/docker-compose.yml up -d

- name: Upload our code into the docker container
run: docker cp $(pwd)/. magento-project-community-edition:/data/extensions && docker exec magento-project-community-edition composer require paazl/magento2-checkout-widget:@dev
- name: Upload our code into the docker container
run: docker cp $(pwd)/. magento-project-community-edition:/data/extensions/workdir && docker exec magento-project-community-edition composer require paazl/magento2-checkout-widget:@dev

- name: Run tests
run: docker exec magento-project-community-edition bash -c "vendor/bin/phpunit extensions/Test/Unit"
- name: Run tests
run: docker exec magento-project-community-edition bash -c "vendor/bin/phpunit extensions/workdir/Test/Unit"
52 changes: 52 additions & 0 deletions Api/Data/CheckQuoteResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Paazl\CheckoutWidget\Api\Data;

/**
* Interface CheckQuoteResultInterface
*
* @package Paazl\CheckoutWidget\Api\Data
*/
interface CheckQuoteResultInterface
{
const RELOAD_WIDGET = 'reload_widget';
const RELOAD_WIDGET_CONFIG_JSON = 'reload_widget_config';
const TOTALS = 'totals';

/**
* @return boolean
*/
public function getReloadWidget();

/**
* @param boolean $value
* @return $this
*/
public function setReloadWidget($value);

/**
* @return string
*/
public function getReloadWidgetConfigJson(): string;

/**
* @param string $value
* @return $this
*/
public function setReloadWidgetConfigJson(string $value);

/**
* @return \Magento\Quote\Api\Data\TotalsInterface|null
*/
public function getTotals();

/**
* @param \Magento\Quote\Api\Data\TotalsInterface $value
* @return $this
*/
public function setTotals($value);
}
14 changes: 10 additions & 4 deletions Model/Api/Builder/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function parseAddress(Address $shippingAddress)
if (!trim($parser)) {
continue;
}
if (!preg_match('~[0-9]+~' , $parser)
if (!preg_match('~[0-9]+~', $parser)
&& strlen($parser) > 2) {
$probably['street'][] = $parser;
} elseif (ctype_digit($parser)) {
Expand All @@ -254,9 +254,15 @@ public function parseAddress(Address $shippingAddress)
}

$probably = [
'street' => implode(' ', $probably['street']),
'houseNumber' => implode('', $probably['houseNumber']),
'houseNumberExtension' => implode('', $probably['houseNumberExtension'])
'street' => is_array($probably['street'])
? implode(' ', $probably['street'])
: $probably['street'],
'houseNumber' => is_array($probably['houseNumber'])
? implode('', $probably['houseNumber'])
: $probably['houseNumber'],
'houseNumberExtension' => is_array($probably['houseNumberExtension'])
? implode('', $probably['houseNumberExtension'])
: $probably['houseNumberExtension']
];
if ($probably['street'] && $probably['houseNumber']) {
return $probably;
Expand Down
52 changes: 52 additions & 0 deletions Model/Api/Converter/ShippingOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Paazl\CheckoutWidget\Model\Api\Converter;

use Magento\Framework\Serialize\Serializer\Json;
use Paazl\CheckoutWidget\Helper\General;

class ShippingOptions
{
/**
* @var Json
*/
private $json;

/**
* @var General
*/
private $generalHelper;

/**
* ShippingOptions constructor.
*
* @param Json $json
* @param General $generalHelper
*/
public function __construct(
Json $json,
General $generalHelper
) {
$this->json = $json;
$this->generalHelper = $generalHelper;
}

/**
* @param string|null $response
* @return array|null
*/
public function convert($response)
{
try {
return $this->json->unserialize($response);
} catch (\InvalidArgumentException $e) {
$this->generalHelper->addTolog('exception', $e->getMessage());
}

return null;
}
}
2 changes: 1 addition & 1 deletion Model/Carrier/Paazlshipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,4 @@ private function extractQuote(RateRequest $request)

return $quote;
}
}
}
27 changes: 23 additions & 4 deletions Model/Checkout/WidgetConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ class WidgetConfigProvider implements ConfigProviderInterface
*/
private $productRepository;

/**
* @var Quote
*/
private $quote;

/**
* Widget constructor.
*
Expand Down Expand Up @@ -169,9 +174,9 @@ public function getConfig()
"sortOrder" => "ASC"
],
"shipmentParameters" => [
"totalWeight" => $this->getTotalWeight(),
"totalPrice" => $this->getQuote()->getSubtotalWithDiscount(),
"numberOfGoods" => $this->getProductsCount(),
"totalWeight" => (float)$this->getTotalWeight(),
"totalPrice" => (float)$this->getQuote()->getSubtotalWithDiscount(),
"numberOfGoods" => (int)$this->getProductsCount(),
"goods" => $goods
],
"shippingOptionsLimit" => $this->getShippingOptionsLimit(),
Expand All @@ -192,7 +197,21 @@ public function getConfig()
*/
public function getQuote()
{
return $this->checkoutHelper->getQuote();
if (!$this->quote) {
return $this->checkoutHelper->getQuote();
}

return $this->quote;
}

/**
* @param Quote|null $quote
* @return $this
*/
public function setQuote(Quote $quote = null)
{
$this->quote = $quote;
return $this;
}

/**
Expand Down
156 changes: 156 additions & 0 deletions Model/Quote/CheckQuoteShippingOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Paazl\CheckoutWidget\Model\Quote;

use Exception;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote;
use Paazl\CheckoutWidget\Helper\General;
use Paazl\CheckoutWidget\Model\Api\Converter\ShippingOptions;
use Paazl\CheckoutWidget\Model\Api\Field\DeliveryType;
use Paazl\CheckoutWidget\Model\Api\PaazlApiFactory;
use Paazl\CheckoutWidget\Model\Carrier\Paazlshipping;
use Paazl\CheckoutWidget\Model\Checkout\WidgetConfigProvider;
use Paazl\CheckoutWidget\Model\ExtInfoHandler;

/**
* Class CheckQuoteShippingOption
*
* @package Paazl\CheckoutWidget\Model\Quote
*/
class CheckQuoteShippingOption
{
/**
* @var ExtInfoHandler
*/
private $extInfoHandler;

/**
* @var PaazlApiFactory
*/
private $apiFactory;

/**
* @var WidgetConfigProvider
*/
private $configProvider;

/**
* @var General
*/
private $generalHelper;

/**
* @var ShippingOptions
*/
private $shippingOptionsConverter;

/**
* CheckQuoteShippingOption constructor.
*
* @param ExtInfoHandler $extInfoHandler
* @param PaazlApiFactory $apiFactory
* @param WidgetConfigProvider $configProvider
* @param General $generalHelper
* @param ShippingOptions $shippingOptionsConverter
*/
public function __construct(
ExtInfoHandler $extInfoHandler,
PaazlApiFactory $apiFactory,
WidgetConfigProvider $configProvider,
General $generalHelper,
ShippingOptions $shippingOptionsConverter
) {
$this->extInfoHandler = $extInfoHandler;
$this->apiFactory = $apiFactory;
$this->configProvider = $configProvider;
$this->generalHelper = $generalHelper;
$this->shippingOptionsConverter = $shippingOptionsConverter;
}

/**
* @param Quote|CartInterface $quote
* @return DataObject
*/
public function validate(Quote $quote)
{
$result = new DataObject();
if (!$quote->getShippingAddress()) {
return $result;
}

$method = (string)$quote->getShippingAddress()->getShippingMethod();
if (strpos($method, Paazlshipping::CODE) === false) {
return $result;
}

$info = $this->extInfoHandler->getInfoFromQuote($quote);
if (!$info) {
return $result;
}

$optionIdentifier = $info->getIdenfifier();
if (($info->getType() !== DeliveryType::DELIVERY) || empty($optionIdentifier)) {
return $result;
}

// Re-retrieving shipping options from API
$api = $this->apiFactory->create($quote->getStoreId());
try {
$config = $this->configProvider->setQuote($quote)->getConfig();

$shippingOptions = $this->shippingOptionsConverter->convert(
$api->getShippingOptions($config)
);
} catch (LocalizedException $e) {
$this->generalHelper->addTolog('CheckQuoteShippingOption exception: ' . $e->getLogMessage(), [
'quoteId' => $quote->getId()
]);
return $result;
} catch (Exception $e) {
$this->generalHelper->addTolog('CheckQuoteShippingOption exception: ' . $e->getMessage(), [
'quoteId' => $quote->getId()
]);
return $result;
}

$options = $shippingOptions['shippingOptions'] ?? [];

$optionExists = false;
$optionExists = array_reduce($options, function ($carry, $item) use ($optionIdentifier) {
return $carry || (!empty($item['identifier']) && ($item['identifier'] == $optionIdentifier));
}, $optionExists);

if (!$optionExists) {
// Reset quote's shipping method to default one
$result->setReloadWidget(true);
return $result;
}

$newPrice = null;
$newPrice = array_reduce($options, function ($carry, $item) use ($optionIdentifier) {
return (!empty($item['identifier'])
&& ($item['identifier'] == $optionIdentifier)
&& array_key_exists('rate', $item))
? (float)$item['rate']
: $carry;
}, $newPrice);

if ($newPrice !== null && $newPrice !== (float)$info->getPrice()) {
// Price has changed - update info + recollect quote totals
$info->setPrice($newPrice);
$this->extInfoHandler->setInfoToQuote($info, $quote);
$result->setRecollectTotals(true);
return $result;
}

return $result;
}
}
Loading

0 comments on commit fd6bc8e

Please sign in to comment.