• File: SmsManager.php
  • Full Path: /var/www/imalipartnersapi/app/Classes/SmsManager.php
  • Date Modified: 02/27/2023 5:43 PM
  • File size: 6.59 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" => "i.Mali Credelec " . "\n" . $data . "\n" . trans('slogan'),
            '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!",
            '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,
           // 'phone' => $this->country_code_final . $data['phone'],
            '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') . " " . $payment['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
    {
        $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') . $user->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" . "i.Mali e o Futuro.",
            'phone' => $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" . "i.Mali e o Futuro.",
            'phone' => $store->mobile_phone,
            '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' => $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' => $account->phone,
            'transaction' => $this->token->generateMSID()
        ];

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