Skip to content

Commit

Permalink
added sms auth system
Browse files Browse the repository at this point in the history
  • Loading branch information
A1Gard committed Sep 8, 2024
1 parent ceb4884 commit d944d31
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 49 deletions.
9 changes: 7 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ CURRENCY_SYMBOL="$"
CURRENCY_FACTOR=1
CURRENCY_CODE=USD

SIGN_SMS=true
SIGN_DRIVER=Kavenegar
SMS_SING=true
SMS_DRIVER=Kavenegar
SMS_TOKEN=
SMS_USER=
SMS_PASSWORD=
SMS_URL="https://api.kavenegar.com/v1/TOKEN/verify/lookup.json"
SMS_NUMBER=

ZARINPAL_MERCHANT=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
ZIBAL_MERCHANT=zibal
Expand Down
69 changes: 68 additions & 1 deletion app/Helpers/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\Menu;
use App\Models\Product;
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;


/**
Expand Down Expand Up @@ -887,7 +888,7 @@ function getCategorySubCatsBySetting($key, $limit = 10, $order = 'id', $dir = "D
{
$c = Category::where('id', getSetting($key) ?? 1)->first();
if ($c == null) {
return [];
return [];
}
return $c->children()->orderBy($order, $dir)->limit($limit)->get();
}
Expand Down Expand Up @@ -1204,3 +1205,69 @@ function fixUrlLang($url)
}
return $url;
}


/**
* Send SMS
* @param $text
* @param $number
* @param $args
* @return bool
* @throws \GuzzleHttp\Exception\GuzzleException
*/
function sendingSMS($text, $number, $args)
{

if (config('app.sms.url') == '' || config('app.sms.url') == null) {
return false;
}
if (config('app.sms.driver') == 'Kavenegar') {
$url = str_replace('TOKEN', config('app.sms.token'), config('app.sms.url')) . '?' . http_build_query($args);
$response = Http::get($url);
$r = json_decode($response->body(), true);
if ($r['return']['status'] != 200) {
\Illuminate\Support\Facades\Log::error($r);
return false;
}
return true;

}
$url = config('app.sms.url');

foreach ($args as $k => $arg) {
$text = str_replace('%' . $k, $arg, $text);
}
$fields = [
'user' => config('app.sms.url'),
'password' => config('app.sms.password'),
'to' => $number,
'from' => config('app.sms.number'),
'text' => $text,
'isflash' => 'false',
];

// Create a new Guzzle client
$client = new Client();

try {
// Send a POST request
$response = $client->post($url, [
'form_params' => $fields,
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Cache-Control' => 'no-cache',
],
]);

// Get the response body as a string
$result = $response->getBody()->getContents();
} catch (\Exception $e) {
// Handle exception
// You can log the error or return an error response here
Log::error($e->getMessage());
return false;
}

return true;

}
16 changes: 16 additions & 0 deletions app/Http/Controllers/Admin/InvoiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ public function __construct()
public function save($invoice, $request)
{

if($invoice->tracking_code != $request->get('tracking_code') && strlen(trim($request->tracking_code)) == 24){
if (config('app.sms.driver') == 'Kavenegar'){
$args = [
'receptor' => $invoice->customer->mobile,
'template' => trim(getSetting('sent')),
'token' => trim($request->tracking_code)
];
}else{
$args = [
'code' => trim($request->tracking_code),
];
}

sendingSMS(getSetting('sent'),$invoice->customer->mobile,$args);
}

$invoice->transport_id = $request->input('transport_id', null);
$invoice->address_id = $request->input('address_id', null);
$invoice->tracking_code = $request->tracking_code;
Expand Down
14 changes: 14 additions & 0 deletions app/Http/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,20 @@ public function sendSms(Request $request)
$customer = Customer::where('mobile', $request->input('tel'));
$code = rand(11111, 99999);

if (config('app.sms.driver') == 'Kavenegar'){
$args = [
'receptor' => $request->input('tel'),
'template' => trim(getSetting('sign')),
'token' => $code
];
}else{
$args = [
'code' => $code,
];
}

sendingSMS(getSetting('sign'),$request->input('tel'),$args);

Log::info('auth code: ' . $code);
if ($customer->count() == 0) {
$customer = new Customer();
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/CustomerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function save(Request $request)
$customer = auth('customer')->user();
$customer->name = $request->name;
$customer->email = $request->email;
$customer->mobile = $request->mobile;
// $customer->mobile = $request->mobile;
if ($request->has('password') && trim($request->input('password')) != '') {
$customer->password = bcrypt($request->password);
}
Expand Down
14 changes: 14 additions & 0 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ public function storeSuccessPayment($paymentId, $referenceId, $cardNumber = null
/** @var \App\Models\Invoice $this */
$this->status = "PAID";
$this->save();
if (config('app.sms.driver') == 'Kavenegar'){
$args = [
'receptor' => $this->customer->mobile,
'template' => trim(getSetting('order')),
'token10' => $this->customer->name,
'token' => $this->hash,
'token2' => number_format($this->total_price)
];
}else{
$args = array_merge($this->toArray(),$this->customer->toArray());
}

sendingSMS(getSetting('order'),$this->customer->mobile,$args);

try {
event(new InvoiceSucceed($this, $payment));
}catch (\Throwable $exception){
Expand Down
11 changes: 8 additions & 3 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,14 @@
|
*/

'sign' => [
'sms' => env('SIGN_SMS',false),
'driver' => env('SIGN_DRIVER',''),
'sms' => [
'sign' => env('SMS_SING',false),
'driver' => env('SMS_DRIVER','direct'),
'username' => env('SMS_USERNAME',''),
'password' => env('SMS_PASSWORD',''),
'number' => env('SMS_NUMBER',''),
'url' => env('SMS_URL',''),
'token' => env('SMS_TOKEN',''),
],
/*
|--------------------------------------------------------------------------
Expand Down
28 changes: 24 additions & 4 deletions database/seeders/SettingSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ public function run(): void
],

],
'SMS' => [
[
'title' => __("Sign-in authentication"),
'key' => 'sign',
'type' => 'LONGTEXT',
'value' => 'sign',
],
[
'title' => __("Order confirmation"),
'key' => 'order',
'type' => 'LONGTEXT',
'value' => 'order',
],
[
'title' => __("Sent message"),
'key' => 'sent',
'type' => 'LONGTEXT',
'value' => 'sent',
],
],
'SEO' => [
[
'title' => __("Common keyword"),
Expand Down Expand Up @@ -200,11 +220,11 @@ public function run(): void
$setting->title = $set['title'];
$setting->section = $section;
$setting->key = $set['key'];
$setting->value = $set['value']??null;
$setting->type = $set['type']??'TEXT';
$setting->ltr = $set['ltr']??false;
$setting->value = $set['value'] ?? null;
$setting->type = $set['type'] ?? 'TEXT';
$setting->ltr = $set['ltr'] ?? false;
$setting->is_basic = true;
$setting->size = $set['size']??12;;
$setting->size = $set['size'] ?? 12;;
$setting->save();
}
}
Expand Down
78 changes: 45 additions & 33 deletions resources/js/client-custom/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,56 @@ function isValidMobile(p) {

document.addEventListener('DOMContentLoaded', function () {
document.querySelector('#send-auth-code')?.addEventListener('click', async function () {
let url = this.getAttribute('data-route');
let tel = document.querySelector('#tel').value;
if (tel.length < 11 || !isValidMobile(tel)){
window.$toast.error('Invalid mobile');
return;
}
try {
let url = this.getAttribute('data-route');
let tel = document.querySelector('#tel').value;
if (tel.length < 11 || !isValidMobile(tel)) {
window.$toast.error('Invalid mobile');
return;
}

let resp = await axios.get(url+'?tel='+tel);
if (resp.data.OK){
window.$toast.success(resp.data.message);
document.querySelector('#tel').setAttribute('readonly','');
document.querySelector('.not-send').style.display = 'block';
document.querySelector('.sent').style.display = 'none';
}else{
window.$toast.error(resp.data.message);
let resp = await axios.get(url + '?tel=' + tel);
if (resp.data.OK) {
window.$toast.success(resp.data.message);
document.querySelector('#tel').setAttribute('readonly', '');
document.querySelector('.not-send').style.display = 'block';
document.querySelector('.sent').style.display = 'none';
} else {
window.$toast.error(resp.data.message);
}
} catch (e) {
window.$toast.error(e.message);
}

});
document.querySelector('#send-auth-check')?.addEventListener('click', async function () {
let url = this.getAttribute('data-route');
let tel = document.querySelector('#tel').value;
let code = document.querySelector('#auth').value;
if (tel.length < 11 || !isValidMobile(tel)){
window.$toast.error('Invalid mobile');
return;
}
if (code.length != 5 ){
window.$toast.error('Invalid code');
return;
}
try {


let url = this.getAttribute('data-route');
let tel = document.querySelector('#tel').value;
let code = document.querySelector('#auth').value;
if (tel.length < 11 || !isValidMobile(tel)) {
window.$toast.error('Invalid mobile');
return;
}
if (code.length != 5) {
window.$toast.error('Invalid code');
return;
}

let resp = await axios.get(url + '?tel=' + tel + '&code=' + code);
if (resp.data.OK) {
window.$toast.success(resp.data.message);
setTimeout(() => {
window.location.href = this.getAttribute('data-profile');
}, 5000);
} else {
window.$toast.error(resp.data.message);
}
} catch (e) {
window.$toast.error(e.message);

let resp = await axios.get(url+'?tel='+tel+'&code='+code);
if (resp.data.OK){
window.$toast.success(resp.data.message);
setTimeout( () => {
window.location.href = this.getAttribute('data-profile');
},5000);
}else{
window.$toast.error(resp.data.message);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class="form-control @error('email') is-invalid @enderror"
<label for="mobile">
{{__('Mobile')}}
</label>
<input name="mobile" type="text" @if(config('app.sign.sms')) readonly
<input name="mobile" type="text" @if(config('app.sms.sign')) readonly
@endif class="form-control @error('mobile') is-invalid @enderror"
placeholder="{{__('Mobile')}}"
value="{{old('mobile',auth('customer')->user()->mobile??null)}}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<section id='LoginBigBg' class="content"
style="background-image: url('{{asset('upload/images/'.$data->area->name.'.'.$data->part.'.jpg')}}')">
<form @if(!config('app.sign.sms')) action="{{route('client.sign-in-do')}}" @endif id="login-form" method="post">
<form @if(!config('app.sms.sign')) action="{{route('client.sign-in-do')}}" @endif id="login-form" method="post">
@csrf
<h3>
{{$subtitle}}
Expand All @@ -9,7 +9,7 @@
@include('components.err')
</div>
<div id="login-content">
@if(!config('app.sign.sms'))
@if(!config('app.sms.sign'))
<label>
{{__("Email")}}
</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
>
<div id="login-container"
style="background-image: url('{{asset('upload/images/'.$data->area->name.'.'.$data->part.'.jpg')}}')">
<form @if(!config('app.sign.sms')) action="{{route('client.sign-in-do')}}" @endif id="login-form" method="post">
<form @if(!config('app.sms.sign')) action="{{route('client.sign-in-do')}}" @endif id="login-form" method="post">
@csrf
<h3>
{{$subtitle}}
Expand All @@ -11,7 +11,7 @@
@include('components.err')
</div>
<div id="login-content">
@if(!config('app.sign.sms'))
@if(!config('app.sms.sign'))
<label>
{{__("Email")}}
</label>
Expand Down

0 comments on commit d944d31

Please sign in to comment.