update exception

This commit is contained in:
Kai Ton 2024-05-20 09:45:45 +07:00
parent 6cab863d14
commit 498471d50b
3 changed files with 46 additions and 9 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace IpSupply\SyncOrder\Config;
use Magento\Store\Model\StoreManagerInterface;
class GetBaseURL
{
protected $storeManagerInterface;
static function get()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
return $storeManager->getStore()->getBaseUrl();
}
public function __construct(StoreManagerInterface $storeManagerInterface)
{
$this->storeManagerInterface = $storeManagerInterface;
}
}

View File

@ -13,10 +13,10 @@ class Buyer implements \JsonSerializable
{
$this->username = $order->getCustomerEmail() ?? null;
$this->fullName = $order->getCustomerName() ?? null;
$this->primaryPhone = $order->getBillingAddress()?->getTelephone() ?? $order->getShippingAddress()->getTelephone();
$this->primaryPhone = $order->getBillingAddress()?->getTelephone() ?? $order->getShippingAddress()?->getTelephone();
$this->email =
$order->getBillingAddress()?->getEmail()
?? $order->getShippingAddress()->getEmail();
?? $order->getShippingAddress()?->getEmail();
}
public function jsonSerialize(): mixed

View File

@ -3,7 +3,9 @@
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
@ -41,7 +43,7 @@ final class Index
$payload = [
...$orderDetail->jsonSerialize(),
'url' => $this->_config['url'],
'url' => GetBaseURL::get(),
'source' => 'magento',
'details' => [],
'buyer' => $buyer->jsonSerialize(),
@ -50,14 +52,26 @@ final class Index
foreach ($order->getAllVisibleItems() as $item) {
$payload['details'][] = (new Detail($item))->jsonSerialize();
}
$this->_client()->post($this->_config['url'], [
'json' => $payload
]);
return json_encode($payload);
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');
return false;
}
}
}