Skip to content

Commit

Permalink
Merge pull request #5 from Paazl/1.0.6
Browse files Browse the repository at this point in the history
1.0.6
  • Loading branch information
Marvin-Magmodules authored Jul 16, 2019
2 parents 81552db + 444bd2b commit f7734c0
Show file tree
Hide file tree
Showing 26 changed files with 794 additions and 150 deletions.
76 changes: 76 additions & 0 deletions Api/Data/Quote/QuoteReferenceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Paazl\CheckoutWidget\Api\Data\Quote;

/**
* Interface QuoteReferenceInterface
*
* @package Paazl\CheckoutWidget\Api\Data\Quote
*/
interface QuoteReferenceInterface
{

/**#@+
* Indexes of fields
*
* @var string
*/
const ENTITY_ID = 'entity_id';
const QUOTE_ID = 'quote_id';
const EXT_SHIPPING_INFO = 'ext_shipping_info';
const TOKEN = 'token';
const TOKEN_EXPIRES_AT = 'token_expires_at';
/**#@-*/

/**
* @return int|null
*/
public function getQuoteId();

/**
* @param int $value
*
* @return $this
*/
public function setQuoteId(int $value);

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

/**
* @return string
*/
public function getExtShippingInfo();

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

/**
* @return string
*/
public function getToken();

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

/**
* @return string
*/
public function getTokenExpiresAt();
}
35 changes: 35 additions & 0 deletions Api/QuoteReferenceRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Paazl\CheckoutWidget\Api;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Paazl\CheckoutWidget\Api\Data\Quote\QuoteReferenceInterface;

/**
* Interface QuoteReferenceRepositoryInterface
* @package Paazl\CheckoutWidget\Api
*/
interface QuoteReferenceRepositoryInterface
{

/**
* @param $quoteId
*
* @return QuoteReferenceInterface
* @throws NoSuchEntityException
*/
public function getByQuoteId($quoteId);

/**
* @param QuoteReferenceInterface $reference
*
* @return QuoteReferenceInterface
* @throws CouldNotSaveException
*/
public function save(QuoteReferenceInterface $reference);
}
87 changes: 42 additions & 45 deletions Model/Api/PaazlApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace Paazl\CheckoutWidget\Model\Api;

use Magento\Framework\HTTP\Adapter\CurlFactory;
use Magento\Framework\HTTP\ClientFactory;
use Magento\Framework\HTTP\ClientInterface;
use Paazl\CheckoutWidget\Helper\General as GeneralHelper;
use Paazl\CheckoutWidget\Model\Api\Response\Data\Token;
use Paazl\CheckoutWidget\Model\Api\Response\Data\TokenBuilder;
Expand All @@ -26,11 +26,6 @@ class PaazlApi
*/
private $scopeConfig;

/**
* @var CurlFactory
*/
private $adapter;

/**
* @var array
*/
Expand Down Expand Up @@ -61,21 +56,18 @@ class PaazlApi
*
* @param Config $scopeConfig
* @param GeneralHelper $generalHelper
* @param CurlFactory $adapter
* @param ClientFactory $httpClientFactory
* @param TokenBuilder $tokenBuilder
* @param UrlProvider $urlProvider
*/
public function __construct(
Config $scopeConfig,
GeneralHelper $generalHelper,
CurlFactory $adapter,
ClientFactory $httpClientFactory,
TokenBuilder $tokenBuilder,
UrlProvider $urlProvider
) {
$this->scopeConfig = $scopeConfig;
$this->adapter = $adapter;
$this->generalHelper = $generalHelper;
$this->httpClientFactory = $httpClientFactory;
$this->tokenBuilder = $tokenBuilder;
Expand All @@ -93,26 +85,19 @@ public function getApiToken($reference)
{
$url = $this->urlProvider->getCheckoutTokenUrl();

$httpAdapter = $this->adapter->create();
$httpClient = $this->getAuthorizedClient();

try {
$this->request['reference'] = $reference;
$this->generalHelper->addTolog('Token request', $this->request);
$httpAdapter->write(
\Zend_Http_Client::POST,
$url,
'1.1',
[
'Content-Type: application/json;charset=UTF-8',
'Accept: application/json;charset=UTF-8',
'Authorization: ' . $this->buildAuthorizationHeader()
],
json_encode($this->request)
);

$response = $httpAdapter->read();
$body = \Zend_Http_Response::extractBody($response);
$status = \Zend_Http_Response::extractCode($response);

$httpClient->addHeader('Content-Type', 'application/json;charset=UTF-8');
$httpClient->addHeader('Accept', 'application/json;charset=UTF-8');

$httpClient->post($url, json_encode($this->request));
$body = $httpClient->getBody();
$status = $httpClient->getStatus();

$this->generalHelper->addTolog('Token response', $body);
if ($status >= 200 && $status < 300) {
/** @var Token $token */
Expand All @@ -137,22 +122,17 @@ public function addOrder(array $orderData)
{
$url = $this->urlProvider->getOrderUrl();

$httpAdapter = $this->adapter->create();
$httpClient = $this->getAuthorizedClient();

$this->generalHelper->addTolog('Order request', $orderData);
$httpAdapter->write(
\Zend_Http_Client::POST,
$url,
'1.1',
[
'Content-Type: application/json;charset=UTF-8',
'Accept: application/json;charset=UTF-8',
'Authorization: ' . $this->buildAuthorizationHeader()
],
json_encode($orderData)
);
$response = $httpAdapter->read();
$body = \Zend_Http_Response::extractBody($response);
$status = \Zend_Http_Response::extractCode($response);

$httpClient->addHeader('Content-Type', 'application/json;charset=UTF-8');
$httpClient->addHeader('Accept', 'application/json;charset=UTF-8');

$httpClient->post($url, json_encode($orderData));
$body = $httpClient->getBody();
$status = $httpClient->getStatus();

$this->generalHelper->addTolog('debug', $body);
if ($status >= 400 && $status < 500) {
throw ApiException::fromErrorResponse($body, $status);
Expand All @@ -175,16 +155,15 @@ public function fetchCheckoutData($reference)
{
$url = $this->urlProvider->getCheckoutUrl();

$httpClient = $this->httpClientFactory->create();
$httpClient = $this->getAuthorizedClient();
$result = null;
try {
$url .= '?' . http_build_query([
'reference' => $reference
]);
$httpClient->setHeaders([
'Accept' => 'application/json;charset=UTF-8',
'Authorization' => $this->buildAuthorizationHeader()
]);

$httpClient->addHeader('Accept', 'application/json;charset=UTF-8');

$httpClient->get($url);
$status = $httpClient->getStatus();
$body = $httpClient->getBody();
Expand Down Expand Up @@ -225,4 +204,22 @@ public function getApiSecret()
{
return $this->scopeConfig->getApiSecret();
}

/**
* @return ClientInterface
*/
private function getAuthorizedClient()
{
$httpClient = $this->httpClientFactory->create();
$httpClient->setHeaders([
'Authorization' => $this->buildAuthorizationHeader()
]);

$timeout = $this->scopeConfig->getApiTimeout();
if ($timeout > 0) {
$httpClient->setTimeout($timeout);
}

return $httpClient;
}
}
52 changes: 52 additions & 0 deletions Model/Checkout/LanguageProvider.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\Checkout;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\Locale\ResolverInterface;

/**
* Class LanguageProvider
* @package Paazl\CheckoutWidget\Model\Checkout
*/
class LanguageProvider implements ConfigProviderInterface
{

/**
* @var ResolverInterface
*/
private $resolver;

/**
* LanguageProvider constructor.
*
* @param ResolverInterface $resolver
*/
public function __construct(
ResolverInterface $resolver
) {
$this->resolver = $resolver;
}

/**
* Retrieve assoc array of checkout configuration
*
* @return array
*/
public function getConfig()
{
$languageCode = 'en';
if ($this->resolver->getLocale()) {
$locale = $this->resolver->getLocale();
$languageCode = explode('_', $locale)[0];
}

return [
'language' => $languageCode,
];
}
}
6 changes: 6 additions & 0 deletions Model/Checkout/PaazlConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public function getConfig()
$config['baseApiUrl'] = $this->urlProvider->getBaseUrl();
$config['googleMapKey'] = $this->config->getGoogleMapKey();
$config['widgetConfig'] = $this->widgetConfigProvider->getConfig();
$config['mode'] = $this->config->isProductionApiMode() ? 'live' : 'test';

if (empty($config['widgetConfig']['token'])) {
// We were unable to obtain a token - enabling other methods if they're available
$config['hideOtherMethods'] = false;
}

return [
'paazlshipping' => $config
Expand Down
Loading

0 comments on commit f7734c0

Please sign in to comment.