update sync order module
This commit is contained in:
		
							parent
							
								
									867e553946
								
							
						
					
					
						commit
						7ef2bef4db
					
				|  | @ -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, | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -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(); | ||||
|  |  | |||
|  | @ -26,4 +26,8 @@ class Config extends Template | |||
|     { | ||||
|         return \IpSupply\SyncOrder\Helper::API_KEY; | ||||
|     } | ||||
| 
 | ||||
|     public function getForm() { | ||||
|         return \IpSupply\SyncOrder\Api\Repository::getForm(); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -0,0 +1,32 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace IpSupply\SyncOrder\Config; | ||||
| 
 | ||||
| use IpSupply\SyncOrder\Helper; | ||||
| use Magento\Framework\App\Config\ScopeConfigInterface; | ||||
| 
 | ||||
| class Getter | ||||
| { | ||||
|     protected $scopeConfig; | ||||
| 
 | ||||
|     static function get($path, $prefix = true) | ||||
|     { | ||||
| 
 | ||||
|         $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | ||||
|         $configGetter = $objectManager->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); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,35 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace IpSupply\SyncOrder\Config; | ||||
| 
 | ||||
| use IpSupply\SyncOrder\Helper; | ||||
| use Magento\Framework\App\Config\Storage\WriterInterface; | ||||
| 
 | ||||
| class Setter | ||||
| { | ||||
|     protected $configWriter; | ||||
| 
 | ||||
|     static function set($path, $value, $prefix = true) | ||||
|     { | ||||
|         $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); | ||||
|         $configSetter = $objectManager->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); | ||||
|     } | ||||
| } | ||||
|  | @ -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!' | ||||
|         ])); | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -0,0 +1,35 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace IpSupply\SyncOrder\Observer; | ||||
| 
 | ||||
| use Magento\Framework\Event\ObserverInterface; | ||||
| 
 | ||||
| class CheckOrderStatus implements ObserverInterface | ||||
| { | ||||
| 
 | ||||
|     protected $orderRepository; | ||||
|     protected $dir; | ||||
| 
 | ||||
|     public function __construct( | ||||
|         \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, | ||||
|         \Magento\Framework\Filesystem\DirectoryList $dir, | ||||
|     ) { | ||||
|         $this->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); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,36 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace IpSupply\SyncOrder\Serialize\Serializer; | ||||
| 
 | ||||
| use Magento\Framework\Serialize\Serializer\Json as SerializerJson; | ||||
| 
 | ||||
| class Json extends SerializerJson | ||||
| { | ||||
|     /** | ||||
|      * @inheritDoc | ||||
|      * @since 101.0.0 | ||||
|      */ | ||||
|     public function unserialize($string) | ||||
|     { | ||||
|         if ($string === null) { | ||||
|             throw new \InvalidArgumentException( | ||||
|                 'Unable to unserialize value. Error: Parameter must be a string type, null given.' | ||||
|             ); | ||||
|         } | ||||
| 
 | ||||
|         $result = json_decode($string, true); | ||||
| 
 | ||||
|         if (json_last_error() !== JSON_ERROR_NONE) { | ||||
|             parse_str($string, $result); | ||||
|             if (is_array($result)) { | ||||
|                 return $result; | ||||
|             } else { | ||||
|                 throw new \InvalidArgumentException( | ||||
|                     $string . " - Unable to unserialize value. Error: " . json_last_error_msg() | ||||
|                 ); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return $result; | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,33 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace IpSupply\SyncOrder\Setup; | ||||
| 
 | ||||
| use Magento\Catalog\Model\Product; | ||||
| use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; | ||||
| use Magento\Framework\Setup\ModuleContextInterface; | ||||
| use Magento\Framework\Setup\ModuleDataSetupInterface; | ||||
| use Magento\Framework\Setup\UpgradeDataInterface; | ||||
| 
 | ||||
| class UpgradeData implements UpgradeDataInterface | ||||
| { | ||||
|     protected $_eavSetupFactory; | ||||
|     protected $_attributeRepositoryInterface; | ||||
|     protected $_attributeSetupFactory; | ||||
| 
 | ||||
|     public function __construct( | ||||
|         \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory, | ||||
|         \Magento\Eav\Api\AttributeRepositoryInterface $attributeRepositoryInterface, | ||||
|         \Magento\Catalog\Setup\CategorySetupFactory $attributeSetupFactory | ||||
|     ) { | ||||
|         $this->_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
 | ||||
|         ])); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,13 @@ | |||
| <?xml version="1.0"?> | ||||
| <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||||
|     <!-- Repository --> | ||||
|     <preference | ||||
|         for="IpSupply\SyncOrder\Api\RepositoryInterface" | ||||
|         type="IpSupply\SyncOrder\Api\Repository" | ||||
|     /> | ||||
| 
 | ||||
|     <preference | ||||
|         for="Magento\Framework\Serialize\Serializer\Json" | ||||
|         type="IpSupply\SyncOrder\Serialize\Serializer\Json" | ||||
|     /> | ||||
| </config> | ||||
|  | @ -0,0 +1,6 @@ | |||
| <?xml version="1.0"?> | ||||
| <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | ||||
|     <event name="sales_order_save_after"> | ||||
|         <observer name="syncorder_sales_order_save_after" instance="IpSupply\SyncOrder\Observer\CheckOrderStatus" /> | ||||
|     </event> | ||||
| </config> | ||||
|  | @ -1,12 +1,12 @@ | |||
| <?xml version="1.0"?> | ||||
| <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd"> | ||||
|     <route url="ipsupply/syncorder/sync" method="GET"> | ||||
|     <route url="V1/syncorder/config" method="GET"> | ||||
|         <service class="IpSupply\SyncOrder\Api\RepositoryInterface" method="getData"/> | ||||
|         <resources> | ||||
|             <resource ref="anonymous"/> | ||||
|         </resources> | ||||
|     </route> | ||||
|     <route url="ipsupply/syncorder/sync" method="POST"> | ||||
|     <route url="V1/syncorder/config" method="POST"> | ||||
|         <service class="IpSupply\SyncOrder\Api\RepositoryInterface" method="postData"/> | ||||
|         <resources> | ||||
|             <resource ref="anonymous"/> | ||||
|  |  | |||
|  | @ -0,0 +1,9 @@ | |||
| <?xml version="1.0"?> | ||||
| <page | ||||
|     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|     xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" | ||||
| > | ||||
|     <head> | ||||
|         <css src="IpSupply_SyncOrder::css/config.css" /> | ||||
|     </head> | ||||
| </page> | ||||
|  | @ -1,14 +1,16 @@ | |||
| <?xml version="1.0"?> | ||||
| <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> | ||||
| <page | ||||
|     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
|     xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" | ||||
| > | ||||
|     <head> | ||||
|        <css src="IpSupply_SyncOrder::css/config.css"/> | ||||
|         <css src="IpSupply_SyncOrder::css/config.css" /> | ||||
|     </head> | ||||
|     <body> | ||||
|         <referenceContainer name="content"> | ||||
|             <block class="IpSupply\SyncOrder\Block\Adminhtml\Config" template="IpSupply_SyncOrder::syncorder_config.phtml"/> | ||||
|             <!-- | ||||
|                 Template: IpSupply_SyncOrder::syncorder_config.phtml to ../templates/syncorder_config.phtml | ||||
|             --> | ||||
|         </referenceContainer> | ||||
|     </body> | ||||
|     <referenceBlock name="content"> | ||||
|         <block | ||||
|             class="IpSupply\SyncOrder\Block\Adminhtml\Config" | ||||
|             template="IpSupply_SyncOrder::syncorder_config.phtml" | ||||
|         /> | ||||
|         <!-- Template: IpSupply_SyncOrder::syncorder_config.phtml to ../templates/syncorder_config.phtml --> | ||||
|     </referenceBlock> | ||||
| </page> | ||||
|  |  | |||
|  | @ -0,0 +1,76 @@ | |||
| <div class="ipsupply"> | ||||
|     <form id="form"> | ||||
|         <?php foreach ($block->getForm() as $field) : ?> | ||||
|             <div class="form-group"> | ||||
|                 <label for="<?= $field['name'] ?>"><?= $field['label'] ?></label> | ||||
|                 <input type="text" id="<?= $field['name'] ?>" name="<?= $field['name'] ?>" value="<?= $field['value'] ?>" /> | ||||
|             </div> | ||||
|         <?php endforeach ?> | ||||
| 
 | ||||
|         <div class="form-group"> | ||||
|             <button class="submit" type="submit">Save</button> | ||||
|         </div> | ||||
|     </form> | ||||
| </div> | ||||
| <script> | ||||
|     const AJAX_URL = location.origin + "/rest/V1/syncorder/config"; // TODO: need /sync to /config | ||||
| 
 | ||||
|     require([ | ||||
|         "jquery", | ||||
|         "mage/translate", | ||||
|         "uiComponent", | ||||
|         "ko", | ||||
|         "Magento_Ui/js/modal/alert", | ||||
|         "Magento_Ui/js/modal/confirm", | ||||
|         "Magento_Ui/js/modal/modal", | ||||
|     ], function($, $t, Component, ko, alertModal, confirmModal, modal) { | ||||
|         "use strict"; | ||||
|         $.ajaxSetup({ | ||||
|             contentType: "application/json;", | ||||
|             headers: { | ||||
|                 API_KEY: "<?= $block->getApiKey() ?>", | ||||
|             }, | ||||
|             success: function(data) { | ||||
|                 // do something before xhr | ||||
|             }, | ||||
|             fail: function(error) { | ||||
|                 console.error(error); | ||||
|                 alertModal({ | ||||
|                     content: error.message, | ||||
|                 }); | ||||
|             }, | ||||
|         }); | ||||
| 
 | ||||
|         // Event | ||||
|         $("#form").on("submit", function(e) { | ||||
|             e.preventDefault(); | ||||
|             e.stopPropagation(); | ||||
|             $(this) | ||||
|                 .find('[type=submit]') | ||||
|                 .attr('disabled', 'disabled'); | ||||
| 
 | ||||
|             $.post(AJAX_URL, $(this).serialize()) | ||||
|                 .then(res => { | ||||
|                     if (res.status) { | ||||
|                         alertModal({ | ||||
|                             content: 'Successfully: Data saved!' | ||||
|                         }) | ||||
|                     } else { | ||||
|                         alertModal({ | ||||
|                             content: 'Failed: Data not saved!' | ||||
|                         }) | ||||
|                     } | ||||
|                 }) | ||||
|                 .catch(() => { | ||||
|                     alertModal({ | ||||
|                         content: 'Fail saved!' | ||||
|                     }) | ||||
|                 }) | ||||
|                 .then(() => { | ||||
|                     $(this) | ||||
|                         .find('[type=submit]') | ||||
|                         .removeAttr('disabled'); | ||||
|                 }) | ||||
|         }); | ||||
|     }); | ||||
| </script> | ||||
|  | @ -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%; | ||||
| } | ||||
		Loading…
	
		Reference in New Issue