• File: RefundController.php
  • Full Path: /var/www/imaliapi/app/Http/Controllers/RefundController.php
  • Date Modified: 04/15/2024 8:53 PM
  • File size: 23.32 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\Kyc;
use App\Classes\MerchantKyC;
use App\Classes\PartnerKyc;
use App\Classes\RefundKyc;
use App\Classes\SendSMS;
use App\Classes\TransactionGeneration;
use App\Imali\ImaliAccount;
use App\Imali\ImaliAccountConfig;
use App\Refund;
use App\Store;
use Illuminate\Http\Request;
use DB;

class RefundController extends Controller
{
    protected $trasactionGeneration;
    protected $requestRefund;
    protected $msid;
    protected $token;
    protected $transactionID;
    protected $requestRefundConfirm;

    public function __construct()
    {

        $this->trasactionGeneration = new TransactionGeneration();
        $this->msid = new TransactionGeneration();

    }

    public function getRefundCustomer(Request $request)
    {
        $refuns = Refund::query()
            ->where('merchant_id', $request->user()->id)
            ->join('users', 'users.id', '=', 'refunds.imali_user_id')
            ->join('stores', 'stores.id', '=', 'refunds.store_id')
            ->select('refunds.*', 'users.name as client_name', 'stores.name as store_name', 'stores.account_number as store_account_number')
            ->paginate(20);

        return response()->json(['data' => $refuns], 200);
    }

    public function refundCustomerNew(Request $request)
    {
        $this->validate($request, [
            'amount' => 'required',
            'description' => 'required',
            'customerAccountNumber' => 'required',
            'storeAccountNumber' => 'required',
            'paymentTransaction' => 'required',
            'terminalID' => 'required',
            'terminalChannel' => 'required',
            'terminalCompanyName' => 'required',
        ], [
            'amount.required' => 'O Campo transaction é de carácter Obrigatório',
            'description.required' => 'O campo description é obrigatório',
            'account_number.required' => 'O campo account_number é obrigatório',
            'merchant_id.required' => 'O campo merchant_id é obrigatório',
            'payment_transaction.required' => 'O campo payment_transaction é obrigatório',
            'store_account_number.required' => 'O campo store_account_number é obrigatório',
            'user_client_id.required' => 'O campo imali_user_id é obrigatório',
        ]);

        $kyc = new RefundKyc();
        $kycR = $kyc->checkRefundPayment($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);
                });

                return response()->json(['message' => trans('otp_sent'), 'transaction' => $this->transactionID]);
            } catch (\Exception $exception) {
                return response()->json(['message' => $exception], 400);
            }

        }
    }

    public function refundCustomerNewStore(Request $request)
    {
        $request->request->add(['storeAccountNumber' => $request->user()->account_number]);

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

        $kyc = new RefundKyc();
        $kycR = $kyc->checkRefundPaymentStore($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];
                    $data = ['phone' => '+258' . $loja->mobile_phone, '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);
            }

        }
    }

    public function refundConfirmation(Request $request)
    {

        $this->validate($request, [
            'transaction_id' => 'required',
            'paymentID' => 'required',
            'token_otp' => 'required'
        ]);

        $kyc = new RefundKyc();
        $kycCheck = $kyc->checkConfirmRefund($request);

        if ($kycCheck) {
            return $kycCheck;
        } else {

            $this->requestRefundConfirm = $request;

            try {
                DB::transaction(function () {

                    $refund = DB::table('refunds')
                        ->where('transaction', $this->requestRefundConfirm->transaction_id)
                        ->first();

                    DB::table('payments')->insert([
                        'transaction_id' => $refund->transaction,
                        'sender_id' => $refund->imali_user_id,
                        'store_id' => $refund->store_id,
                        'received_points' => 0,
                        'amount' => $refund->amount,
                        'amount_credited' => $refund->amount,
                        'comissao' => $refund->fee,
                        'description' => $refund->description,
                        'used_points' => 0,
                        'estado' => 'pago',
//                    'client_id' => $this->requestRefundConfirm->user()->id,
                        'payment_type' => 'reembolso',
                        'estado_color' => '#388E3C',
                        'terminalCompanyName' => $refund->terminalCompanyName,
                        'terminalChannel' => $refund->terminalChannel,
                        'terminalID' => $refund->terminalID,
                        'created_at' => now(),
                        'updated_at' => now()
                    ]);

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

                    $loja = DB::table('stores')->where('id', '=', $refund->store_id)->first();

                    $imaliConfig = ImaliAccountConfig::find($merchant->kyc_config_id);

                    $payment = DB::table('payments')->where('id', $refund->payment_id)->first();

                    $user = DB::table('users')->where('id', $refund->imali_user_id)->first();

                    DB::table('imali_accounts')->where('user_id', $refund->imali_user_id)->increment('balance', $refund->amount);
//                    DB::table('merchant_accounts')->where('id', $refund->merchant_id)->increment('balance', $refund->amount + $imaliConfig->taxa_refund_mechant);

                    $taxaDesconto = $refund->amount * ($imaliConfig->taxa_refund_mechant) / 100;

                    $valorMin = $imaliConfig->min_amount;
//                    $valorMin = $loja->min_amount;
//                    $valorMax = $loja->max_amount;
                    $valorMax = $imaliConfig->max_amount;

                    if ($taxaDesconto < $valorMin) {
                        $taxaDesconto = $valorMin;
                    }
                    if ($taxaDesconto > $valorMax) {
                        $taxaDesconto = $valorMax;
                    }


//                    DB::table('stores')->where('id', $refund->store_id)->decrement('balance', $refund->amount + $imaliConfig->taxa_refund_mechant);
                    DB::table('stores')->where('id', $refund->store_id)->decrement('balance', $refund->amount + $taxaDesconto);


                    $tran = new TransactionGeneration();

                    DB::table('profits')->insert([
                        'payer_id' => $user->id,
                        'payer_account' => $loja->account_number,
                        'amount' => $refund->amount,
                        'amount_credited' => $refund->amount + $imaliConfig->taxa_refund_mechant,
                        'comissao' => $imaliConfig->taxa_refund_mechant,
                        'profit_id' => $tran->generateTransaction(),
                        'payment_id' => $payment->id,
                        'profit_payer_id' => $payment->store_id,
                        'created_at' => now(),
                        'updated_at' => now()
                    ]);

                    DB::table('refunds')
                        ->where('transaction', $refund->transaction)
                        ->update(['estado' => 'successo', 'updated_at' => now()]);

                    DB::table('payments')
                        ->where('transaction_id', $this->requestRefundConfirm->paymentID)
                        ->update(['refund_confirmation' => true, 'updated_at' => now()]);

                    $notification = array(
                        'icon' => 'ic_imali_logo_verde_01',
//                            'icon' => 'ic_i_mali_cover',
                        'title' => 'Recebeste ' . $refund->amount . ' MT',
                        'body' => 'Parabéns, ' . ' recebeste reembolso ' . $refund->amount . ' MT ' . ' da loja ' . $loja->name,
                        'click_action' => 'com.imali.payapp.payment_PAGAR_NOTIFICATION',
//                            'color' => '#008577'
                        'color' => '#ffffff'
                    );


                    $data = array(
                        'transaction' => $refund->transaction,
                        'loja' => $loja->name,
                        'loja_account' => $loja->account_number,
                        'pontos' => $payment->received_points,
                        'pontos_usados' => $payment->used_points,
                        'amount' => (double)$refund->amount,
                        'amount_debited' => (double)$refund->amount,
                        'account_number' => (int)$refund->account_number,
                        'phone' => $user->phone,
                        'descricao' => $refund->description,
                        'data' => $refund->created_at,
                        'estado' => $refund->estado,
                        'comissao' => (double)$refund->fee,
			'route' => 'PAGAR_NOTIFICATION',
                        'terminal' => 'firebase'
                    );

//                    $sms = new SendSMS();
//
//                    $resultPayment = json_decode(json_encode($payment), true);
//                    $result = json_decode(json_encode($refund), true);
//
////                    Enviar Mensagem ao cliente
//                    $sms->sendMessageToCustomerPayment($result, $resultPayment);


                    $this->pushNotifification($user->firebase_token, $notification, $data);

                });

                return response()->json(['message' => trans('refund_done')], 200);

            } catch (\Exception $exception) {
                return response()->json(['message' => $exception->getMessage()],400);
            }

        }
    }

    public function refundCustomer(Request $request)
    {
        $this->validate($request, [
//            'transaction' => 'required',
            'amount' => 'required',
            'description' => 'required',
            'account_number' => 'required',
            'store_id' => 'required',
            'password' => 'required'
        ], [
            'transaction.required' => 'O Campo transaction é de carácter Obrigatório',
            'amount.required' => 'O Campo transaction é de carácter Obrigatório',
            'description.required' => 'O campo description é obrigatório',
            'account_number.required' => 'O campo account_number é obrigatório',
            'merchant_id.required' => 'O campo merchant_id é obrigatório',
            'imali_user_id.required' => 'O campo imali_user_id é obrigatório',
            'store_id.required' => 'O campo imali_user_id é obrigatório',
            'user_client_id.required' => 'O campo imali_user_id é obrigatório',
        ]);

        $kyc = new MerchantKyC();
        $kycCheck = $kyc->checkMerchant($request);

        if ($kycCheck) {
            return $kycCheck;
        } else {
            $imaliUser = ImaliAccount::query()
                ->join('users', 'users.id', '=', 'imali_accounts.user_id')
                ->where('account_number', $request->account_number)
                ->select('users.*')
                ->first();

            $imaliConfig = ImaliAccountConfig::find($request->user()->kyc_config_id);
            $loja = Store::query()->where('id', '=', $request->store_id)->first();

            $refund = Refund::create([
                'transaction' => $this->trasactionGeneration->generateTransaction(),
                'amount' => $request->amount,
                'fee' => $imaliConfig->taxa_refund_mechant,
                'amount_debited' => $request->amount + $imaliConfig->taxa_refund_mechant,
                'account_number' => $request->account_number,
                'description' => $request->description,
                'store_id' => $request->store_id,
                'imali_user_id' => $imaliUser->id,
                'estado' => 'Reembolso',
                'merchant_id' => $request->user()->id,
                'terminalCompanyName' => 'Paytek',
                'terminalChannel' => 'Dashboard',
                'terminalID' => '2021001'
            ]);

            $payment = Payment::create([
                'transaction_id' => $refund->transaction,
                'sender_id' => $refund->imali_user_id,
                'store_id' => $refund->store_id,
                'received_points' => 0,
                'amount' => $refund->amount,
                'amount_credited' => $refund->amount,
                'comissao' => $refund->fee,
                'description' => $refund->description,
                'used_points' => 0,
                'estado' => 'pago',
                'payment_type' => 'reembolso',
                'estado_color' => '#388E3C',
                'created_at' => now(),
                'updated_at' => now()
            ]);

            DB::table('imali_accounts')->where('user_id', $refund->imali_user_id)->increment('balance', $refund->amount);
            DB::table('merchant_accounts')->where('id', $refund->merchant_id)->increment('balance', $refund->amount);

            $notification = array(
                'icon' => 'ic_imali_logo_verde_01',
//                            'icon' => 'ic_i_mali_cover',
                'title' => 'Recebeste ' . $request->amount . ' MT',
                'body' => 'Parabéns, ' . ' recebeste reembolso de ' . $request->amount . ' MT ' . ' da loja ' . $loja->name,
                'click_action' => 'com.imali.payapp.payment_PAGAR_NOTIFICATION',
//                            'color' => '#008577'
                'color' => '#ffffff'
            );


            $data = array(
                'transaction' => $refund->transaction,
                'loja' => $loja->name,
                'loja_account' => $loja->account_number,
                'pontos' => $payment->received_points,
                'pontos_usados' => $payment->used_points,
                'amount' => (double)$refund->amount,
                'amount_debited' => (double)$refund->amount,
                'account_number' => (int)$refund->account_number,
                'phone' => $request->user()->phone,
                'descricao' => $refund->description,
                'data' => $refund->created_at,
                'estado' => $refund->estado,
                'comissao' => (double)$refund->comissao,
		'route' => 'PAGAR_NOTIFICATION',
                'terminal' => 'firebase'
            );

            $this->pushNotifification($imaliUser->firebase_token, $notification, $data);

            return response()->json(['message' => 'Reembolso feito com Sucesso!'], 200);
        }
    }

    public function pushNotifification($token, $notification = array(), $data = array())
    {
        $apiKey = 'AAAA8zVzEPQ:APA91bHl_DXB6UGb_6gZlmFnaLTQoANtX_OBjvl3nOy2bSlnFhxedvk6EhGj7cZoIvmlbKeCnqGxXbuyMH_rEPuhRXvuitXzo6Pfl2TMXLar1PlifXqEhYq6tS55UMrY2Kffzj-P_UH-';
        $fields = array('to' => $token, 'notification' => $notification, 'data' => $data);
        $headers = array('Authorization: key=' . $apiKey, 'Content-Type: application/json');
        $url = 'https://fcm.googleapis.com/fcm/send';

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($curl);
        curl_close($curl);

        return json_decode($result, true);

    }
}