-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from Paazl/1.4.1
1.4.1
- Loading branch information
Showing
19 changed files
with
743 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,4 +230,4 @@ private function extractQuote(RateRequest $request) | |
|
||
return $quote; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.