Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection flow #60

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions Controller/Settings/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Airwallex\Payments\Controller\Settings;

use Airwallex\Payments\Controller\Adminhtml\Configuration\SetUpdateSettingsMessage;
use Airwallex\Payments\Helper\Configuration;
use Exception;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Cache\Manager;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\App\Config\Storage\Writer;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\Request\Http as RequestHttp;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\Response\Http as ResponseHttp;
use Magento\Framework\Encryption\EncryptorInterface;

class Index implements HttpPostActionInterface, CsrfAwareActionInterface
{
private const HTTP_OK = 200;
private ResponseHttp $response;
private Writer $configWriter;
private RequestHttp $request;
private CacheInterface $cache;
public Configuration $configuration;
protected Manager $cacheManager;

public function __construct(
ResponseHttp $response,
Writer $configWriter,
RequestHttp $request,
CacheInterface $cache,
Configuration $configuration,
Manager $cacheManager
) {
$this->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;
}
}
1 change: 0 additions & 1 deletion etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<arguments>
<argument name="messages" xsi:type="array">
<item name="ExpressDisabledNotification" xsi:type="string">Airwallex\Payments\Model\Adminhtml\Notifications\ExpressDisabled</item>
<item name="WebhookNotification" xsi:type="string">Airwallex\Payments\Model\Adminhtml\Notifications\Webhook</item>
<item name="UpgradeNotification" xsi:type="string">Airwallex\Payments\Model\Adminhtml\Notifications\Upgrade</item>
</argument>
</arguments>
Expand Down
102 changes: 51 additions & 51 deletions etc/adminhtml/system/basic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,59 @@
<source_model>Airwallex\Payments\Model\Config\Source\Mode</source_model>
<config_path>airwallex/general/mode</config_path>
</field>
<!-- <field id="update_settings" translate="label" type="button" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="update_settings" translate="label" type="button" sortOrder="15" showInDefault="1" showInWebsite="1" showInStore="1">
<frontend_model>Airwallex\Payments\Model\Config\Adminhtml\UpdateSettings</frontend_model>
</field> -->
<field id="demo_client_id" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Demo Client ID</label>
<config_path>airwallex/general/demo_client_id</config_path>
<depends>
<field id="mode">demo</field>
</depends>
</field>
<field id="demo_api_key" translate="label" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Demo API Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<config_path>airwallex/general/demo_api_key</config_path>
<depends>
<field id="mode">demo</field>
</depends>
</field>
<field id="webhook_demo_secret_key" translate="label" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Webhook Demo Secret Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<config_path>airwallex/general/webhook_demo_secret_key</config_path>
<depends>
<field id="mode">demo</field>
</depends>
</field>
<field id="prod_client_id" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Production Client ID</label>
<config_path>airwallex/general/prod_client_id</config_path>
<depends>
<field id="mode">prod</field>
</depends>
</field>
<field id="prod_api_key" translate="label" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Production API Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<config_path>airwallex/general/prod_api_key</config_path>
<depends>
<field id="mode">prod</field>
</depends>
</field>
<field id="webhook_prod_secret_key" translate="label" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Webhook Production Secret Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<config_path>airwallex/general/webhook_prod_secret_key</config_path>
<depends>
<field id="mode">prod</field>
</depends>
</field>
<field id="webhook_url" translate="label" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Webhook Url</label>
<frontend_model>Airwallex\Payments\Model\Config\Source\WebhookUrl</frontend_model>
</field>
<!-- <field id="demo_client_id" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>Demo Client ID</label>-->
<!-- <config_path>airwallex/general/demo_client_id</config_path>-->
<!-- <depends>-->
<!-- <field id="mode">demo</field>-->
<!-- </depends>-->
<!-- </field>-->
<!-- <field id="demo_api_key" translate="label" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>Demo API Key</label>-->
<!-- <backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>-->
<!-- <config_path>airwallex/general/demo_api_key</config_path>-->
<!-- <depends>-->
<!-- <field id="mode">demo</field>-->
<!-- </depends>-->
<!-- </field>-->
<!-- <field id="webhook_demo_secret_key" translate="label" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>Webhook Demo Secret Key</label>-->
<!-- <backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>-->
<!-- <config_path>airwallex/general/webhook_demo_secret_key</config_path>-->
<!-- <depends>-->
<!-- <field id="mode">demo</field>-->
<!-- </depends>-->
<!-- </field>-->
<!-- <field id="prod_client_id" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>Production Client ID</label>-->
<!-- <config_path>airwallex/general/prod_client_id</config_path>-->
<!-- <depends>-->
<!-- <field id="mode">prod</field>-->
<!-- </depends>-->
<!-- </field>-->
<!-- <field id="prod_api_key" translate="label" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>Production API Key</label>-->
<!-- <backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>-->
<!-- <config_path>airwallex/general/prod_api_key</config_path>-->
<!-- <depends>-->
<!-- <field id="mode">prod</field>-->
<!-- </depends>-->
<!-- </field>-->
<!-- <field id="webhook_prod_secret_key" translate="label" type="obscure" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">-->
<!-- <label>Webhook Production Secret Key</label>-->
<!-- <backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>-->
<!-- <config_path>airwallex/general/webhook_prod_secret_key</config_path>-->
<!-- <depends>-->
<!-- <field id="mode">prod</field>-->
<!-- </depends>-->
<!-- </field>-->
<!-- <field id="webhook_url" translate="label" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">-->
<!-- <label>Webhook Url</label>-->
<!-- <frontend_model>Airwallex\Payments\Model\Config\Source\WebhookUrl</frontend_model>-->
<!-- </field>-->
<field id="request_logger" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1">
<label>Request Logger</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down
Loading