<?php
namespace App\Classes;
use App\Imali\ImaliAccount;
use App\Imali\MerchantAccount;
use App\SmsLog;
use App\Store;
use App\User;
use Illuminate\Support\Facades\Http;
use App\Classes\GenerateToken;
use App\PhoneValidation;
class SendSMSSislog
{
protected $username;
protected $password;
protected string $from;
protected string $key;
protected string $url;
protected string $country_code;
protected string $country_code_final;
protected TransactionGeneration $token;
protected LogSmsManager $logSmsManager;
public function __construct()
{
$this->username = env('SISLOG_USERNAME');
$this->password = env('SISLOG_PASSWORD');
$this->from = env('SISLOG_FROM');
$this->key = "Basic " . env('SISLOG_KEY');
$this->url = env('SISLOG_URL');
$this->country_code = env('SISLOG_COUNTRY_CODE');
$this->country_code_final = env('SISLOG_COUNTRY_CODE_PLUS');
$this->token = new TransactionGeneration();
$this->logSmsManager = new LogSmsManager();
}
public static function sendSmService($country_code, $phone, $username, $user_id, $signature)
{
$token = new GenerateToken();
$smsCode = $token->generatePhoneNumberCode();
//? Chamar servico de mensagens
try {
//code...
//! New
$convert = strtotime(date('Y-m-d H:i:s')) + (60 * 10);
$duration = date("Y-m-d H:i:s", $convert);
//! New
$validate = PhoneValidation::query()->where('phone', $phone)->where('is_Validated', 1)->first();
//$validate = PhoneValidation::query()->where('phone', $phone)->count();
if (!$validate) return response()->json(['message' => 'Número de telefone usado no registo da conta nao foi validado.'], 400);
//$validate->update(['codigo' => $smsCode,'duration' => 5]);
// 842588843
//if($validate->phone === "821464567"){
if (($validate->phone === "845956378") || ($validate->phone === "841234568") || ($validate->phone === "821464567") || ($validate->phone === "870059219") || ($validate->phone === "823180481") || ($validate->phone === "843115033") || ($validate->phone === "870555219") || ($validate->phone === "850369193") || ($validate->phone === "849904773") || ($validate->phone === "873035450") || ($validate->phone === "829423818") || ($validate->phone === "842588843") || ($validate->phone === "841100022")) {
$validate->updated_at = date('Y-m-d H:i:s');
$validate->duration = 5;
$validate->update();
} else {
$validate->expire_at = $duration;
$validate->codigo = $smsCode;
$validate->duration = 5;
$validate->update();
}
$log = new Record();
$log->createLog([
'description' => $phone . ' ' . $smsCode,
'details' => 'Código de Verificação enviado com sucesso!',
'operation' => 'Resend Verification code',
'status' => 'Success',
'user_id' => $user_id
]);
$resp = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json'
])->post('http://172.16.50.2:3004/api/send/sms', [
'phoneNumbers' => [$country_code . $phone],
'message' => "<#> Caro cliente " . $username . ", valide a sua identidade atraves do seguinte codigo : " . $smsCode . "\n" . "Diferente, Como Tu! " . $signature,
]);
if (!$resp->ok()) return response()->json(['message' => 'Servico de SMS indisponivel.', 'success' => false, 'failure' => true]);
return response()->json(['message' => 'Código de Verificação reenviado com sucesso!'], 200);
} catch (\Throwable $th) {
//throw $th;
return response()->json(['message' => 'Servico de SMS indisponivel.', 'error' => $th->getMessage(), 'success' => false, 'failure' => true]);
}
}
public function sendSms($data): \Illuminate\Http\JsonResponse
{
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => $this->key
])->post($this->url, [
'From' => $this->from,
'To' => $data['phone'],
'RegisteredDelivery' => 'true',
'Content' => $data['content'],
'ClientReference' => $data['transaction'],
'Time' => date('Y-m-d H:i:s')
]);
// $smsLog = SmsLog::query()->count();
//
// if ($smsLog == 0) {
// SmsLog::create([
// 'status' => $response->json('Status'),
// 'balance' => $response->json('balance'),
// 'client_reference' => $response->json('ClientReference'),
// 'message_id' => $response->json('MessageId'),
// ]);
// } else {
// $smsLog = SmsLog::find(1);
// $smsLog->update([
// 'status' => $response->json('Status'),
// 'balance' => $response->json('balance'),
// 'client_reference' => $response->json('ClientReference'),
// 'message_id' => $response->json('MessageId'),
// ]);
// }
$this->logSmsManager->logSmsToDatabase($response->json());
return response()->json($response->body());
}
public function sendSMSPurchaseCredelec($data, $phone): \Illuminate\Http\JsonResponse
{
$token = new TransactionGeneration();
$transaction = $token->generateMSID();
$finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $phone);
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE='
])->post('https://lin4.sislog.com/hubtel/v0/messages/send', [
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE=',
'From' => $this->from,
'To' => $this->country_code_final . $finalPhone,
'RegisteredDelivery' => 'true',
'Content' => "i.Mali Credelec " . "\n" . $data . "\n" . trans('slogan'),
'ClientReference' => $transaction,
'Time' => date('Y-m-d H:i:s')
]);
return response()->json($response->body());
}
public function smsVerifyUser($data)
{
$token = new TransactionGeneration();
$transaction = $token->generateMSID();
$finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $data['phone']);
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE='
])->post($this->url, [
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE=',
'From' => $this->from,
'To' => $this->country_code_final . $finalPhone,
'RegisteredDelivery' => 'true',
'Content' => "Codigo de Validacao: " . $data['codigo'] . "\n" . "Diferente, Como Tu!",
'ClientReference' => $transaction,
'Time' => date('Y-m-d H:i:s')
]);
return response()->json($response->body());
}
public function tokenPayment($data): \Illuminate\Http\JsonResponse
{
$token = new TransactionGeneration();
$transaction = $token->generateMSID();
$finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $data['phone']);
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE='
])->post($this->url, [
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE=',
'From' => $this->from,
'To' => $this->country_code_final . $finalPhone,
'RegisteredDelivery' => 'true',
'Content' => trans('token_payment') . $data['token'] . "\n" . trans("slogan"),
'ClientReference' => $transaction,
'Time' => date('Y-m-d H:i:s')
]);
return response()->json($response->body());
}
public function sendMessageToClientePayment($data)
{
$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();
// 'Confirmado dhjeebjk.
// Aos 7/11/2019 as 21h lavantaste 150,00MT no agente 11214 - nome da loja.
// O novo saldo m-pesa é de 8,00MT. i.mali e a cena'
$token = new TransactionGeneration();
$transaction = $token->generateMSID();
$finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $payerUser->phone);
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE='
])->post($this->url, [
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE=',
'From' => 'i.Mali',
'To' => $this->country_code_final . $finalPhone,
'RegisteredDelivery' => 'true',
'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'),
'ClientReference' => $transaction,
'Time' => date('Y-m-d H:i:s')
]);
return response()->json($response->body());
}
public function sendMessageToCustomerPayment($data, $payment)
{
$payerUser = User::query()->where('id', $payment['sender_id'])->first();
$comerciante = Store::query()->where('id', $payment['store_id'])->first();
$token = new TransactionGeneration();
$transaction = $token->generateMSID();
$finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $payerUser->phone);
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE='
])->post($this->url, [
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE=',
'From' => 'i.Mali',
'To' => $this->country_code_final . $finalPhone,
'RegisteredDelivery' => 'true',
'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'),
'ClientReference' => $transaction,
'Time' => date('Y-m-d H:i:s')
]);
return response()->json($response->body());
// $requestBody->setText("Parabens " . strtok($payerUser->name, " ") . "\n" . "Confirmado " . $payment['transaction_id'] . "\n" . "Aos " . $payment['created_at'] . "\n" . "Recebeste " . $data['amount'] .
// "MT da loja " . $comerciante->account_number . "-" . $comerciante->name . "\n" . "i.Mali e o Futuro.");
// 'Confirmado dhjeebjk.
// Aos 7/11/2019 as 21h lavantaste 150,00MT no agente 11214 - nome da loja.
// O novo saldo m-pesa é de 8,00MT. i.mali e a cena'
}
public function sendSMSForUserRecharge($data)
{
$user = ImaliAccount::query()
->join('users', 'users.id', '=', 'imali_accounts.user_id')
->where('imali_accounts.id', $data->imali_account_id)
->first();
$token = new TransactionGeneration();
$transaction = $token->generateMSID();
$finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $user->phone);
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE='
])->post($this->url, [
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE=',
'From' => 'i.Mali',
'To' => $this->country_code_final . $finalPhone,
'RegisteredDelivery' => 'true',
'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'),
'ClientReference' => $transaction,
'Time' => date('Y-m-d H:i:s')
]);
// Creating request body
// $requestBody = new SMSTextualRequest();
// $requestBody->setFrom($this->from);
// $requestBody->setTo(['+258' . $user->phone]);
//// $requestBody->setTo([TO]);
// $requestBody->setText(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'));
//
//// 'Confirmado dhjeebjk.
// Aos 7/11/2019 as 21h lavantaste 150,00MT no agente 11214 - nome da loja.
// O novo saldo m-pesa é de 8,00MT. i.mali e a cena'
return response()->json($response->body());
}
public function sendNotification($data)
{
$token = new TransactionGeneration();
$transaction = $token->generateMSID();
// $payerUser = User::query()->where('id', $data['sender_id'])->first();
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE='
])->post($this->url, [
'Authorization' => $this->key,
'From' => 'i.Mali',
// 'To' => '+258' . $payerUser->phone,
'To' => '+258' . $data['phone'],
'RegisteredDelivery' => 'true',
'Content' => $data['message'] . "\n" . "i.Mali e o Futuro.",
'ClientReference' => $transaction,
'Time' => date('Y-m-d H:i:s')
]);
return response()->json($response->body());
}
public function sendSMSToComerciante($data)
{
$token = new TransactionGeneration();
$transaction = $token->generateMSID();
$payerUser = User::query()->where('id', '=', $data['sender_id'])->first();
// $comerciante = MerchantAccount::query()->where('id', $data['merchant_account_id'])->first();
$store = Store::query()->where('id', '=', $data['store_id'])->first();
// return $store;
// $imali = ImaliAccount::query()->where('user_id', '=', $payerUser->id)->first();
$imali = User::getAccountByUser($payerUser->id, $payerUser->account_id);
// return $store->mobile_phone;
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE='
])->post($this->url, [
'Authorization' => $this->key,
'From' => 'i.Mali',
'To' => $this->country_code_final . $store->mobile_phone,
'RegisteredDelivery' => 'true',
'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.",
'ClientReference' => $transaction,
'Time' => date('Y-m-d H:i:s')
]);
return response()->json($response->body());
}
// public function sendSMSPurchaseCredelec($data, $phone)
// {
// $token = new TransactionGeneration();
// $transaction = $token->generateMSID();
// $finalPhone = preg_replace("/^\+?{$this->country_code}/", '', $phone);
// $response = Http::withHeaders([
// 'Content-Type' => 'application/json',
// 'Accept' => 'application/json',
// 'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE='
// ])->post($this->url, [
// 'Authorization' => $this->key,
// 'From' => $this->from,
// 'To' =>$this->country_code_final.$finalPhone,
// 'RegisteredDelivery' => 'true',
// 'Content' => "i.Mali Credelec " . "\n" . $data . "\n" . trans('slogan'),
// 'ClientReference' => $transaction,
// 'Time' => date('Y-m-d H:i:s')
// ]);
// return response()->json($response->body());
// }
public function notifyAdminTopUpBalance($balance, $account, $service): \Illuminate\Http\JsonResponse
{
$transaction = $this->token->generateMSID();
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => $this->key
])->post($this->url, [
'Authorization' => 'Basic MDAwNDp1Yko1eEhNZGhQWEE=',
'From' => $this->from,
'To' => $account->phone,
'RegisteredDelivery' => 'true',
'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.',
'ClientReference' => $transaction,
'Time' => date('Y-m-d H:i:s')
]);
return response()->json($response->body());
}
}