Skip to content

Commit

Permalink
update Buckaroo Hosted Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
vegimcarkaxhija committed Nov 6, 2024
1 parent 671432f commit 2622241
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 73 deletions.
46 changes: 33 additions & 13 deletions Controller/CredentialsChecker/GetToken.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Buckaroo\Magento2\Controller\CredentialsChecker;

use Buckaroo\Magento2\Model\ConfigProvider\Account;
use Buckaroo\Magento2\Model\ConfigProvider\Method\Creditcards;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
Expand All @@ -14,7 +14,7 @@ class GetToken extends Action
{
protected $resultJsonFactory;
protected $logger;
protected $configProviderAccount;
protected $configProviderCreditcard;
protected $encryptor;
protected $store;
protected $curlClient;
Expand All @@ -23,14 +23,14 @@ public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
LoggerInterface $logger,
Account $configProviderAccount,
Creditcards $configProviderCreditcard,
EncryptorInterface $encryptor,
StoreManagerInterface $storeManager,
Curl $curlClient
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->logger = $logger;
$this->configProviderAccount = $configProviderAccount;
$this->configProviderCreditcard = $configProviderCreditcard;
$this->encryptor = $encryptor;
$this->store = $storeManager->getStore();
$this->curlClient = $curlClient;
Expand Down Expand Up @@ -59,30 +59,40 @@ 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());
return null;
}
}

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());
return null;
}
}

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();
Expand All @@ -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 {
Expand All @@ -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
]
]);
}

Expand Down
73 changes: 27 additions & 46 deletions Model/ConfigProvider/Method/Creditcards.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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
],
]
];

/**
Expand Down Expand Up @@ -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
);
}

Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion Model/Method/Creditcards.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/**
Expand Down
20 changes: 10 additions & 10 deletions etc/adminhtml/system/payment_methods/creditcards.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@
</tooltip>
</field>

<field id="hosted_fields_username" translate="label comment tooltip" type="obscure" sortOrder="33" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Buckaroo Hosted Fields Username:</label>
<comment><![CDATA[Enter your Buckaroo Hosted Fields Username, obtainable from the Buckaroo Plaza at → Settings → <a href="https://plaza.buckaroo.nl/Configuration/TokenApi/Applications" target="_blank">Token registration</a>.]]></comment>
<tooltip>Please enter your Buckaroo Hosted Fields Username.</tooltip>
<field id="hosted_fields_client_id" translate="label comment tooltip" type="obscure" sortOrder="33" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Buckaroo Hosted Fields Client ID:</label>
<comment><![CDATA[Enter your Buckaroo Hosted Fields Client ID, obtainable from the Buckaroo Plaza at → Settings → <a href="https://plaza.buckaroo.nl/Configuration/TokenApi/Applications" target="_blank">Token registration</a>.]]></comment>
<tooltip>Please enter your Buckaroo Hosted Fields Client ID.</tooltip>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<config_path>buckaroo_magento2/account/hosted_fields_username</config_path>
<config_path>payment/buckaroo_magento2_creditcards/hosted_fields_client_id</config_path>
</field>

<field id="hosted_fields_password" translate="label comment tooltip" type="obscure" sortOrder="34" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Buckaroo Hosted Fields Password:</label>
<comment><![CDATA[Enter your Buckaroo Hosted Fields Password, obtainable from the Buckaroo Plaza at → Settings → <a href="https://plaza.buckaroo.nl/Configuration/TokenApi/Applications" target="_blank">Token registration</a>.]]></comment>
<tooltip>Please enter your Buckaroo Hosted Fields Password.</tooltip>
<field id="hosted_fields_client_secret" translate="label comment tooltip" type="obscure" sortOrder="34" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Buckaroo Hosted Fields Client Secret:</label>
<comment><![CDATA[Enter your Buckaroo Hosted Fields Client Secret, obtainable from the Buckaroo Plaza at → Settings → <a href="https://plaza.buckaroo.nl/Configuration/TokenApi/Applications" target="_blank">Token registration</a>.]]></comment>
<tooltip>Please enter your Buckaroo Hosted Fields Client Secret.</tooltip>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
<config_path>buckaroo_magento2/account/hosted_fields_password</config_path>
<config_path>payment/buckaroo_magento2_creditcards/hosted_fields_client_secret</config_path>
</field>

<field id="title" translate="label comment" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
Expand Down
24 changes: 21 additions & 3 deletions view/frontend/web/js/view/payment/method-renderer/creditcards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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');
Expand All @@ -139,14 +145,26 @@ 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",
fontFamily: 'Consolas, Liberation Mono, Menlo, Courier, monospace',
textAlign: 'left',
background: 'inherit',
color: 'black',
placeholderColor: 'grey'
placeholderColor: 'grey',
cardLogoStyling: cardLogoStyling
};

await sdkClient.mountCardHolderName("#cc-name-wrapper", {
Expand Down

0 comments on commit 2622241

Please sign in to comment.