Skip to content

Commit

Permalink
Updated to FraudLabs Pro SMS Verification API v2
Browse files Browse the repository at this point in the history
  • Loading branch information
fraudlabspro committed Dec 28, 2023
1 parent 2e262b7 commit 743eb1a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 deletions.
59 changes: 35 additions & 24 deletions Block/Fraudlabsprosmsverificationsend.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function methodBlock()
}
$params['format'] = 'json';
$params['source'] = 'magento';
$params['key'] = $apiKey;
$params['tel'] = trim($tel);
if (strpos($params['tel'], '+') !== 0)
$params['tel'] = '+' . $params['tel'];
Expand All @@ -56,42 +57,52 @@ public function methodBlock()
$params['flp_id'] = $flpId;
$params['tel_cc'] = (filter_input(INPUT_POST, 'tel_cc')) ? (filter_input(INPUT_POST, 'tel_cc')) : '';
$params['otp_timeout'] = $otpTimeout;
$url = 'https://api.fraudlabspro.com/v1/verification/send';

$query = '';
$request = $this->post('https://api.fraudlabspro.com/v2/verification/send', $params);

foreach($params as $key=>$value) {
$query .= '&' . $key . '=' . rawurlencode($value);
}

$url = $url . '?key=' . $apiKey . $query;

$result = file_get_contents($url);
if ($request) {
$data = json_decode($request);

// network error, wait 2 seconds for next retry
if (!$result) {
for ($i = 0; $i < 3; ++$i) {
sleep(2);
$result = file_get_contents($url);
if (isset($data->error->error_message)) {
return $data->error->error_message;
} else {
return 'FLPOK' . $data->tran_id . $data->otp_char;
}
} else {
// Network error
return;
}
}

// still having network issue after 3 retries, give up
if (!$result)
return;
private function post($url, $fields = '')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HTTP_VERSION, '1.1');
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);

if (!empty($fields)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, (is_array($fields)) ? http_build_query($fields) : $fields);
}

// Get the HTTP response
$data = json_decode($result);
$response = curl_exec($ch);

if (trim($data->error) != '') {
return $data->error;
} else {
return 'FLPOK' . $data->tran_id . $data->otp_char;
if (!curl_errno($ch)) {
return $response;
}

return false;
}

private function _unserialize($data){
private function _unserialize($data)
{
if (class_exists(\Magento\Framework\Serialize\SerializerInterface::class)) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$serializer = $objectManager->create(\Magento\Framework\Serialize\SerializerInterface::class);
Expand Down
12 changes: 6 additions & 6 deletions Block/Fraudlabsprosmsverificationverify.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function methodBlock()
if ($params['otp'] == 'OTP cannot be empty.') return 'OTP cannot be empty.';
$params['tran_id'] = (filter_input(INPUT_POST, 'tran_id')) ? (filter_input(INPUT_POST, 'tran_id')) : 'Tran ID cannot be empty.';
if ($params['tran_id'] == 'Tran ID cannot be empty.') return 'Tran ID cannot be empty.';
$url = 'https://api.fraudlabspro.com/v1/verification/result';
$url = 'https://api.fraudlabspro.com/v2/verification/result';

$query = '';

Expand All @@ -61,12 +61,12 @@ public function methodBlock()
// Get the HTTP response
$data = json_decode($result);

if (trim($data->error) != '') {
if ($data->error == 'Invalid OTP.') {
return 'ERROR 601-' . $data->error;
if (isset($data->error->error_message)) {
if ($data->error->error_message == 'INVALID OTP') {
return 'ERROR 601-' . $data->error->error_message;
} else {
$this->write_debug_log('Error occurred during FraudLabs Pro SMS OTP Verify. ERROR: ' . $data->error);
return 'ERROR 600-' . $data->error;
$this->write_debug_log('Error occurred during FraudLabs Pro SMS OTP Verify. ERROR: ' . $data->error->error_messag);
return 'ERROR 600-' . $data->error->error_message;
}
} else {
if ( $sms_order_id != "" ) {
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Revision History for FraudLabs Pro SMS Verification Magento 2

1.3.0 2023-12-27
Update to FraudLabs Pro SMS Verification API v2..

1.2.3 2023-06-12
Update FraudLabs Pro SMS Verification settings page.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hexasoft/module-fraudlabsprosmsverification",
"description": "FraudLabs Pro SMS Verification extension for Magento 2 that help merchants to authenticate the client's identity by sending them a SMS for verification.",
"version": "1.2.3",
"version": "1.3.0",
"type": "magento2-module",
"require": {
"php": ">=5.5"
Expand Down

0 comments on commit 743eb1a

Please sign in to comment.