Skip to content

Commit

Permalink
Improve SMS verification process
Browse files Browse the repository at this point in the history
  • Loading branch information
fraudlabspro committed Apr 1, 2021
1 parent 0dc9b68 commit 37b91ae
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 45 deletions.
50 changes: 31 additions & 19 deletions Block/Fraudlabsprosmsverification.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public function methodBlock()
var phoneNum, defaultCc;
jQuery( document ).ready(function() {
if( jQuery("#sms_phone_cc").length ){
defaultCc = jQuery("#sms_phone_cc").val();
} else {
defaultCc = "US";
defaultCc = jQuery("#sms_phone_cc").val();
} else {
defaultCc = "US";
}
phoneNum = window.intlTelInput(document.querySelector("#phone_number"), {
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.5/js/utils.min.js",
Expand All @@ -97,9 +97,10 @@ public function methodBlock()
jQuery("#get_otp").click(function(e) {
if (jQuery("#phone_number").val() == "") {
alert("Please enter a valid phone number.");
jQuery("#sms_err").html("Please enter a valid phone number.");
jQuery("#sms_err").show();
jQuery("#phone_number").focus();
}else if (!confirm("Send OTP to " + phoneNum.getNumber() + "?")) {
} else if (!confirm("Send OTP to " + phoneNum.getNumber() + "?")) {
e.preventDefault();
} else {
doOTP();
Expand All @@ -115,7 +116,8 @@ public function methodBlock()
}
if(sessionStorage.resent_count == 3){
alert("Maximum number of retries to send verification SMS exceeded. Please wait for your OTP code.");
jQuery("#sms_err").html("Maximum number of retries to send verification SMS exceeded. Please wait for your OTP code.");
jQuery("#sms_err").show();
jQuery("#get_otp").hide();
jQuery("#resend_otp").hide();
} else if (!confirm("Send OTP to " + phoneNum.getNumber() + "?")) {
Expand All @@ -131,7 +133,8 @@ public function methodBlock()
});
if(sessionStorage.resent_count >= 3){
alert("Maximum number of retries to send verification SMS exceeded. Please wait for your OTP code.");
jQuery("#sms_err").html("Maximum number of retries to send verification SMS exceeded. Please wait for your OTP code.");
jQuery("#sms_err").show();
jQuery("#get_otp").hide();
jQuery("#resend_otp").hide();
}
Expand All @@ -140,16 +143,15 @@ public function methodBlock()
jQuery(".btn-checkout").prop("disabled",false);
} else if (jQuery.trim(jQuery("#sms_verified").val()) == "") {
jQuery(".btn-checkout").prop("disabled",true);
/* alert("Please complete the SMS Verification.");
jQuery("#sms_otp2").focus();
document.getElementById("verifysms").scrollIntoView(true);*/
}
function doOTP() {
var data = {
"tel": phoneNum.getNumber(),
"sms_order_id": jQuery("#sms_order_id").val(),
"sms_code": jQuery("#sms_code").val()
"tel_cc": phoneNum.getSelectedCountryData().iso2.toUpperCase(),
"sms_order_id": jQuery("#sms_order_id").val()
};
jQuery.ajax({
type: "POST",
Expand All @@ -167,26 +169,30 @@ function sms_doOTP_success(data) {
alert("A verification SMS has been sent to " + phoneNum.getNumber() + ".");
jQuery("#sms_tran_id").val(data.substr(num+5, 20));
jQuery("#get_otp").hide();
jQuery("#sms_err").hide();
jQuery("#resend_otp").show();
jQuery("#submit_otp").show();
jQuery("#enter_sms_otp").show();
jQuery("#sms_otp1").val(data.substr(num+25, 6));
jQuery("#phone_number").prop("disabled", true);
jQuery("#sms_otp1").prop("disabled", true);
}
else {
alert("Error: Unable to send the SMS verification message to " + phoneNum.getNumber() + ".");
} else {
jQuery("#sms_err").html("Error: Unable to send the SMS verification message to " + phoneNum.getNumber() + ".");
jQuery("#sms_err").show();
}
}
function sms_doOTP_error() {
alert("Error: Unable to send the SMS verification message to " + phoneNum.getNumber() + ".");
jQuery("#sms_err").html("Error: Unable to send the SMS verification message to " + phoneNum.getNumber() + ".");
jQuery("#sms_err").show();
}
function checkOTP() {
var data = {
"otp": jQuery("#sms_otp1").val() + "-" + jQuery("#sms_otp2").val(),
"tran_id": jQuery("#sms_tran_id").val()
"tran_id": jQuery("#sms_tran_id").val(),
"sms_order_id": jQuery("#sms_order_id").val(),
"sms_code": jQuery("#sms_code").val()
};
jQuery.ajax({
type: "POST",
Expand All @@ -210,19 +216,24 @@ function sms_checkOTP_success(data) {
jQuery("#submit_otp").hide();
jQuery("#get_otp").hide();
jQuery("#resend_otp").hide();
jQuery("#sms_err").hide();
jQuery("#sms_box").hide();
jQuery("#sms_success_status").show();
// redirect the page to get phone number
var url = window.location.href + "&phone=" + phoneNum.getNumber();
window.location.href = url;
}
else {
alert("Error while performing verification.");
} else if (data.includes("ERROR 601")) {
jQuery("#sms_err").html("Error: Invalid OTP. Please enter the correct OTP.");
jQuery("#sms_err").show();
} else {
jQuery("#sms_err").html("Error: Error while performing verification.");
jQuery("#sms_err").show();
}
}
function sms_checkOTP_error() {
alert("Error: Could not perform sms verification.");
jQuery("#sms_err").html("Error: Could not perform sms verification.");
jQuery("#sms_err").show();
}
if(sessionStorage.sms_vrf == "YES") {
Expand All @@ -241,6 +252,7 @@ function sms_checkOTP_error() {
<br />
<div id="sms_box" class="page-width" style="font-size: 14px; border: 1px solid silver; padding: 5px;">
<h1 id="verifysms">SMS Verification<abbr class="required" title="required">*</abbr></h1>
<div id="sms_err" style="background-color:#f8d7da;color:#7d5880;padding:10px;margin-bottom:20px;font-size:1em;display:none;"></div>
<label for="phone_number" id="enter_phone_number">
' . $smsInstruction . ' <br /><br />
Phone Number with country code<br /><input type="text" class="page-width" name="phone_number" id="phone_number" value="" placeholder="Enter phone number.">
Expand Down
39 changes: 18 additions & 21 deletions Block/Fraudlabsprosmsverificationsend.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,30 @@ public function methodBlock()
$tel = (filter_input(INPUT_POST, 'tel')) ? (filter_input(INPUT_POST, 'tel')) : 'Phone number cannot be empty.';
if ($tel == 'Phone number cannot be empty.') return 'Phone number cannot be empty.';
$sms_order_id = (filter_input(INPUT_POST, 'sms_order_id')) ? (filter_input(INPUT_POST, 'sms_order_id')) : '';
$sms_code = (filter_input(INPUT_POST, 'sms_code')) ? (filter_input(INPUT_POST, 'sms_code')) : '';
if ($sms_order_id != "") {
$order = $this->getOrder($sms_order_id);
if ($order->getfraudlabspro_response()) {
if (is_null(json_decode($order->getfraudlabspro_response(), true))){
if ($order->getfraudlabspro_response()){
$flpData = $this->_unserialize($order->getfraudlabspro_response());
}
} else {
$flpData = json_decode($order->getfraudlabspro_response(), true);
}
$flpId = $flpData['fraudlabspro_id'];
}
} else {
$flpId = '';
}
$params['format'] = 'json';
$params['source'] = 'magento';
$params['tel'] = trim($tel);
if (strpos($params['tel'], '+') !== 0)
$params['tel'] = '+' . $params['tel'];
$params['mesg'] = ($this->getConfig()->getValue('fraudlabsprosmsverification/active_display/sms_template')) ? $this->getConfig()->getValue('fraudlabsprosmsverification/active_display/sms_template') : 'Hi, your OTP for Magento is {otp}.';
$params['mesg'] = str_replace(['{', '}'], ['<', '>'], $params['mesg']);
$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';

Expand Down Expand Up @@ -69,26 +85,7 @@ public function methodBlock()

if (trim($data->error) != '') {
return $data->error;
}
else {
if ( $sms_order_id != "" ) {
if ( $sms_code != "" ) {
$order = $this->getOrder($sms_order_id);
if ($order->getfraudlabspro_response()) {
if(is_null(json_decode($order->getfraudlabspro_response(), true))){
if($order->getfraudlabspro_response()){
$flpdata = $this->_unserialize($order->getfraudlabspro_response());
}
} else {
$flpdata = json_decode($order->getfraudlabspro_response(), true);
}
if ( $flpdata['fraudlabspro_sms_email_code'] == $sms_code ) {
$flpdata['fraudlabspro_sms_email_code'] = $sms_code . '_VERIFIED';
$order->setfraudlabspro_response(json_encode($flpdata))->save();
}
}
}
}
} else {
return 'FLPOK' . $data->tran_id . $data->otp_char;
}

Expand Down
30 changes: 27 additions & 3 deletions Block/Fraudlabsprosmsverificationverify.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public function methodBlock()
{
$apiKey = ($this->getConfig()->getValue('fraudlabsprosmsverification/active_display/api_key')) ? $this->getConfig()->getValue('fraudlabsprosmsverification/active_display/api_key') : 'API Key cannot be empty.';
if ($apiKey == 'Phone number cannot be empty.') return 'API Key cannot be empty.';
$sms_order_id = (filter_input(INPUT_POST, 'sms_order_id')) ? (filter_input(INPUT_POST, 'sms_order_id')) : '';
$sms_code = (filter_input(INPUT_POST, 'sms_code')) ? (filter_input(INPUT_POST, 'sms_code')) : '';
$params['format'] = 'json';
$params['otp'] = (filter_input(INPUT_POST, 'otp')) ? (filter_input(INPUT_POST, 'otp')) : 'OTP cannot be empty.';
if ($params['otp'] == 'OTP cannot be empty.') return 'OTP cannot be empty.';
Expand Down Expand Up @@ -45,9 +47,31 @@ public function methodBlock()
$data = json_decode($result);

if (trim($data->error) != '') {
return $data->error;
}
else {
if ($data->error == 'Invalid OTP.') {
return 'ERROR 601-' . $data->error;
} else {
$this->write_debug_log('Error occurred during FraudLabs Pro SMS OTP Verify. ERROR: ' . $data->error);
return 'ERROR 600-' . $data->error;
}
} else {
if ( $sms_order_id != "" ) {
if ( $sms_code != "" ) {
$order = $this->getOrder($sms_order_id);
if ($order->getfraudlabspro_response()) {
if(is_null(json_decode($order->getfraudlabspro_response(), true))){
if($order->getfraudlabspro_response()){
$flpdata = $this->_unserialize($order->getfraudlabspro_response());
}
} else {
$flpdata = json_decode($order->getfraudlabspro_response(), true);
}
if ( $flpdata['fraudlabspro_sms_email_code'] == $sms_code ) {
$flpdata['fraudlabspro_sms_email_code'] = $sms_code . '_VERIFIED';
$order->setfraudlabspro_response(json_encode($flpdata))->save();
}
}
}
}
return 'FLPOK';
}
}
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ Revision History for FraudLabs Pro Magento 2
Improve phone number entering.

1.1.2 2021-01-26
Add default country code for SMS sending.
Add default country code for SMS sending.

1.1.3 2021-03-31
Improve SMS verification process.
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.1.2",
"version": "1.1.3",
"type": "magento2-module",
"require": {
"php": "~5.5.0|~5.6.0|~7.0"
Expand Down

0 comments on commit 37b91ae

Please sign in to comment.