• File: SmsManager.php
  • Full Path: /var/www/imaliapitest/app/Classes/SmsManager.php
  • Date Modified: 05/19/2025 4:29 PM
  • File size: 8.47 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace App\Classes;

use App\Imali\ImaliAccount;
use App\Store;
use App\User;
use Illuminate\Support\Facades\Http;

class SmsManager
{

    protected string $country_code;
    protected string $country_code_final;
    protected TransactionGeneration $token;
    protected SendSMSSislog $sislog;

    public function __construct()
    {
        $this->country_code = '258';
        $this->country_code_final = '+258';
        $this->token = new TransactionGeneration();
        $this->sislog = new SendSMSSislog();
    }

    public function sendSMSPurchaseCredelec($data, $phone): void
    {
        $finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $phone);

        $data = [
            // "content" => "iMali Credelec " . "\n" . $data . "\n" . trans('slogan'),
            "content" => $data,
            'phone' => $this->country_code_final . $finalPhone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }

    public function smsVerifyUser($data): void
    {
        $finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $data['phone']);

        $data = [
            //"content" => "Codigo de Validacao: " . $data['codigo'] . "\n" . "Diferente, Como Tu!",
            "content" => "<#> Codigo de Validacao: " . $data['codigo'] . "\n" . "Diferente, Como Tu! " . $data['signature'],
            'phone' => $this->country_code_final . $finalPhone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }

    public function smsVerifyUserDevice($data): void
    {
        $finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $data['phone']);

        $data = [
            // "content" => "Caro cliente ". $data['username']. ", valide a sua identidade atraves do seguinte codigo: " . $data['codigo'] . "\n" . "Diferente, Como Tu!",
            "content" => "Caro cliente " . $data['username'] . ", estas a tentar fazer login em outro dispositivo. Valide a sua identidade atraves do seguinte codigo: " . $data['codigo'],
            'phone' => $this->country_code_final . $finalPhone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }

    public function tokenPayment($data): void
    {
        $finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $data['phone']);

        $data = [
            "content" => trans('token_payment') . $data['token'] . "\n" . trans("slogan"),
            'phone' => $this->country_code_final . $finalPhone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }

    public function sendMessageToClientePayment($data): void
    {
        $payerUser = User::query()->where('id', $data['sender_id'])->first();
        $comerciante = Store::query()->where('id', $data['store_id'])->first();
        $imali = ImaliAccount::query()->where('user_id', $payerUser->id)->first();

        $finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $payerUser->phone);

        $data = [
            "content" => trans("congrats") . " " . strtok($payerUser->name, " ") . "\n" . trans('confirmed') . " " . $data['transaction_id'] . "\n" . trans('at') . " " . $data['created_at'] . "\n" . trans('paid') . " " . $data['amount'] .
                "MT" . " " . trans('merchant') . " " . $comerciante->account_number . "-" . $comerciante->name . "\n" . trans('slogan'),
            'phone' => $this->country_code_final . $finalPhone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }

    public function sendMessageToCustomerPayment($data, $payment): void
    {
        $payerUser = User::query()->where('id', $payment['sender_id'])->first();
        $comerciante = Store::query()->where('id', $payment['store_id'])->first();

        $finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $payerUser->phone);

        $data = [
            "content" => trans("congrats") . " " . strtok($payerUser->name, " ") . "\n" . trans('confirmed') . " " . $payment['transaction_id'] . "\n" . trans('at') . " " . $payment['created_at'] . "\n" . trans('received') . " " . $data['amount'] .
                "MT" . " " . trans('merchant') . " " . $comerciante->account_number . "-" . $comerciante->name . "\n" . trans('slogan'),
            'phone' => $this->country_code_final . $finalPhone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }

    public function sendSMSForUserRecharge($data): void
    {

        if (User::is_subAccount($data['account_number'])) {
            $account = User::getAccount($data['account_number']);
            $user = User::getUserDetails($account->user_id);
        } else {
            $account = User::getAccount($data['account_number']);
            $user = User::getUserDetails($account->user_id);
        }

        //$user = ImaliAccount::query()
        //    ->join('users', 'users.id', '=', 'imali_accounts.user_id')
        //    ->where('imali_accounts.id', $data->imali_account_id)
        //    ->first();

        $finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $user->phone);

        $data = [
            "content" => trans('congrats') . " " . $user->name . "!" . "\n" . trans('confirmed') . " " . $data['transaction_id'] . "." . "\n" . trans('at') . " " . $data['created_at'] . "\n" . trans('recharged_with') . " " . $data['amount'] .
                "MT" . " " . trans('in_account') . " " . $account->account_number . "." . "\n" . trans('slogan'),
            'phone' => $this->country_code_final . $finalPhone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }

    public function sendNotification($data)
    {

        $data = [
            "content" => $data['message'] . "\n" . "iMali, diferente como tu!",
            'phone' => $this->country_code_final . $data['phone'],
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }

    public function sendSMSToComerciante($data): void
    {
        $payerUser = User::query()->where('id', '=', $data['sender_id'])->first();
        $store = Store::query()->where('id', '=', $data['store_id'])->first();
        $imali = ImaliAccount::query()->where('user_id', '=', $payerUser->id)->first();

        $data = [
            "content" => "Parabens " . $store->name . "\n" . "Confirmado " . $data['transaction_id'] . "\n" . "Aos " . $data['created_at'] . "\n" . "Recebeste " . $data['amount'] .
                "MT do " . strtok($payerUser->name, " ") . "-" . $imali->account_number . "\n" . "iMali, diferente como tu!",
            'phone' => $this->country_code_final . $store->mobile_phone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }

    public function sendSMSToClient($data): void
    {
        $payerUser = User::query()
            ->leftJoin('user_documents', 'user_documents.user_id', 'users.id')
            ->where('users.id', '=', $data['user_id'])->first();

        $data = [
            "content" => "Caro cliente " . $payerUser->name . " o teu " . $payerUser->document_type . " expirou. A tua conta encontra-se limitada a 300 MT por transacao.",
            'phone' => $this->country_code_final . $payerUser->phone,
            // 'phone' => '842592349',
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }


    public function notifyAdminTopUpBalance($balance, $account, $service): void
    {
        $data = [
            "content" => 'ALERTA iMali: saldo topup abaixo de ' . number_format($service->min_amount, 2, ",", ".") . "MT" . ' . Saldo actual: ' . number_format($balance, 2, ",", ".") . "MT" . ' Carregue a conta Topup.',
            'phone' => $this->country_code_final . $account->phone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }
    public function notifyAdminSMSBalance($balance, $account, $service): void
    {
        $data = [
            "content" => 'ALERTA iMali: saldo Sislog de sms abaixo de ' . number_format($service->min_amount, 2, ",", ".") . "MT" . ' . Saldo actual: ' . number_format($balance, 2, ",", ".") . "MT" . ' Carregue a conta Sislog SMS.',
            'phone' => $this->country_code_final . $account->phone,
            'transaction' => $this->token->generateMSID()
        ];

        $this->sislog->sendSms($data);
    }
}