Skip to content

Commit

Permalink
update error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vegimcarkaxhija committed Sep 26, 2024
1 parent 0ef018b commit 6cc3514
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 48 deletions.
85 changes: 43 additions & 42 deletions Controller/CredentialsChecker/GetToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
]);
}
}
Expand Down
10 changes: 7 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 @@ -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);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@
</div>
</div>

<div class="oauth-token-error text-red-500 text-xs italic" data-bind="text: oauthTokenError, visible: oauthTokenError"></div>

<div class="payment-method-content">
<div class="payment-method-billing-address">
<!-- ko foreach: $parent.getRegion(getBillingAddressFormName()) -->
<!-- ko template: getTemplate() --><!-- /ko -->
<!--/ko-->
</div>


<div class="payment-method-second-col billing-address-form">
<form class="bg-white px-8 pt-6 pb-8">
<div class="mb-4">
Expand Down Expand Up @@ -78,6 +75,8 @@
<span data-bind="i18n: 'Please make sure all fields are filled in correctly before proceeding.'"></span>
</div>

<div class="field oauth-token-error text-red-500" data-bind="text: oauthTokenError, visible: oauthTokenError"></div>

<div class="flex items-center justify-between">
<button id="pay"
class="bg-[#ff5555] hover:bg-blue-700 text-white py-2 px-4 rounded focus:outline-none focus:shadow-outline"
Expand Down

0 comments on commit 6cc3514

Please sign in to comment.