From 824fe8265123b124ed12cd8962b30586c17c40e9 Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Mon, 29 Jul 2024 14:47:38 +0200 Subject: [PATCH 01/17] hosted fields --- Controller/CredentialsChecker/GetToken.php | 142 +++++ Model/ConfigProvider/Account.php | 6 + etc/adminhtml/system/account.xml | 16 + etc/csp_whitelist.xml | 8 +- view/frontend/layout/checkout_index_index.xml | 1 + view/frontend/requirejs-config.js | 2 + .../payment/method-renderer/creditcards.js | 513 ++++++++---------- .../buckaroo_magento2_creditcards.html | 169 ++---- 8 files changed, 442 insertions(+), 415 deletions(-) create mode 100644 Controller/CredentialsChecker/GetToken.php diff --git a/Controller/CredentialsChecker/GetToken.php b/Controller/CredentialsChecker/GetToken.php new file mode 100644 index 000000000..10679c186 --- /dev/null +++ b/Controller/CredentialsChecker/GetToken.php @@ -0,0 +1,142 @@ +resultJsonFactory = $resultJsonFactory; + $this->logger = $logger; + $this->configProviderAccount = $configProviderAccount; + $this->encryptor = $encryptor; + $this->store = $storeManager->getStore(); + parent::__construct($context); + } + + private function sendPostRequest($url, $username, $password, $postData) { + // Initialize cURL + $ch = curl_init(); + + // Set the URL + curl_setopt($ch, CURLOPT_URL, $url); + + // Set the HTTP method to POST + curl_setopt($ch, CURLOPT_POST, true); + + // Set the username and password for Basic Auth + curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); + + // Set the Content-Type to application/x-www-form-urlencoded + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']); + + // Set the POST fields + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); + + // Return the response instead of printing it + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + // Execute the request + $response = curl_exec($ch); + + // Check for cURL errors + if ($response === false) { + $error = 'Curl error: ' . curl_error($ch); + curl_close($ch); + throw new \Exception($error); + } + + // Close the cURL session + curl_close($ch); + return $response; + } + + protected function getHostedFieldsUsername() + { + try { + return $this->encryptor->decrypt( + $this->configProviderAccount->getHostedFieldsUsername($this->store) + ); + } catch (\Exception $e) { + $this->logger->error('Error decrypting Hosted Fields Username: ' . $e->getMessage()); + return null; + } + } + + protected function getHostedFieldsPassword() + { + try { + return $this->encryptor->decrypt( + $this->configProviderAccount->getHostedFieldsPassword($this->store) + ); + } catch (\Exception $e) { + $this->logger->error('Error decrypting Hosted Fields Password: ' . $e->getMessage()); + return null; + } + } + + public function execute() + { + $result = $this->resultJsonFactory->create(); + + $requestOrigin = $this->getRequest()->getHeader('X-Requested-From'); + + if ($requestOrigin !== 'MagentoFrontend') { + return $result->setHttpResponseCode(403)->setData(['error' => 'Unauthorized request']); + } + + $hostedFieldsUsername = $this->getHostedFieldsUsername(); + $hostedFieldsPassword = $this->getHostedFieldsPassword(); + + if (!empty($hostedFieldsUsername) && !empty($hostedFieldsPassword)) { + try { + $url = "https://auth.buckaroo.io/oauth/token"; + $postData = [ + 'scope' => 'hostedfields:save', + 'grant_type' => 'client_credentials' + ]; + + $response = $this->sendPostRequest($url, $hostedFieldsUsername, $hostedFieldsPassword, $postData); + $responseArray = json_decode($response, true); + + if (isset($responseArray['access_token'])) { + return $result->setData($responseArray); + } + + return $result->setHttpResponseCode(500)->setData([ + 'error' => 'Unable to fetch token', + 'response' => $response + ]); + } catch (\Exception $e) { + $this->logger->error('Error occurred while fetching token: ' . $e->getMessage()); + return $result->setHttpResponseCode(500)->setData([ + 'error' => 'An error occurred while fetching the token', + 'message' => $e->getMessage() + ]); + } + } else { + return $result->setHttpResponseCode(400)->setData([ + 'error' => 'Hosted Fields Username or Password is empty.' + ]); + } + } +} diff --git a/Model/ConfigProvider/Account.php b/Model/ConfigProvider/Account.php index b6cecaaa2..f2595f2b2 100644 --- a/Model/ConfigProvider/Account.php +++ b/Model/ConfigProvider/Account.php @@ -31,6 +31,8 @@ * @method mixed getActive() * @method mixed getSecretKey() * @method mixed getMerchantKey() + * @method mixed getHostedFieldsUsername() + * @method mixed getHostedFieldsPassword() * @method mixed getMerchantGuid() * @method mixed getTransactionLabel() * @method mixed getCertificateFile() @@ -58,6 +60,8 @@ class Account extends AbstractConfigProvider const XPATH_ACCOUNT_ACTIVE = 'buckaroo_magento2/account/active'; const XPATH_ACCOUNT_SECRET_KEY = 'buckaroo_magento2/account/secret_key'; const XPATH_ACCOUNT_MERCHANT_KEY = 'buckaroo_magento2/account/merchant_key'; + const XPATH_ACCOUNT_HOSTED_FIELDS_USERNAME = 'buckaroo_magento2/account/hosted_fields_username'; + const XPATH_ACCOUNT_HOSTED_FIELDS_PASSWORD = 'buckaroo_magento2/account/hosted_fields_password'; const XPATH_ACCOUNT_MERCHANT_GUID = 'buckaroo_magento2/account/merchant_guid'; const XPATH_ACCOUNT_TRANSACTION_LABEL = 'buckaroo_magento2/account/transaction_label'; const XPATH_ACCOUNT_INVOICE_HANDLING = 'buckaroo_magento2/account/invoice_handling'; @@ -121,6 +125,8 @@ public function getConfig($store = null) 'active' => $this->getActive($store), 'secret_key' => $this->getSecretKey($store), 'merchant_key' => $this->getMerchantKey($store), + 'hosted_fields_username' => $this->getHostedFieldsUsername($store), + 'hosted_fields_password' => $this->getHostedFieldsPassword($store), 'merchant_guid' => $this->getMerchantGuid($store), 'transaction_label' => $this->getTransactionLabel($store), 'certificate_file' => $this->getCertificateFile($store), diff --git a/etc/adminhtml/system/account.xml b/etc/adminhtml/system/account.xml index e8e36dcce..6c664d97c 100644 --- a/etc/adminhtml/system/account.xml +++ b/etc/adminhtml/system/account.xml @@ -56,6 +56,22 @@ + + + + The Secret Key can be retrieved in Payment Plaza under Configuration > Security > Secret Key. For support contact Buckaroo. + Magento\Config\Model\Config\Backend\Encrypted + buckaroo_magento2/account/hosted_fields_username + + + + + + The (Merchant) Key can be retrieved in Payment Plaza under My Buckaroo > Websites. For support contact Buckaroo. + Magento\Config\Model\Config\Backend\Encrypted + buckaroo_magento2/account/hosted_fields_password + + diff --git a/etc/csp_whitelist.xml b/etc/csp_whitelist.xml index 35c36e996..3a311fc5b 100644 --- a/etc/csp_whitelist.xml +++ b/etc/csp_whitelist.xml @@ -1,6 +1,5 @@ - + @@ -8,6 +7,9 @@ list.xsd"> https://checkout.buckaroo.nl https://testcheckout.buckaroo.nl https://buckaroo.nl + https://hostedfields-externalapi.alpha.buckaroo.aws + https://hostedfields-externalapi.prod-pci.buckaroo.io + https://cdn.tailwindcss.com @@ -27,6 +29,8 @@ list.xsd"> wss://websockets.buckaroo.io/ https://checkout.buckaroo.nl https://testcheckout.buckaroo.nl + https://hostedfields-externalapi.alpha.buckaroo.aws + https://hostedfields-externalapi.prod-pci.buckaroo.io diff --git a/view/frontend/layout/checkout_index_index.xml b/view/frontend/layout/checkout_index_index.xml index e5ff51df4..bff197388 100644 --- a/view/frontend/layout/checkout_index_index.xml +++ b/view/frontend/layout/checkout_index_index.xml @@ -22,6 +22,7 @@ +