magento2-docker/app/code/IpSupply/SyncOrder/Sync/Index.php

67 lines
1.7 KiB
PHP
Executable File

<?php
namespace IpSupply\SyncOrder\Sync;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request as GuzzRequest;
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' => $this->_config['url'],
'source' => 'magento',
'details' => [],
'buyer' => $buyer->jsonSerialize(),
'shippingAddress' => $shippingAddress->jsonSerialize(),
];
foreach ($order->getAllVisibleItems() as $item) {
$payload['details'][] = (new Detail($item))->jsonSerialize();
}
$this->_client()->post($this->_config['url'], [
'body' => [
"magento_2_" . time() => $payload
]
]);
return json_encode($payload);
} else {
throw new \ErrorException('$order is not instance \Magento\Sales\Model\Order\Interceptor');
return false;
}
}
}