diff --git a/app/code/IpSupply/SyncOrder/Config/GetBaseURL.php b/app/code/IpSupply/SyncOrder/Config/GetBaseURL.php new file mode 100644 index 00000000..8acff8c5 --- /dev/null +++ b/app/code/IpSupply/SyncOrder/Config/GetBaseURL.php @@ -0,0 +1,23 @@ +get('\Magento\Store\Model\StoreManagerInterface'); + return $storeManager->getStore()->getBaseUrl(); + } + + public function __construct(StoreManagerInterface $storeManagerInterface) + { + $this->storeManagerInterface = $storeManagerInterface; + } +} diff --git a/app/code/IpSupply/SyncOrder/Sync/Buyer.php b/app/code/IpSupply/SyncOrder/Sync/Buyer.php index 2ac6bfcc..2d8fc5ca 100755 --- a/app/code/IpSupply/SyncOrder/Sync/Buyer.php +++ b/app/code/IpSupply/SyncOrder/Sync/Buyer.php @@ -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 diff --git a/app/code/IpSupply/SyncOrder/Sync/Index.php b/app/code/IpSupply/SyncOrder/Sync/Index.php index 23623389..a962bd90 100755 --- a/app/code/IpSupply/SyncOrder/Sync/Index.php +++ b/app/code/IpSupply/SyncOrder/Sync/Index.php @@ -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; } } }