diff --git a/Controller/CredentialsChecker/GetToken.php b/Controller/CredentialsChecker/GetToken.php index 0abffcf3..83de9bdd 100644 --- a/Controller/CredentialsChecker/GetToken.php +++ b/Controller/CredentialsChecker/GetToken.php @@ -37,16 +37,12 @@ private function sendPostRequest($url, $username, $password, $postData) { // Initialize cURL $ch = curl_init(); - // Set the URL + // Set the URL and method 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 + // Basic Auth and headers 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 @@ -98,52 +94,57 @@ public function execute() { $result = $this->resultJsonFactory->create(); + // Validate the request origin $requestOrigin = $this->getRequest()->getHeader('X-Requested-From'); - if ($requestOrigin !== 'MagentoFrontend') { - return $result->setHttpResponseCode(403)->setData(['error' => 'Unauthorized request']); + return $result->setHttpResponseCode(403)->setData([ + 'error' => true, + 'message' => 'Unauthorized request' + ]); } + // Get username and password $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); - } - - // Check if there's a message in the response - if (isset($responseArray['message'])) { - return $result->setHttpResponseCode(400)->setData([ - 'error' => 'Error fetching token', - 'response' => $responseArray['message'] - ]); - } - - 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() + if (empty($hostedFieldsUsername) || empty($hostedFieldsPassword)) { + return $result->setHttpResponseCode(400)->setData([ + 'error' => true, + 'message' => 'Hosted Fields Username or Password is empty.' + ]); + } + + // Try to fetch the token + 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); + + // Check for successful response + if (isset($responseArray['access_token'])) { + return $result->setData([ + 'error' => false, + 'data' => $responseArray ]); } - } else { + + // Handle error response + $message = isset($responseArray['message']) ? $responseArray['message'] : 'Unknown error occurred'; return $result->setHttpResponseCode(400)->setData([ - 'error' => 'Hosted Fields Username or Password is empty.' + 'error' => true, + 'message' => 'Error fetching token: ' . $message + ]); + + } catch (\Exception $e) { + $this->logger->error('Error occurred while fetching token: ' . $e->getMessage()); + return $result->setHttpResponseCode(500)->setData([ + 'error' => true, + 'message' => 'An error occurred while fetching the token: ' . $e->getMessage() ]); } } 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 2589f8f2..a6cdf278 100644 --- a/view/frontend/web/js/view/payment/method-renderer/creditcards.js +++ b/view/frontend/web/js/view/payment/method-renderer/creditcards.js @@ -78,12 +78,16 @@ define( } }); - if (response.access_token) { - await this.initHostedFields(response.access_token); + // Check for error field in response + if (response.error) { + // Display the error message in the observable + this.oauthTokenError("Error getting OAuth token: " + response.message); } else { - this.oauthTokenError("Error getting OAuth token: " + response.error); + // Success: Initialize hosted fields with access token + await this.initHostedFields(response.data.access_token); } } catch (error) { + // Catch any other errors (e.g., network issues) this.oauthTokenError("Error getting OAuth token: " + error.message); } }, diff --git a/view/frontend/web/template/payment/buckaroo_magento2_creditcards.html b/view/frontend/web/template/payment/buckaroo_magento2_creditcards.html index 5977c296..aabc8b09 100644 --- a/view/frontend/web/template/payment/buckaroo_magento2_creditcards.html +++ b/view/frontend/web/template/payment/buckaroo_magento2_creditcards.html @@ -19,8 +19,6 @@ -
-
@@ -28,7 +26,6 @@
-
@@ -78,6 +75,8 @@
+
+