From 2622241b45bc82f985326406e1f2b8ae6a17c534 Mon Sep 17 00:00:00 2001 From: "v.carkaxhija" Date: Wed, 6 Nov 2024 14:51:28 +0100 Subject: [PATCH] update Buckaroo Hosted Fields --- Controller/CredentialsChecker/GetToken.php | 46 ++++++++---- Model/ConfigProvider/Method/Creditcards.php | 73 +++++++------------ Model/Method/Creditcards.php | 2 +- .../system/payment_methods/creditcards.xml | 20 ++--- .../payment/method-renderer/creditcards.js | 24 +++++- 5 files changed, 92 insertions(+), 73 deletions(-) diff --git a/Controller/CredentialsChecker/GetToken.php b/Controller/CredentialsChecker/GetToken.php index d4cfc5720..6807cf8d9 100644 --- a/Controller/CredentialsChecker/GetToken.php +++ b/Controller/CredentialsChecker/GetToken.php @@ -1,7 +1,7 @@ resultJsonFactory = $resultJsonFactory; $this->logger = $logger; - $this->configProviderAccount = $configProviderAccount; + $this->configProviderCreditcard = $configProviderCreditcard; $this->encryptor = $encryptor; $this->store = $storeManager->getStore(); $this->curlClient = $curlClient; @@ -59,11 +59,11 @@ private function sendPostRequest($url, $username, $password, $postData) { } } - protected function getHostedFieldsUsername() + protected function getHostedFieldsClientId() { try { return $this->encryptor->decrypt( - $this->configProviderAccount->getHostedFieldsUsername($this->store) + $this->configProviderCreditcard->getHostedFieldsClientId($this->store) ); } catch (\Exception $e) { $this->logger->error('Error decrypting Hosted Fields Username: ' . $e->getMessage()); @@ -71,11 +71,11 @@ protected function getHostedFieldsUsername() } } - protected function getHostedFieldsPassword() + protected function getHostedFieldsClientSecret() { try { return $this->encryptor->decrypt( - $this->configProviderAccount->getHostedFieldsPassword($this->store) + $this->configProviderCreditcard->getHostedFieldsClientSecret($this->store) ); } catch (\Exception $e) { $this->logger->error('Error decrypting Hosted Fields Password: ' . $e->getMessage()); @@ -83,6 +83,16 @@ protected function getHostedFieldsPassword() } } + protected function getAllowedIssuers() + { + try { + return $this->configProviderCreditcard->getSupportedServices(); + } catch (\Exception $e) { + $this->logger->error('Error getting Allowed Issuers: ' . $e->getMessage()); + return null; + } + } + public function execute() { $result = $this->resultJsonFactory->create(); @@ -97,15 +107,22 @@ public function execute() } // Get username and password - $hostedFieldsUsername = $this->getHostedFieldsUsername(); - $hostedFieldsPassword = $this->getHostedFieldsPassword(); + $hostedFieldsClientId = $this->getHostedFieldsClientId(); + $hostedFieldsClientSecret = $this->getHostedFieldsClientSecret(); + $issuers = $this->getAllowedIssuers(); - if (empty($hostedFieldsUsername) || empty($hostedFieldsPassword)) { + if (empty($hostedFieldsClientId) || empty($hostedFieldsClientSecret)) { return $result->setHttpResponseCode(400)->setData([ 'error' => true, 'message' => 'Hosted Fields Username or Password is empty.' ]); } + if (empty($issuers)) { + return $result->setHttpResponseCode(400)->setData([ + 'error' => true, + 'message' => 'There is no Allowed Issuers for Hosted Fields.' + ]); + } // Try to fetch the token try { @@ -115,14 +132,17 @@ public function execute() 'grant_type' => 'client_credentials' ]; - $response = $this->sendPostRequest($url, $hostedFieldsUsername, $hostedFieldsPassword, $postData); + $response = $this->sendPostRequest($url, $hostedFieldsClientId, $hostedFieldsClientSecret, $postData); $responseArray = json_decode($response, true); // Check for successful response if (isset($responseArray['access_token'])) { return $result->setData([ 'error' => false, - 'data' => $responseArray + 'data' => [ + 'access_token' => $responseArray['access_token'], + 'issuers' => $issuers + ] ]); } diff --git a/Model/ConfigProvider/Method/Creditcards.php b/Model/ConfigProvider/Method/Creditcards.php index 8e59eb7c0..d5619f73a 100644 --- a/Model/ConfigProvider/Method/Creditcards.php +++ b/Model/ConfigProvider/Method/Creditcards.php @@ -39,10 +39,10 @@ class Creditcards extends AbstractConfigProvider /**#@+ * Creditcard service codes. */ - const CREDITCARD_SERVICE_CODE_MASTERCARD = 'mastercard'; - const CREDITCARD_SERVICE_CODE_VISA = 'visa'; - const CREDITCARD_SERVICE_CODE_AMEX = 'amex'; - const CREDITCARD_SERVICE_CODE_MAESTRO = 'maestro'; + const CREDITCARD_SERVICE_CODE_MASTERCARD = 'MasterCard'; + const CREDITCARD_SERVICE_CODE_VISA = 'Visa'; + const CREDITCARD_SERVICE_CODE_AMEX = 'Amex'; + const CREDITCARD_SERVICE_CODE_MAESTRO = 'Maestro'; const CREDITCARD_SERVICE_CODE_VPAY = 'vpay'; const CREDITCARD_SERVICE_CODE_VISAELECTRON = 'visaelectron'; const CREDITCARD_SERVICE_CODE_CARTEBLEUE = 'cartebleuevisa'; @@ -71,6 +71,8 @@ class Creditcards extends AbstractConfigProvider const XPATH_CREDITCARDS_SELLERS_PROTECTION_UNAUTHORIZEDPAYMENT_ELIGIBLE = 'payment/'. 'buckaroo_magento2_creditcards/sellers_protection_unauthorizedpayment_eligible'; const XPATH_CREDITCARDS_ALLOWED_ISSUERS = 'payment/buckaroo_magento2_creditcards/allowed_issuers'; + const XPATH_CREDITCARDS_HOSTED_FIELDS_CLIENT_ID = 'payment/buckaroo_magento2_creditcards/hosted_fields_client_id'; + const XPATH_CREDITCARDS_HOSTED_FIELDS_CLIENT_SECRET = 'payment/buckaroo_magento2_creditcards/hosted_fields_client_secret'; const XPATH_ALLOWED_CURRENCIES = 'payment/buckaroo_magento2_creditcards/allowed_currencies'; const XPATH_ALLOW_SPECIFIC = 'payment/buckaroo_magento2_creditcards/allowspecific'; const XPATH_SPECIFIC_COUNTRY = 'payment/buckaroo_magento2_creditcards/specificcountry'; @@ -82,21 +84,6 @@ class Creditcards extends AbstractConfigProvider 'code' => self::CREDITCARD_SERVICE_CODE_AMEX, 'sort' => 0 ], - [ - 'name' => 'Carte Bancaire', - 'code' => self::CREDITCARD_SERVICE_CODE_CARTEBANCAIRE, - 'sort' => 0 - ], - [ - 'name' => 'Carte Bleue', - 'code' => self::CREDITCARD_SERVICE_CODE_CARTEBLEUE, - 'sort' => 0 - ], - [ - 'name' => 'Dankort', - 'code' => self::CREDITCARD_SERVICE_CODE_DANKORT, - 'sort' => 0 - ], [ 'name' => 'Maestro', 'code' => self::CREDITCARD_SERVICE_CODE_MAESTRO, @@ -107,31 +94,11 @@ class Creditcards extends AbstractConfigProvider 'code' => self::CREDITCARD_SERVICE_CODE_MASTERCARD, 'sort' => 0 ], - [ - 'name' => 'Nexi', - 'code' => self::CREDITCARD_SERVICE_CODE_NEXI, - 'sort' => 0 - ], - [ - 'name' => 'PostePay', - 'code' => self::CREDITCARD_SERVICE_CODE_POSTEPAY, - 'sort' => 0 - ], [ 'name' => 'VISA', 'code' => self::CREDITCARD_SERVICE_CODE_VISA, 'sort' => 0 - ], - [ - 'name' => 'VISA Electron', - 'code' => self::CREDITCARD_SERVICE_CODE_VISAELECTRON, - 'sort' => 0 - ], - [ - 'name' => 'VPay', - 'code' => self::CREDITCARD_SERVICE_CODE_VPAY, - 'sort' => 0 - ], + ] ]; /** @@ -178,19 +145,19 @@ public function getPaymentFee($storeId = null) return $paymentFee ? $paymentFee : false; } - public function getHostedFieldsUsername() + public function getHostedFieldsClientId() { return $this->scopeConfig->getValue( - 'buckaroo_magento2/account/hosted_fields_username', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE + self::XPATH_CREDITCARDS_HOSTED_FIELDS_CLIENT_ID, + ScopeInterface::SCOPE_STORE ); } - public function getHostedFieldsPassword() + public function getHostedFieldsClientSecret() { return $this->scopeConfig->getValue( - 'buckaroo_magento2/account/hosted_fields_password', - \Magento\Store\Model\ScopeInterface::SCOPE_STORE + self::XPATH_CREDITCARDS_HOSTED_FIELDS_CLIENT_SECRET, + ScopeInterface::SCOPE_STORE ); } @@ -215,4 +182,18 @@ public function formatIssuers() return $issuers; } + + public function getSupportedServices(): array + { + $issuers = $this->formatIssuers(); + $supportedServices = []; + + foreach ($issuers as $issuer) { + if ($issuer['active']) { + $supportedServices[] = $issuer['code']; + } + } + + return $supportedServices; + } } diff --git a/Model/Method/Creditcards.php b/Model/Method/Creditcards.php index 5713883de..329155a9d 100644 --- a/Model/Method/Creditcards.php +++ b/Model/Method/Creditcards.php @@ -129,7 +129,7 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null) */ $gcConfig = $this->configProviderMethodFactory->get('creditcards'); - if ($gcConfig->getHostedFieldsUsername() === null || $gcConfig->getHostedFieldsPassword() === null) { + if ($gcConfig->getHostedFieldsClientId() === null || $gcConfig->getHostedFieldsClientSecret() === null) { return false; } /** diff --git a/etc/adminhtml/system/payment_methods/creditcards.xml b/etc/adminhtml/system/payment_methods/creditcards.xml index fd57032ae..a61118c63 100644 --- a/etc/adminhtml/system/payment_methods/creditcards.xml +++ b/etc/adminhtml/system/payment_methods/creditcards.xml @@ -35,20 +35,20 @@ - - - Token registration.]]> - Please enter your Buckaroo Hosted Fields Username. + + + Token registration.]]> + Please enter your Buckaroo Hosted Fields Client ID. Magento\Config\Model\Config\Backend\Encrypted - buckaroo_magento2/account/hosted_fields_username + payment/buckaroo_magento2_creditcards/hosted_fields_client_id - - - Token registration.]]> - Please enter your Buckaroo Hosted Fields Password. + + + Token registration.]]> + Please enter your Buckaroo Hosted Fields Client Secret. Magento\Config\Model\Config\Backend\Encrypted - buckaroo_magento2/account/hosted_fields_password + payment/buckaroo_magento2_creditcards/hosted_fields_client_secret diff --git a/view/frontend/web/js/view/payment/method-renderer/creditcards.js b/view/frontend/web/js/view/payment/method-renderer/creditcards.js index e299c4dc0..81f24cb73 100644 --- a/view/frontend/web/js/view/payment/method-renderer/creditcards.js +++ b/view/frontend/web/js/view/payment/method-renderer/creditcards.js @@ -79,8 +79,10 @@ define( // Display the error message in the observable this.oauthTokenError("Error getting OAuth token."); } else { + const accessToken = response.data.access_token; + const issuers = response.data.issuers; // Success: Initialize hosted fields with access token - await this.initHostedFields(response.data.access_token); + await this.initHostedFields(accessToken,issuers); } } catch (error) { // Catch any other errors (e.g., network issues) @@ -114,9 +116,13 @@ define( $('#cc-cvc-wrapper iframe').remove(); }, - async initHostedFields(accessToken) { + async initHostedFields(accessToken,issuers) { try { const sdkClient = new BuckarooHostedFieldsSdk.HFClient(accessToken); + var locale = document.documentElement.lang; + var languageCode = locale.split('_')[0]; + sdkClient.setLanguage(languageCode); + sdkClient.setSupportedServices(issuers); await sdkClient.startSession(event => { sdkClient.handleValidation(event, 'cc-name-error', 'cc-number-error', 'cc-expiry-error', 'cc-cvc-error'); @@ -139,6 +145,17 @@ define( this.service = sdkClient.getService(); }); + let cardLogoStyling = { + height:"80%", + position: 'absolute', + border: '1px soli gray', + radius: '5px', + opacity:'1', + transition:'all 0.3s ease', + right:'5px', + backgroundColor:'inherit' + }; + // Define styling and mount hosted fields as needed... let styling = { fontSize: "14px", @@ -146,7 +163,8 @@ define( textAlign: 'left', background: 'inherit', color: 'black', - placeholderColor: 'grey' + placeholderColor: 'grey', + cardLogoStyling: cardLogoStyling }; await sdkClient.mountCardHolderName("#cc-name-wrapper", {