78 lines
2.3 KiB
PHP
Executable File
78 lines
2.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace IpSupply\SyncOrder\Sync;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\ClientException;
|
|
use GuzzleHttp\Psr7\Request as GuzzRequest;
|
|
use IpSupply\SyncOrder\Config\GetBaseURL;
|
|
use IpSupply\SyncOrder\Config\Getter;
|
|
|
|
final class Index
|
|
{
|
|
protected $_config;
|
|
|
|
function __construct()
|
|
{
|
|
$this->_config = json_decode(Getter::get('config'), true);
|
|
}
|
|
|
|
protected function _headers($headers = array())
|
|
{
|
|
return array_merge([
|
|
'Authorization' => 'Bearer ' . null
|
|
], $headers);
|
|
}
|
|
|
|
protected function _client()
|
|
{
|
|
|
|
$client = new Client([
|
|
'headers' => $this->_headers()
|
|
]);
|
|
|
|
return $client;
|
|
}
|
|
|
|
public function syncOrder($order)
|
|
{
|
|
if ($order instanceof \Magento\Sales\Model\Order) {
|
|
$orderDetail = new Order($order);
|
|
$shippingAddress = new ShippingAddress($order->getShippingAddress());
|
|
$buyer = new Buyer($order);
|
|
|
|
$payload = [
|
|
...$orderDetail->jsonSerialize(),
|
|
'url' => GetBaseURL::get(),
|
|
'source' => 'magento',
|
|
'details' => [],
|
|
'buyer' => $buyer->jsonSerialize(),
|
|
'shippingAddress' => $shippingAddress->jsonSerialize(),
|
|
];
|
|
foreach ($order->getAllVisibleItems() as $item) {
|
|
$payload['details'][] = (new Detail($item))->jsonSerialize();
|
|
}
|
|
try {
|
|
$response = $this->_client()->post($this->_config['url'], [
|
|
'json' => $payload
|
|
]);
|
|
$statusCode = $response->getStatusCode();
|
|
$responseContent = $response->getBody()->getContents();
|
|
} catch (ClientException $ex) {
|
|
$statusCode = $ex->getResponse()->getStatusCode();
|
|
$responseContent = $ex->getResponse()->getBody()->getContents();
|
|
} finally {
|
|
return json_encode([
|
|
'guzzle' => [
|
|
'statusCode' => $statusCode,
|
|
'response' => $responseContent
|
|
],
|
|
'payload' => $payload
|
|
]);
|
|
}
|
|
} else {
|
|
throw new \ErrorException('$order is not instance \Magento\Sales\Model\Order\Interceptor');
|
|
}
|
|
}
|
|
}
|