diff --git a/Controller/Settings/Index.php b/Controller/Settings/Index.php new file mode 100644 index 0000000..15523bb --- /dev/null +++ b/Controller/Settings/Index.php @@ -0,0 +1,125 @@ +response = $response; + $this->configWriter = $configWriter; + $this->request = $request; + $this->cache = $cache; + $this->configuration = $configuration; + $this->cacheManager = $cacheManager; + } + + /** + * @return ResponseHttp + * @throws Exception + */ + public function execute(): ResponseHttp + { + $data = json_decode($this->request->getContent(), true); + $tokenFromCache = $this->cache->load(SetUpdateSettingsMessage::CACHE_NAME); + $this->cache->remove(SetUpdateSettingsMessage::CACHE_NAME); + + $signature = $this->request->getHeader('x-signature'); + if (!$signature) { + return $this->error('Signature id is required.'); + } + $ts = $this->request->getHeader('x-timestamp') . $this->request->getContent(); + if (hash_hmac('sha256', $ts, $tokenFromCache) !== $signature) { + return $this->error('Signature id is invalid.'); + } + + $clientId = $data['client_id']; + $apiKey = $data['api_key']; + $webhookKey = $data['webhook_secret']; + $accountId = $data['account_id']; + $accountName = $data['account_name']; + if (empty($clientId)) { + return $this->error('Client ID is required.'); + } + if (empty($apiKey)) { + return $this->error('API Key is required.'); + } + if (empty($webhookKey)) { + return $this->error('Webhook Key is required.'); + } + if (empty($accountId)) { + return $this->error('Account id is required.'); + } + if (empty($accountName)) { + return $this->error('Account name is required.'); + } + $encryptor = ObjectManager::getInstance()->get(EncryptorInterface::class); + $mode = substr($tokenFromCache, 0, 4) === 'demo' ? 'demo' : 'prod'; + $account = $this->configuration->getAccount(); + $arrAccount = $account ? json_decode($account, true) : []; + $arrAccount[$mode . '_account_id'] = $accountId; + $arrAccount[$mode . '_account_name'] = $accountName; + $this->configWriter->save('airwallex/general/' . 'account', json_encode($arrAccount)); + $this->configWriter->save('airwallex/general/' . $mode . '_account_name', $accountName); + $this->configWriter->save('airwallex/general/' . $mode . '_client_id', $clientId); + $this->configWriter->save('airwallex/general/' . $mode . '_api_key', $encryptor->encrypt($apiKey)); + $this->configWriter->save('airwallex/general/webhook_' . $mode . '_secret_key', $encryptor->encrypt($webhookKey)); + $this->configWriter->save('airwallex/general/mode', $mode); + $this->cacheManager->flush(['config']); + $this->response->setBody(json_encode(['success' => true])); + return $this->response->setStatusCode(self::HTTP_OK); + } + + /** + * @throws Exception + */ + public function error(string $message) + { + throw new Exception($message); + } + + public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException + { + return null; + } + + /** + * @param RequestInterface $request + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * + * @return bool|null + */ + public function validateForCsrf(RequestInterface $request): ?bool + { + return true; + } +} diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml index 1556939..08ff925 100644 --- a/etc/adminhtml/di.xml +++ b/etc/adminhtml/di.xml @@ -16,7 +16,6 @@ Airwallex\Payments\Model\Adminhtml\Notifications\ExpressDisabled - Airwallex\Payments\Model\Adminhtml\Notifications\Webhook Airwallex\Payments\Model\Adminhtml\Notifications\Upgrade diff --git a/etc/adminhtml/system/basic.xml b/etc/adminhtml/system/basic.xml index 2a02748..05ac354 100644 --- a/etc/adminhtml/system/basic.xml +++ b/etc/adminhtml/system/basic.xml @@ -7,59 +7,59 @@ Airwallex\Payments\Model\Config\Source\Mode airwallex/general/mode - - - - airwallex/general/demo_client_id - - demo - - - - - Magento\Config\Model\Config\Backend\Encrypted - airwallex/general/demo_api_key - - demo - - - - - Magento\Config\Model\Config\Backend\Encrypted - airwallex/general/webhook_demo_secret_key - - demo - - - - - airwallex/general/prod_client_id - - prod - - - - - Magento\Config\Model\Config\Backend\Encrypted - airwallex/general/prod_api_key - - prod - - - - - Magento\Config\Model\Config\Backend\Encrypted - airwallex/general/webhook_prod_secret_key - - prod - - - - - Airwallex\Payments\Model\Config\Source\WebhookUrl + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Magento\Config\Model\Config\Source\Yesno