Skip to content

Commit

Permalink
Merge pull request #57 from Paazl/1.6.0
Browse files Browse the repository at this point in the history
1.6.0
  • Loading branch information
Marvin-Magmodules authored Apr 23, 2021
2 parents 4237b07 + 8f059f6 commit c496f23
Show file tree
Hide file tree
Showing 20 changed files with 344 additions and 27 deletions.
13 changes: 13 additions & 0 deletions Api/Data/Order/OrderReferenceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface OrderReferenceInterface
const ORDER_ID = 'order_id';
const EXT_SHIPPING_INFO = 'ext_shipping_info';
const EXT_SENT_AT = 'ext_sent_at';
const EXT_INVALID = 'invalid';
/**#@-*/

/**
Expand Down Expand Up @@ -61,6 +62,18 @@ public function setExtSentAt($value);
*/
public function getExtSentAt();

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

/**
* @return int
*/
public function getInvalid();

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

namespace Paazl\CheckoutWidget\Api\Webapi;

use Magento\Framework\Exception\NoSuchEntityException;
use Paazl\CheckoutWidget\Api\Data\CheckQuoteResultInterface;

/**
* Interface CheckShippingOptionInterface
*/
interface CheckShippingOptionInterface
{
/**
* @param string $cartId
* @return CheckQuoteResultInterface
* @throws NoSuchEntityException
*/
public function get($cartId);
}
24 changes: 24 additions & 0 deletions Api/Webapi/GuestCheckShippingOptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Paazl\CheckoutWidget\Api\Webapi;

use Magento\Framework\Exception\NoSuchEntityException;
use Paazl\CheckoutWidget\Api\Data\CheckQuoteResultInterface;

/**
* Interface CheckShippingOptionInterface
*/
interface GuestCheckShippingOptionInterface
{
/**
* @param int $cartId
* @return CheckQuoteResultInterface
* @throws NoSuchEntityException
*/
public function get($cartId);
}
111 changes: 111 additions & 0 deletions Model/Api/CurlExtra.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* Copyright © 2019 Paazl. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Paazl\CheckoutWidget\Model\Api;

use Exception;
use Magento\Framework\HTTP\Client\Curl;

/**
* Class CurlExtra
*
* Provide PUT and DEL methods
*/
class CurlExtra extends Curl
{

/**
* Make PUT request using curl
* Magento has no built in support for 'put'
*
* @param string $uri
* @param array|string $params
* @return void
*
* @throws Exception
*/
public function put($uri, $params)
{
$this->makePutRequest($uri, $params);
}

/**
* Make DEL request using curl
* Magento has no built in support for 'delete'
*
* @param string $uri
*
* @return void
*/
public function del($uri)
{
$this->makeRequest("DELETE", $uri);
}

/**
* @param string $uri
* @param string|array $params
* @throws Exception
*/
protected function makePutRequest($uri, $params)
{
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$this->_ch = curl_init();
$this->curlOption(CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS | CURLPROTO_FTP | CURLPROTO_FTPS);
$this->curlOption(CURLOPT_URL, $uri);
$this->curlOption(CURLOPT_POSTFIELDS, is_array($params) ? http_build_query($params) : $params);
$this->curlOption(CURLOPT_CUSTOMREQUEST, 'PUT');
$this->curlOption(CURLOPT_RETURNTRANSFER, true);

if (count($this->_headers)) {
$heads = [];
foreach ($this->_headers as $k => $v) {
$heads[] = $k . ': ' . $v;
}
$this->curlOption(CURLOPT_HTTPHEADER, $heads);
}

if (count($this->_cookies)) {
$cookies = [];
foreach ($this->_cookies as $k => $v) {
$cookies[] = "{$k}={$v}";
}
$this->curlOption(CURLOPT_COOKIE, implode(";", $cookies));
}

if ($this->_timeout) {
$this->curlOption(CURLOPT_TIMEOUT, $this->_timeout);
}

if ($this->_port != 80) {
$this->curlOption(CURLOPT_PORT, $this->_port);
}

$this->curlOption(CURLOPT_RETURNTRANSFER, 1);
$this->curlOption(CURLOPT_HEADERFUNCTION, [$this, 'parseHeaders']);

if (count($this->_curlUserOptions)) {
foreach ($this->_curlUserOptions as $k => $v) {
$this->curlOption($k, $v);
}
}

$this->_headerCount = 0;
$this->_responseHeaders = [];

// phpcs:ignore Magento2.Functions.DiscouragedFunction
$this->_responseBody = curl_exec($this->_ch);
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$err = curl_errno($this->_ch);
if ($err) {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
$this->doError(curl_error($this->_ch));
}
// phpcs:ignore Magento2.Functions.DiscouragedFunction
curl_close($this->_ch);
}
}
4 changes: 2 additions & 2 deletions Model/Api/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace Paazl\CheckoutWidget\Model\Api\Http;

use Magento\Framework\HTTP\Client\Curl;
use Paazl\CheckoutWidget\Model\Api\CurlExtra;

/**
* Extension of the core's HTTP client - we need PUT method
*/
class Client extends Curl
class Client extends CurlExtra
{
private $skipNextHeader = false;

Expand Down
27 changes: 19 additions & 8 deletions Model/Api/PaazlApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,26 +115,37 @@ public function getApiToken($reference)
* Sends order to Paazl
*
* @param array $orderData
* @param bool $modify
* @return boolean
* @throws ApiException
*/
public function addOrder(array $orderData)
public function addOrder(array $orderData, bool $modify = false)
{
$url = $this->urlProvider->getOrderUrl();

$httpClient = $this->getAuthorizedClient();

$this->generalHelper->addTolog('AddOrder request: ', $orderData);

$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();
if ($modify == false) {
$this->generalHelper->addTolog('AddOrder request: ', $orderData);
$httpClient->post($url, json_encode($orderData));
$body = $httpClient->getBody();
$status = $httpClient->getStatus();

$this->generalHelper->addTolog('AddOrder response status: ', $status);
$this->generalHelper->addTolog('AddOrder response body: ', $body);
} else {
$this->generalHelper->addTolog('ModifyOrder request: ', $orderData);
$httpClient->put($url, json_encode($orderData));
$body = $httpClient->getBody();
$status = $httpClient->getStatus();

$this->generalHelper->addTolog('ModifyOrder response status: ', $status);
$this->generalHelper->addTolog('ModifyOrder response body: ', $body);
}

$this->generalHelper->addTolog('AddOrder response status: ', $status);
$this->generalHelper->addTolog('AddOrder response body: ', $body);
if ($status >= 400 && $status < 500) {
throw ApiException::fromErrorResponse($body, $status);
}
Expand Down
1 change: 1 addition & 0 deletions Model/Api/Processor/MarkOrderAsSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function process(OrderInterface $order)
}

$reference->setExtSentAt($this->dateTime->gmtDate());
$reference->setInvalid(0);

try {
$this->orderReferenceRepository->save($reference);
Expand Down
9 changes: 7 additions & 2 deletions Model/Api/Processor/SendToService.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,20 @@ public function process(OrderInterface $order, $force = false)
throw new LocalizedException(__('Reference information not found'));
}

if ($reference->isSent() && !$force) {
if ($reference->isSent() && !$force && !$reference->getInvalid()) {
throw new LocalizedException(__('Order was sent to Paazl already'));
}

try {
$orderInfo = $this->orderBuilder->getCreateOrderData($order);
/** @var PaazlApi $paazlApi */
$paazlApi = $this->paazlApiFactory->create($order->getStoreId());
$paazlApi->addOrder($orderInfo);
if ($reference->isSent() && $reference->getInvalid()) {
$modify = true;
} else {
$modify = false;
}
$paazlApi->addOrder($orderInfo, $modify);
$this->markOrderAsSent->process($order);
} catch (\Exception $e) {
$this->generalHelper->addTolog('exception', $e->getMessage());
Expand Down
18 changes: 18 additions & 0 deletions Model/Order/OrderReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ public function getExtSentAt()
return $this->getData(self::EXT_SENT_AT);
}

/**
* @param int $value
*
* @return $this
*/
public function setInvalid($value)
{
return $this->setData(self::EXT_INVALID, $value);
}

/**
* @return int
*/
public function getInvalid()
{
return (int)$this->getData(self::EXT_INVALID);
}

/**
* @return bool
*/
Expand Down
7 changes: 3 additions & 4 deletions Model/Webapi/CheckShippingOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
use Paazl\CheckoutWidget\Model\Quote\CheckQuoteShippingOption;
use Paazl\CheckoutWidget\Model\Webapi\CheckShippingOption\Result;
use Paazl\CheckoutWidget\Model\Webapi\CheckShippingOption\ResultFactory;
use Paazl\CheckoutWidget\Api\Webapi\CheckShippingOptionInterface;

class CheckShippingOption
class CheckShippingOption implements CheckShippingOptionInterface
{
/**
* @var CheckQuoteShippingOption
Expand Down Expand Up @@ -87,9 +88,7 @@ public function __construct(
}

/**
* @param integer $cartId
* @return \Paazl\CheckoutWidget\Api\Data\CheckQuoteResultInterface
* @throws NoSuchEntityException
* {@inheritDoc}
*/
public function get($cartId)
{
Expand Down
7 changes: 3 additions & 4 deletions Model/Webapi/GuestCheckShippingOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
use Magento\Quote\Model\QuoteIdMask;
use Magento\Quote\Model\QuoteIdMaskFactory;
use Paazl\CheckoutWidget\Api\Data\CheckQuoteResultInterface;
use Paazl\CheckoutWidget\Api\Webapi\GuestCheckShippingOptionInterface;

class GuestCheckShippingOption
class GuestCheckShippingOption implements GuestCheckShippingOptionInterface
{
/**
* @var QuoteIdMaskFactory
Expand All @@ -38,9 +39,7 @@ public function __construct(
}

/**
* @param string $cartId
* @return \Paazl\CheckoutWidget\Api\Data\CheckQuoteResultInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
* {@inheritDoc}
*/
public function get($cartId)
{
Expand Down
Loading

0 comments on commit c496f23

Please sign in to comment.