From 7ef2bef4db639bf6deccb1db912159e265ea09e7 Mon Sep 17 00:00:00 2001 From: "kai.t" Date: Fri, 17 May 2024 08:15:32 +0700 Subject: [PATCH] update sync order module --- .../IpSupply/SyncOrder/Api/Repository.php | 64 ++++++++-------- .../SyncOrder/Api/RepositoryInterface.php | 5 +- .../SyncOrder/Block/Adminhtml/Config.php | 4 + app/code/IpSupply/SyncOrder/Config/Getter.php | 32 ++++++++ app/code/IpSupply/SyncOrder/Config/Setter.php | 35 +++++++++ app/code/IpSupply/SyncOrder/Helper.php | 28 +++++++ .../SyncOrder/Observer/CheckOrderStatus.php | 35 +++++++++ .../SyncOrder/Serialize/Serializer/Json.php | 36 +++++++++ .../IpSupply/SyncOrder/Setup/UpgradeData.php | 33 ++++++++ app/code/IpSupply/SyncOrder/etc/di.xml | 13 ++++ app/code/IpSupply/SyncOrder/etc/events.xml | 6 ++ app/code/IpSupply/SyncOrder/etc/webapi.xml | 4 +- .../view/adminhtml/layout/default.xml | 9 +++ .../layout/syncorder_config_index.xml | 22 +++--- .../templates/syncorder_config.phtml | 76 +++++++++++++++++++ .../view/adminhtml/web/css/config.css | 47 ++++++++++++ 16 files changed, 401 insertions(+), 48 deletions(-) create mode 100644 app/code/IpSupply/SyncOrder/Config/Getter.php create mode 100644 app/code/IpSupply/SyncOrder/Config/Setter.php create mode 100755 app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php create mode 100644 app/code/IpSupply/SyncOrder/Serialize/Serializer/Json.php create mode 100755 app/code/IpSupply/SyncOrder/Setup/UpgradeData.php create mode 100755 app/code/IpSupply/SyncOrder/etc/di.xml create mode 100755 app/code/IpSupply/SyncOrder/etc/events.xml create mode 100644 app/code/IpSupply/SyncOrder/view/adminhtml/layout/default.xml diff --git a/app/code/IpSupply/SyncOrder/Api/Repository.php b/app/code/IpSupply/SyncOrder/Api/Repository.php index 1dc1f84b..ecb10796 100755 --- a/app/code/IpSupply/SyncOrder/Api/Repository.php +++ b/app/code/IpSupply/SyncOrder/Api/Repository.php @@ -11,10 +11,10 @@ class Repository implements RepositoryInterface { function __construct() { - $this->authorization(); + $this->_authorization(); } - private function authorization() + private function _authorization() { $headers = apache_request_headers(); $apiKey = \IpSupply\SyncOrder\Helper::API_KEY; @@ -32,55 +32,51 @@ class Repository implements RepositoryInterface die(json_encode(['message' => 'unauthorized'])); } - private function _responseOk(array $data) + static function getForm() { - header('HTTP/1.1 200 Ok'); - header('Accept: application/json'); - header('Content-Type: application/json'); - die(json_encode($data)); + $data = [ + 'url' => '', + ]; + + $config = \IpSupply\SyncOrder\Config\Getter::get('config'); + if ($config) { + $data = json_decode($config, true); + } + + return [ + [ + 'label' => 'API', + 'name' => 'url', + 'value' => $data['url'] ?? '' + ] + ]; } - private function _responseFail(array $data) - { - header('HTTP/1.1 400 Bad request'); - header('Accept: application/json'); - header('Content-Type: application/json'); - die(json_encode($data)); - } - - private function _responseMethodFail() - { - header('HTTP/1.1 400 bad request'); - header('Accept: application/json'); - header('Content-Type: application/json'); - die(json_encode([ - 'status' => false, - 'message' => 'Param ?method=... not exist!' - ])); - } public function getData() { - return $this->_responseOk([ + return \IpSupply\SyncOrder\Helper::responseOk([ 'status' => true, 'data' => [] ]); - // return $this->_responseMethodFail(); } public function postData() { parse_str( string: file_get_contents('php://input'), - result: $payload + result: $payload, ); - return $this->_responseOk([ - 'status' => true, - 'data' => [], // TODO - 'payload' => $payload - ]); + unset($payload['form_key']); - // return $this->_responseMethodFail(); + \IpSupply\SyncOrder\Config\Setter::set( + path: 'config', + value: json_encode($payload), + ); + + return \IpSupply\SyncOrder\Helper::responseOk([ + 'status' => true, + ]); } } diff --git a/app/code/IpSupply/SyncOrder/Api/RepositoryInterface.php b/app/code/IpSupply/SyncOrder/Api/RepositoryInterface.php index 52a740cb..d13ae370 100755 --- a/app/code/IpSupply/SyncOrder/Api/RepositoryInterface.php +++ b/app/code/IpSupply/SyncOrder/Api/RepositoryInterface.php @@ -2,6 +2,8 @@ namespace IpSupply\SyncOrder\Api; +use Magento\Framework\Exception\LocalizedException; + /** * @api */ @@ -15,8 +17,7 @@ interface RepositoryInterface public function getData(); /** - * methot: POST - * + * method: POST * @return string */ public function postData(); diff --git a/app/code/IpSupply/SyncOrder/Block/Adminhtml/Config.php b/app/code/IpSupply/SyncOrder/Block/Adminhtml/Config.php index 7ae86814..1382cef8 100755 --- a/app/code/IpSupply/SyncOrder/Block/Adminhtml/Config.php +++ b/app/code/IpSupply/SyncOrder/Block/Adminhtml/Config.php @@ -26,4 +26,8 @@ class Config extends Template { return \IpSupply\SyncOrder\Helper::API_KEY; } + + public function getForm() { + return \IpSupply\SyncOrder\Api\Repository::getForm(); + } } diff --git a/app/code/IpSupply/SyncOrder/Config/Getter.php b/app/code/IpSupply/SyncOrder/Config/Getter.php new file mode 100644 index 00000000..6fa34935 --- /dev/null +++ b/app/code/IpSupply/SyncOrder/Config/Getter.php @@ -0,0 +1,32 @@ +create(self::class); + $path = $prefix + ? Helper::PREFIX . '/' . $path + : $path; + return $configGetter->getConfigValue($path); + } + + public function __construct(ScopeConfigInterface $scopeConfig) + { + $this->scopeConfig = $scopeConfig; + } + + public function getConfigValue($fullPath) + { + return $this->scopeConfig->getValue($fullPath); + } +} diff --git a/app/code/IpSupply/SyncOrder/Config/Setter.php b/app/code/IpSupply/SyncOrder/Config/Setter.php new file mode 100644 index 00000000..759cdc1a --- /dev/null +++ b/app/code/IpSupply/SyncOrder/Config/Setter.php @@ -0,0 +1,35 @@ +create(self::class); + $path = $prefix + ? Helper::PREFIX . '/' . $path + : $path; + $configSetter->setConfigValue($path, $value); + + // Clean cache old + $cacheManager = $objectManager->get(\Magento\Framework\App\Cache\Manager::class); + $cacheManager->flush(['config']); + } + + public function __construct(WriterInterface $configWriter) + { + $this->configWriter = $configWriter; + } + + public function setConfigValue($fullPath, $value) + { + $this->configWriter->save($fullPath, $value); + } +} diff --git a/app/code/IpSupply/SyncOrder/Helper.php b/app/code/IpSupply/SyncOrder/Helper.php index e33c96df..c23b76c7 100755 --- a/app/code/IpSupply/SyncOrder/Helper.php +++ b/app/code/IpSupply/SyncOrder/Helper.php @@ -4,4 +4,32 @@ namespace IpSupply\SyncOrder; final class Helper { public const API_KEY = 'IpSupply@123'; + public const PREFIX = 'IpSupply_SyncOrder'; + + static function responseOk(array $data) + { + header('HTTP/1.1 200 Ok'); + header('Accept: application/json'); + header('Content-Type: application/json'); + die(json_encode($data)); + } + + static function responseFail(array $data) + { + header('HTTP/1.1 400 Bad request'); + header('Accept: application/json'); + header('Content-Type: application/json'); + die(json_encode($data)); + } + + static function responseMethodFail() + { + header('HTTP/1.1 400 bad request'); + header('Accept: application/json'); + header('Content-Type: application/json'); + die(json_encode([ + 'status' => false, + 'message' => 'Param ?method=... not exist!' + ])); + } } diff --git a/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php b/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php new file mode 100755 index 00000000..7eace9bc --- /dev/null +++ b/app/code/IpSupply/SyncOrder/Observer/CheckOrderStatus.php @@ -0,0 +1,35 @@ +orderRepository = $orderRepository; + $this->dir = $dir; + } + + public function execute(\Magento\Framework\Event\Observer $observer) + { + $order = $observer->getEvent()->getOrder(); + $customerId = $order->getCustomerId(); + $OrderStatus = $order->getStatus(); + $file = $this->dir->getPath('log') . '/' . 'CheckOrderStatus.log'; + if (!file_exists($file)) { + $fh = fopen($file, 'w') or die("Can't create file"); + fclose($fh); + } + $current = file_get_contents($file); + $current .= $customerId . ':' . $OrderStatus . " \n"; + file_put_contents($file, $current); + } +} diff --git a/app/code/IpSupply/SyncOrder/Serialize/Serializer/Json.php b/app/code/IpSupply/SyncOrder/Serialize/Serializer/Json.php new file mode 100644 index 00000000..97be9d98 --- /dev/null +++ b/app/code/IpSupply/SyncOrder/Serialize/Serializer/Json.php @@ -0,0 +1,36 @@ +_eavSetupFactory = $eavSetupFactory; + $this->_attributeRepositoryInterface = $attributeRepositoryInterface; + $this->_attributeSetupFactory = $attributeSetupFactory; + } + + public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + \IpSupply\SyncOrder\Config\Setter::set(path: 'config', value: json_encode([ + 'url' => 'https://google.com' // TODO: config api + ])); + } +} diff --git a/app/code/IpSupply/SyncOrder/etc/di.xml b/app/code/IpSupply/SyncOrder/etc/di.xml new file mode 100755 index 00000000..034d9a2b --- /dev/null +++ b/app/code/IpSupply/SyncOrder/etc/di.xml @@ -0,0 +1,13 @@ + + + + + + + diff --git a/app/code/IpSupply/SyncOrder/etc/events.xml b/app/code/IpSupply/SyncOrder/etc/events.xml new file mode 100755 index 00000000..290a5b7c --- /dev/null +++ b/app/code/IpSupply/SyncOrder/etc/events.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/app/code/IpSupply/SyncOrder/etc/webapi.xml b/app/code/IpSupply/SyncOrder/etc/webapi.xml index ff2976a2..4be01505 100755 --- a/app/code/IpSupply/SyncOrder/etc/webapi.xml +++ b/app/code/IpSupply/SyncOrder/etc/webapi.xml @@ -1,12 +1,12 @@ - + - + diff --git a/app/code/IpSupply/SyncOrder/view/adminhtml/layout/default.xml b/app/code/IpSupply/SyncOrder/view/adminhtml/layout/default.xml new file mode 100644 index 00000000..c10ba637 --- /dev/null +++ b/app/code/IpSupply/SyncOrder/view/adminhtml/layout/default.xml @@ -0,0 +1,9 @@ + + + + + + diff --git a/app/code/IpSupply/SyncOrder/view/adminhtml/layout/syncorder_config_index.xml b/app/code/IpSupply/SyncOrder/view/adminhtml/layout/syncorder_config_index.xml index 2855b0fd..f96bbac6 100755 --- a/app/code/IpSupply/SyncOrder/view/adminhtml/layout/syncorder_config_index.xml +++ b/app/code/IpSupply/SyncOrder/view/adminhtml/layout/syncorder_config_index.xml @@ -1,14 +1,16 @@ - + - + - - - - - - + + + + diff --git a/app/code/IpSupply/SyncOrder/view/adminhtml/templates/syncorder_config.phtml b/app/code/IpSupply/SyncOrder/view/adminhtml/templates/syncorder_config.phtml index e69de29b..450c5f91 100755 --- a/app/code/IpSupply/SyncOrder/view/adminhtml/templates/syncorder_config.phtml +++ b/app/code/IpSupply/SyncOrder/view/adminhtml/templates/syncorder_config.phtml @@ -0,0 +1,76 @@ +
+
+ getForm() as $field) : ?> +
+ + +
+ + +
+ +
+
+
+ diff --git a/app/code/IpSupply/SyncOrder/view/adminhtml/web/css/config.css b/app/code/IpSupply/SyncOrder/view/adminhtml/web/css/config.css index e69de29b..acb1feac 100755 --- a/app/code/IpSupply/SyncOrder/view/adminhtml/web/css/config.css +++ b/app/code/IpSupply/SyncOrder/view/adminhtml/web/css/config.css @@ -0,0 +1,47 @@ +.ipsupply { + box-sizing: border-box; + font-size: 16px; +} + +.ipsupply button { + background: #eb5202; + color: #fff; + border: none; + outline: 0; +} + +.ipsupply form { + box-sizing: border-box; + padding: 20px; + border: 1.5px solid #aca; + max-width: 600px; +} + +.ipsupply form .form-group { + margin-bottom: 10px; + display: flex; + justify-content: space-between; + align-items: center; +} + +.ipsupply form .form-group label { + font-weight: bold; + text-transform: uppercase; + margin-right: 5px; + min-width: 70px; +} + +.ipsupply form .form-group label::after { + content: ":"; +} + +.ipsupply form .form-group input { + outline: 0; + border: 1px solid #aca; + padding: 3px 5px; + width: 100%; +} + +.ipsupply form .form-group [type=submit] { + width: 100%; +}