added sms auth system

master
A1Gard 2 weeks ago
parent ceb48845ab
commit d944d317a4

@ -73,8 +73,13 @@ CURRENCY_SYMBOL="$"
CURRENCY_FACTOR=1 CURRENCY_FACTOR=1
CURRENCY_CODE=USD CURRENCY_CODE=USD
SIGN_SMS=true SMS_SING=true
SIGN_DRIVER=Kavenegar 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 ZARINPAL_MERCHANT=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
ZIBAL_MERCHANT=zibal ZIBAL_MERCHANT=zibal

@ -9,6 +9,7 @@ use App\Models\Part;
use App\Models\Menu; use App\Models\Menu;
use App\Models\Product; use App\Models\Product;
use Illuminate\Support\Facades\Route; 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(); $c = Category::where('id', getSetting($key) ?? 1)->first();
if ($c == null) { if ($c == null) {
return []; return [];
} }
return $c->children()->orderBy($order, $dir)->limit($limit)->get(); return $c->children()->orderBy($order, $dir)->limit($limit)->get();
} }
@ -1204,3 +1205,69 @@ function fixUrlLang($url)
} }
return $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) 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->transport_id = $request->input('transport_id', null);
$invoice->address_id = $request->input('address_id', null); $invoice->address_id = $request->input('address_id', null);
$invoice->tracking_code = $request->tracking_code; $invoice->tracking_code = $request->tracking_code;

@ -520,6 +520,20 @@ class ClientController extends Controller
$customer = Customer::where('mobile', $request->input('tel')); $customer = Customer::where('mobile', $request->input('tel'));
$code = rand(11111, 99999); $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); Log::info('auth code: ' . $code);
if ($customer->count() == 0) { if ($customer->count() == 0) {
$customer = new Customer(); $customer = new Customer();

@ -68,7 +68,7 @@ class CustomerController extends Controller
$customer = auth('customer')->user(); $customer = auth('customer')->user();
$customer->name = $request->name; $customer->name = $request->name;
$customer->email = $request->email; $customer->email = $request->email;
$customer->mobile = $request->mobile; // $customer->mobile = $request->mobile;
if ($request->has('password') && trim($request->input('password')) != '') { if ($request->has('password') && trim($request->input('password')) != '') {
$customer->password = bcrypt($request->password); $customer->password = bcrypt($request->password);
} }

@ -115,6 +115,20 @@ class Invoice extends Model
/** @var \App\Models\Invoice $this */ /** @var \App\Models\Invoice $this */
$this->status = "PAID"; $this->status = "PAID";
$this->save(); $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 { try {
event(new InvoiceSucceed($this, $payment)); event(new InvoiceSucceed($this, $payment));
}catch (\Throwable $exception){ }catch (\Throwable $exception){

@ -159,9 +159,14 @@ return [
| |
*/ */
'sign' => [ 'sms' => [
'sms' => env('SIGN_SMS',false), 'sign' => env('SMS_SING',false),
'driver' => env('SIGN_DRIVER',''), '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' => [ 'SEO' => [
[ [
'title' => __("Common keyword"), 'title' => __("Common keyword"),
@ -200,11 +220,11 @@ class SettingSeeder extends Seeder
$setting->title = $set['title']; $setting->title = $set['title'];
$setting->section = $section; $setting->section = $section;
$setting->key = $set['key']; $setting->key = $set['key'];
$setting->value = $set['value']??null; $setting->value = $set['value'] ?? null;
$setting->type = $set['type']??'TEXT'; $setting->type = $set['type'] ?? 'TEXT';
$setting->ltr = $set['ltr']??false; $setting->ltr = $set['ltr'] ?? false;
$setting->is_basic = true; $setting->is_basic = true;
$setting->size = $set['size']??12;; $setting->size = $set['size'] ?? 12;;
$setting->save(); $setting->save();
} }
} }

@ -8,44 +8,56 @@ function isValidMobile(p) {
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {
document.querySelector('#send-auth-code')?.addEventListener('click', async function () { document.querySelector('#send-auth-code')?.addEventListener('click', async function () {
let url = this.getAttribute('data-route'); try {
let tel = document.querySelector('#tel').value; let url = this.getAttribute('data-route');
if (tel.length < 11 || !isValidMobile(tel)){ let tel = document.querySelector('#tel').value;
window.$toast.error('Invalid mobile'); if (tel.length < 11 || !isValidMobile(tel)) {
return; window.$toast.error('Invalid mobile');
} return;
}
let resp = await axios.get(url+'?tel='+tel); let resp = await axios.get(url + '?tel=' + tel);
if (resp.data.OK){ if (resp.data.OK) {
window.$toast.success(resp.data.message); window.$toast.success(resp.data.message);
document.querySelector('#tel').setAttribute('readonly',''); document.querySelector('#tel').setAttribute('readonly', '');
document.querySelector('.not-send').style.display = 'block'; document.querySelector('.not-send').style.display = 'block';
document.querySelector('.sent').style.display = 'none'; document.querySelector('.sent').style.display = 'none';
}else{ } else {
window.$toast.error(resp.data.message); window.$toast.error(resp.data.message);
}
} catch (e) {
window.$toast.error(e.message);
} }
}); });
document.querySelector('#send-auth-check')?.addEventListener('click', async function () { document.querySelector('#send-auth-check')?.addEventListener('click', async function () {
let url = this.getAttribute('data-route'); try {
let tel = document.querySelector('#tel').value;
let code = document.querySelector('#auth').value;
if (tel.length < 11 || !isValidMobile(tel)){ let url = this.getAttribute('data-route');
window.$toast.error('Invalid mobile'); let tel = document.querySelector('#tel').value;
return; let code = document.querySelector('#auth').value;
} if (tel.length < 11 || !isValidMobile(tel)) {
if (code.length != 5 ){ window.$toast.error('Invalid mobile');
window.$toast.error('Invalid code'); return;
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"> <label for="mobile">
{{__('Mobile')}} {{__('Mobile')}}
</label> </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" @endif class="form-control @error('mobile') is-invalid @enderror"
placeholder="{{__('Mobile')}}" placeholder="{{__('Mobile')}}"
value="{{old('mobile',auth('customer')->user()->mobile??null)}}" value="{{old('mobile',auth('customer')->user()->mobile??null)}}"

@ -1,6 +1,6 @@
<section id='LoginBigBg' class="content" <section id='LoginBigBg' class="content"
style="background-image: url('{{asset('upload/images/'.$data->area->name.'.'.$data->part.'.jpg')}}')"> 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 @csrf
<h3> <h3>
{{$subtitle}} {{$subtitle}}
@ -9,7 +9,7 @@
@include('components.err') @include('components.err')
</div> </div>
<div id="login-content"> <div id="login-content">
@if(!config('app.sign.sms')) @if(!config('app.sms.sign'))
<label> <label>
{{__("Email")}} {{__("Email")}}
</label> </label>

@ -2,7 +2,7 @@
> >
<div id="login-container" <div id="login-container"
style="background-image: url('{{asset('upload/images/'.$data->area->name.'.'.$data->part.'.jpg')}}')"> 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 @csrf
<h3> <h3>
{{$subtitle}} {{$subtitle}}
@ -11,7 +11,7 @@
@include('components.err') @include('components.err')
</div> </div>
<div id="login-content"> <div id="login-content">
@if(!config('app.sign.sms')) @if(!config('app.sms.sign'))
<label> <label>
{{__("Email")}} {{__("Email")}}
</label> </label>

Loading…
Cancel
Save