diff --git a/app/code/IpSupply/SyncOrder/Config/Getter.php b/app/code/IpSupply/SyncOrder/Config/Getter.php old mode 100644 new mode 100755 diff --git a/app/code/IpSupply/SyncOrder/Config/Setter.php b/app/code/IpSupply/SyncOrder/Config/Setter.php old mode 100644 new mode 100755 diff --git a/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php b/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php index fffcfae1..28abd525 100755 --- a/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php +++ b/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php @@ -30,6 +30,7 @@ class CheckOrderStatus implements ObserverInterface $currentContent .= date("Y-m-d H:i:s") . ':'; $currentContent .= $content; $currentContent .= "\n"; + $currentContent = str_replace(array("\r", "\n"), '', 500); file_put_contents($file, $currentContent); } @@ -37,9 +38,9 @@ class CheckOrderStatus implements ObserverInterface { $order = $observer->getEvent()->getOrder(); - $order = [ - 'customer_id' => $order->getCustomerId(), - 'status' => $order->getStatus(), - ]; + $sync = new \IpSupply\SyncOrder\Sync\Index; + $content = $sync->syncOrder($order); + $this->_saveLog($content); + } } diff --git a/app/code/IpSupply/SyncOrder/Serialize/Serializer/Json.php b/app/code/IpSupply/SyncOrder/Serialize/Serializer/Json.php old mode 100644 new mode 100755 diff --git a/app/code/IpSupply/SyncOrder/Sync/Buyer.php b/app/code/IpSupply/SyncOrder/Sync/Buyer.php old mode 100644 new mode 100755 index f7928353..2ac6bfcc --- a/app/code/IpSupply/SyncOrder/Sync/Buyer.php +++ b/app/code/IpSupply/SyncOrder/Sync/Buyer.php @@ -1,14 +1,25 @@ username = $order->getCustomerEmail() ?? null; + $this->fullName = $order->getCustomerName() ?? null; + $this->primaryPhone = $order->getBillingAddress()?->getTelephone() ?? $order->getShippingAddress()->getTelephone(); + $this->email = + $order->getBillingAddress()?->getEmail() + ?? $order->getShippingAddress()->getEmail(); + } + + public function jsonSerialize(): mixed { return get_object_vars($this); } diff --git a/app/code/IpSupply/SyncOrder/Sync/Detail.php b/app/code/IpSupply/SyncOrder/Sync/Detail.php old mode 100644 new mode 100755 index 2c8aae9f..f03b2735 --- a/app/code/IpSupply/SyncOrder/Sync/Detail.php +++ b/app/code/IpSupply/SyncOrder/Sync/Detail.php @@ -1,21 +1,33 @@ productId = $item->getProductId(); + $this->title = $item->getProduct()->getName(); + $this->sku = $item->getSku(); + $this->qty = $item->getQtyOrdered(); + $this->price = $item->getPrice(); + $this->amount = $item->getQtyOrdered() * $item->getPrice(); + if ($condition = $item->getProductOptionByCode()) { + $this->condition = $condition['attributes_info'][0]['value'] ?? null; + } } - public function jsonSerialize() + public function jsonSerialize(): mixed { - return get_object_vars($this); + return (array) get_object_vars($this); } } diff --git a/app/code/IpSupply/SyncOrder/Sync/Index.php b/app/code/IpSupply/SyncOrder/Sync/Index.php old mode 100644 new mode 100755 index f95ad957..e53dc278 --- a/app/code/IpSupply/SyncOrder/Sync/Index.php +++ b/app/code/IpSupply/SyncOrder/Sync/Index.php @@ -1,4 +1,5 @@ _config = Getter::get('config'); + $this->_config = json_decode(Getter::get('config'), true); } protected function _headers($headers = array()) { return array_merge([ - 'Authorization' => 'Bearer ' . '' + 'Authorization' => 'Bearer ' . null ], $headers); } - protected function _client() { + protected function _client() + { $client = new Client([ 'headers' => $this->_headers(), 'base_url' => $this->_config['url'], @@ -30,17 +32,26 @@ final class Index return $client; } - public function syncOrder($order): bool + public function syncOrder($order) { - if ($order instanceof \Magento\Sales\Model\Order\Interceptor) { + 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', - ...(new Order($order))->jsonSerialize() + 'details' => [], + 'buyer' => $buyer->jsonSerialize(), + 'shippingAddress' => $shippingAddress->jsonSerialize(), ]; + foreach ($order->getAllVisibleItems() as $item) { + $payload['details'][] = (new Detail($item))->jsonSerialize(); + } - - return true; + return json_encode($payload); } else { throw new \ErrorException('$order is not instance \Magento\Sales\Model\Order\Interceptor'); return false; diff --git a/app/code/IpSupply/SyncOrder/Sync/Order.php b/app/code/IpSupply/SyncOrder/Sync/Order.php old mode 100644 new mode 100755 index 2ffb7c87..80f0a097 --- a/app/code/IpSupply/SyncOrder/Sync/Order.php +++ b/app/code/IpSupply/SyncOrder/Sync/Order.php @@ -1,28 +1,29 @@ orderId = $order->getId(); $this->statusPayment = $order->getStatus(); $this->orderDate = $order->getUpdatedAt(); $this->subTotal = $order->getSubtotal(); - $this->total = $order->getTotalInvoiced(); - $this->shippingPrice = $order->getShippingInvoiced(); + $this->total = $order->getGrandTotal(); + $this->shippingPrice = $order->getShippingAmount(); $this->currency = $order->getOrderCurrencyCode(); } - public function jsonSerialize() + public function jsonSerialize(): mixed { return get_object_vars($this); } diff --git a/app/code/IpSupply/SyncOrder/Sync/ShippingAddress.php b/app/code/IpSupply/SyncOrder/Sync/ShippingAddress.php old mode 100644 new mode 100755 index 3988cfcb..aae81f17 --- a/app/code/IpSupply/SyncOrder/Sync/ShippingAddress.php +++ b/app/code/IpSupply/SyncOrder/Sync/ShippingAddress.php @@ -1,37 +1,38 @@ email = $address->getEmail(); - $this->name = $address->getName(); - $this->street1 = $address->getStreetLine(1); - $this->street2 = $address->getStreetLine(2); - $this->country = $address->getCountryId(); - $this->cityName = $address->getCity(); - $this->phone = $address->getTelephone(); - $this->postalCode = $address->getPostcode(); - $this->countryName = $address->getCountryId(); - } else { - // do something else + if (!$address) { + return; } + $this->email = $address->getEmail(); + $this->name = $address->getName(); + $this->street1 = $address->getStreetLine(1); + $this->street2 = $address->getStreetLine(2); + $this->country = $address->getCountryId(); + $this->cityName = $address->getCity(); + $this->stateOrProvince = $address->getRegion(); + $this->phone = $address->getTelephone(); + $this->postalCode = $address->getPostcode(); + $this->countryName = $address->getCountryId(); } - public function jsonSerialize() + public function jsonSerialize(): mixed { return get_object_vars($this); } diff --git a/app/code/IpSupply/SyncOrder/view/adminhtml/layout/default.xml b/app/code/IpSupply/SyncOrder/view/adminhtml/layout/default.xml old mode 100644 new mode 100755