• File: UssdController.php
  • Full Path: /var/www/imaliapi/app/Http/Controllers/UssdController.php
  • Date Modified: 04/15/2024 8:53 PM
  • File size: 7.1 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace App\Http\Controllers;

use App\Bank\Payment;
use App\Classes\GenerateToken;
use App\Classes\RefundKyc;
use App\Classes\SendSMS;
use App\Imali\ImaliAccount;
use App\Refund;
use App\Store;
use Illuminate\Http\Request;

class UssdController extends Controller
{
//    public function getBalance(Request $request)
//    {
//        $imali = ImaliAccount::query()
//            ->join('users', 'users.id', '=', 'imali_accounts.user_id')
//            ->where('users.phone', $request->phone)
//            ->first();
//
//        if ($imali) {
//            return response()->json($imali);
//        } else {
//            return response()->json(['message' => 'Conta invalida']);
//        }
//    }

    public function getBalance($phone)
    {
        $imali = ImaliAccount::query()
            ->join('users', 'users.id', '=', 'imali_accounts.user_id')
            ->where('users.phone', '=', $phone)
            ->first();

        if ($imali) {
            return response()->json($imali);
        } else {
            return response()->json(['message' => 'Conta invalida']);
        }
    }


    public function getTransactions($phone)
    {

        $store = Store::query()->where('mobile_phone', '=', $phone)->first();

        if ($store) {
            $payment = Payment::query()
                ->join('stores', 'stores.id', '=', 'payments.store_id')
                ->join('users', 'users.id', '=', 'payments.sender_id')
                ->join('imali_accounts', 'imali_accounts.user_id', '=', 'payments.sender_id')
//            ->leftJoin('store_configs', 'store_configs.store_id', '=', 'payments.store_id')
                ->where('stores.mobile_phone', '=', $phone)
//            ->whereDate('payments.created_at', '=', date('Y-m-d'))
                ->orderByDesc('payments.created_at')
//            ->select('payments.*', 'imali_accounts.account_number as imali_account', 'users.name as username', 'store_configs.use_refund', 'store_configs.use_period')
                ->select('payments.*', 'imali_accounts.account_number as imali_account', 'users.name as username')
                ->take(5)
                ->get();

            $payment->makeHidden(['id', 'sender_id', 'store_id', 'client_id', 'category_id', 'updated_at', 'estado_color']);

            return response()->json(['data' => $payment]);

        } else {

            return response()->json(['message' => 'Esta conta ao tem loja associada.'],400);
        }


    }

    public function transferMoney(Request $request)
    {

        $this->validate($request, [
            'amount' => 'required',
            'description' => 'required',
            'customerAccountNumber' => 'required',
            'phone' => 'required',
            'transactionID' => 'required',
            'terminalID' => 'required',
            'terminalChannel' => 'required',
            'terminalCompanyName' => 'required',
        ], [
            'amount.required' => 'O Campo transaction é de carácter Obrigatório',
            'description.required' => 'O campo description é obrigatório',
            'customerAccountNumber.required' => 'O campo customerAccountNumber é obrigatório',
            'phone.required' => 'O campo phone é obrigatório',
            'transactionID.required' => 'O campo transactionID é obrigatório',
            'terminalID.required' => 'O campo payment_transaction é obrigatório',
            'terminalChannel.required' => 'O campo store_account_number é obrigatório',
            'terminalCompanyName.required' => 'O campo imali_user_id é obrigatório',
        ]);


        $kyc = new RefundKyc();
        $kycR = $kyc->checkRefundPaymentUSSD($request);

        if ($kycR) {
            return $kycR;
        } else {
            $this->requestRefund = $request;
            try {
                DB::transaction(function () {

                    $imaliUser = DB::table('imali_accounts')
                        ->join('users', 'users.id', '=', 'imali_accounts.user_id')
                        ->where('account_number', $this->requestRefund->customerAccountNumber)
                        ->select('users.*')
                        ->first();

                    $merchant = DB::table('merchant_accounts')
                        ->join('stores', 'stores.merchant_account_id', '=', 'merchant_accounts.id')
                        ->where('stores.account_number', $this->requestRefund->storeAccountNumber)
                        ->select('merchant_accounts.*', 'stores.balance as store_balance', 'stores.id as store_id')
                        ->first();

                    $imaliConfig = DB::table('imali_account_configs')
                        ->where('id', $merchant->kyc_config_id)
                        ->first();

                    $loja = Store::query()->where('account_number', '=', $this->requestRefund->storeAccountNumber)->first();

                    $payment = Payment::query()
                        ->where('transaction_id', $this->requestRefund->paymentTransaction)
                        ->first();

                    $generation = new GenerateToken();

                    $token = $generation->generatePhoneNumberCode();

                    $this->transactionID = $this->msid->generateTransaction();

                    Refund::create([
                        'transaction' => $this->transactionID,
                        'amount' => $this->requestRefund->amount,
                        'fee' => $imaliConfig->taxa_refund_mechant,
                        'amount_debited' => $this->requestRefund->amount + $imaliConfig->taxa_refund_mechant,
                        'account_number' => $this->requestRefund->customerAccountNumber,
                        'description' => $this->requestRefund->description,
                        'store_id' => $merchant->store_id,
                        'imali_user_id' => $imaliUser->id,
                        'payment_id' => $payment->id,
                        'estado' => 'pendente',
                        'token' => $token,
                        'merchant_id' => $merchant->id,
                        'terminalCompanyName' => $this->requestRefund->terminalCompanyName,
                        'terminalChannel' => $this->requestRefund->terminalChannel,
                        'terminalID' => $this->requestRefund->terminalID
                    ]);

                    $data = ['phone' => '+258' . $merchant->phone_number, 'token' => $token];

                    $this->token = $token;
                    $sms = new SendSMS();
                    $sms->tokenPayment($data);

                    info("Ocorreu com Sucesso");
                });
//                return response()->json(['message' => 'OTP enviado com Sucesso', 'token' => $this->token, 'transaction' => $this->transactionID]);
                return response()->json(['message' => 'OTP enviado com Sucesso', 'transaction' => $this->transactionID]);
            } catch (\Exception $exception) {

                return response()->json(['message' => $exception], 400);
            }

        }
    }
}