added sms auth system

pull/49/head
A1Gard 4 months ago
parent ceb48845ab
commit d944d317a4

@ -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

@ -9,6 +9,7 @@ use App\Models\Part;
use App\Models\Menu;
use App\Models\Product;
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;
/**
@ -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();
}
@ -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;
}

@ -52,6 +52,22 @@ class InvoiceController extends XController
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;

@ -520,6 +520,20 @@ class ClientController extends Controller
$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();

@ -68,7 +68,7 @@ class CustomerController extends Controller
$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);
}

@ -115,6 +115,20 @@ class Invoice extends Model
/** @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){

@ -159,9 +159,14 @@ return [
|
*/
'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',''),
],
/*
|--------------------------------------------------------------------------

@ -95,6 +95,26 @@ class SettingSeeder extends Seeder
],
],
'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"),
@ -200,11 +220,11 @@ class SettingSeeder extends Seeder
$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();
}
}

@ -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);
}
});
});

@ -272,7 +272,7 @@
<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)}}"

@ -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}}
@ -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>

@ -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}}
@ -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>

Loading…
Cancel
Save