From d944d317a42deee09032b9c5696a16de5eac8d34 Mon Sep 17 00:00:00 2001 From: A1Gard Date: Sun, 8 Sep 2024 07:48:19 +0330 Subject: [PATCH] added sms auth system --- .env.example | 9 ++- app/Helpers/Helper.php | 69 +++++++++++++++- .../Controllers/Admin/InvoiceController.php | 16 ++++ app/Http/Controllers/ClientController.php | 14 ++++ app/Http/Controllers/CustomerController.php | 2 +- app/Models/Invoice.php | 14 ++++ config/app.php | 11 ++- database/seeders/SettingSeeder.php | 28 ++++++- resources/js/client-custom/login.js | 78 +++++++++++-------- .../AvisaCustomer/AvisaCustomer.blade.php | 2 +- .../login/LoginBigBg/LoginBigBg.blade.php | 4 +- .../LoginPatternBg/LoginPatternBg.blade.php | 4 +- 12 files changed, 202 insertions(+), 49 deletions(-) diff --git a/.env.example b/.env.example index 647cb09..556504a 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php index b702583..48babdb 100644 --- a/app/Helpers/Helper.php +++ b/app/Helpers/Helper.php @@ -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; + +} diff --git a/app/Http/Controllers/Admin/InvoiceController.php b/app/Http/Controllers/Admin/InvoiceController.php index 051b095..bdf0790 100644 --- a/app/Http/Controllers/Admin/InvoiceController.php +++ b/app/Http/Controllers/Admin/InvoiceController.php @@ -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; diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 182991e..3aa4155 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -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(); diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index 8df6261..86408d6 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -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); } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index b12b79f..3add0a8 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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){ diff --git a/config/app.php b/config/app.php index 728329c..96f1a66 100644 --- a/config/app.php +++ b/config/app.php @@ -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',''), ], /* |-------------------------------------------------------------------------- diff --git a/database/seeders/SettingSeeder.php b/database/seeders/SettingSeeder.php index bcdeb80..1a10ba8 100644 --- a/database/seeders/SettingSeeder.php +++ b/database/seeders/SettingSeeder.php @@ -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(); } } diff --git a/resources/js/client-custom/login.js b/resources/js/client-custom/login.js index e8e3949..1cc702d 100644 --- a/resources/js/client-custom/login.js +++ b/resources/js/client-custom/login.js @@ -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); } }); }); diff --git a/resources/views/segments/customer/AvisaCustomer/AvisaCustomer.blade.php b/resources/views/segments/customer/AvisaCustomer/AvisaCustomer.blade.php index 2aa08aa..45665a2 100644 --- a/resources/views/segments/customer/AvisaCustomer/AvisaCustomer.blade.php +++ b/resources/views/segments/customer/AvisaCustomer/AvisaCustomer.blade.php @@ -272,7 +272,7 @@ - -
+ @csrf

{{$subtitle}} @@ -9,7 +9,7 @@ @include('components.err')
- @if(!config('app.sign.sms')) + @if(!config('app.sms.sign')) diff --git a/resources/views/segments/login/LoginPatternBg/LoginPatternBg.blade.php b/resources/views/segments/login/LoginPatternBg/LoginPatternBg.blade.php index 16f6091..44f78d7 100644 --- a/resources/views/segments/login/LoginPatternBg/LoginPatternBg.blade.php +++ b/resources/views/segments/login/LoginPatternBg/LoginPatternBg.blade.php @@ -2,7 +2,7 @@ >
- + @csrf

{{$subtitle}} @@ -11,7 +11,7 @@ @include('components.err')

- @if(!config('app.sign.sms')) + @if(!config('app.sms.sign'))