• File: TopUpTestController.php
  • Full Path: /var/www/imalipartnersapi/app/Http/Controllers/TopUpTestController.php
  • Date Modified: 02/07/2023 8:01 PM
  • File size: 30.32 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace App\Http\Controllers;

use App\Classes\Kyc;
use App\Classes\Record;
use App\Classes\TransactionGeneration;
use App\GeneralAdvice;
use App\Imali\ImaliAccount;
use App\PurchaseVoucher;
use Illuminate\Http\Request;
use SoapClient;
use SoapFault;
use DB;

class TopUpTestController extends Controller
{
    protected $username;
    protected $password;
    protected $topUpUrl;
    protected $client;
    protected $msid;
    protected $imaliUser;
    protected $request;

    public function __construct()
    {

        //TESTE
        $this->username = "TEST_PAYTEK";
        $this->password = "TESTpassPAYtek";
        $this->topUpUrl = 'https://topupretail.com:18880/Service.asmx?wsdl';


//        PRODUCTION
//        $this->username = "PAYTEK_PROD";
//        $this->password = "fs.S}nf4:IGXT|R";
//        $this->topUpUrl = 'https://topupretail.com:18873/Service.asmx?wsdl';

//        $this->request = $request;


        $this->client = new SoapClient($this->topUpUrl, ['trace' => true]);
        $this->msid = new TransactionGeneration();
    }

    public function checkClientStartTimes($smartCard)
    {


        $transaction = $this->msid->generateMSID();
        $directRecharge = array(
            'req' => array(
                'authCred' => array(
                    'opName' => $this->username,
                    'password' => $this->password
                ),
                'terminalID' => 'i.Mali',
                'terminalMsgID' => '2020010700060',
                'msgID' => $transaction,
                'test' => true,
//                'smartCardnumber' => '01012301234'
                'smartCardnumber' => $smartCard
            )
        );

        $data = json_decode(json_encode($directRecharge), true);

        $response = $this->client->StarConfirmCustomer($data);

        if ($response->StarConfirmCustomerResult->hasFault == true) {
            //        return response()->json($response->StarConfirmCustomerResult);
            return response()->json([
                'message' => $response->StarConfirmCustomerResult->fault->POR_operatorMsg
            ], 400);
        } else {

            if (is_array($response->StarConfirmCustomerResult->availProducts->StarProduct)) {

                return response()->json([
                    'account' => $response->StarConfirmCustomerResult,
                    'data' => $response->StarConfirmCustomerResult->availProducts->StarProduct
                ]);

            } else {

                return response()->json([
                    'account' => $response->StarConfirmCustomerResult,
                    'data' => array($response->StarConfirmCustomerResult->availProducts->StarProduct)
                ]);
            }

        }


    }

    public function payStartTimesPackage(Request $request)
    {
        $this->validate($request, [
            'productDesc' => 'required',
            'productNumber' => 'required',
            'purchaseValue' => 'required',
            'smartCardnumber' => 'required'
        ]);
        $kyc = new Kyc();
        $result = $kyc->checkUserBuyStartimes($request);

        if ($result) {

            $log = new Record();
            $log->createLog([
                'description' => 'Nr Cliente: ' . $request->smartCardnumber . 'code: ' . $request->productNumber, 'price: ' . $request->purchaseValue,
                'details' => $result,
                'operation' => 'Buy StarTimes',
                'status' => 'Error',
                'origin_ip' => $request->ip(),
                'origin_request' => $request->url(),
                'user_id' => $request->user()->id
            ]);

            return $result;
        } else {

            $transaction = $this->msid->generateMSID();
            $directRecharge = array(
                'req' => array(
                    'authCred' => array(
                        'opName' => $this->username,
                        'password' => $this->password
                    ),
                    'terminalID' => 'i.Mali',
                    'terminalMsgID' => '2020010700060',
                    'msgID' => $transaction,
                    'test' => true,
                    'smartCardnumber' => '01012301234',
                    'productDesc' => $request->productDesc,
                    'productNumber' => $request->productNumber,
                    'purchaseValue' => $request->purchaseValue,
                    'receiptFormat' => 'POR_FORMATED_50',
                    'terminalChannel' => 'iMali_APP',
                    'terminalCompanyName' => 'Paytek',
                    'terminalOperator' => 'iMali',
                    'terminalLocation' => 'Maputo'
                )
            );
            $data = json_decode(json_encode($directRecharge), true);

            $response = $this->client->StarPayment($data);

            if ($response->StarPaymentResult->hasFault == false) {
                $result = $response->StarPaymentResult;

                $imaliUser = ImaliAccount::query()->where('user_id', $request->user()->id)->first();

                $totalAmount = $request->purchaseValue + $imaliUser->taxa;

                DB::table('imali_accounts')->where('user_id', $request->user()->id)->decrement('balance', $totalAmount);

                $recarga = PurchaseVoucher::create([
                    'transaction' => $result->transactionId,
                    'vouchername' => 'StarTimes-' . $request->productDesc,
                    'vouchercode' => $request->productNumber,
                    'vouchervalue' => $request->purchaseValue,
                    'price' => $request->purchaseValue,
                    'datepurchased' => $result->respDateTime,
                    'reqterminalMsgID' => $result->reqterminalMsgID,
                    'reqterminalID' => $result->reqterminalID,
                    'reqMsgID' => $result->reqMsgID,
                    'respDateTime' => $result->respDateTime,
                    'receiptFormat' => $result->receiptFormat,
                    'receipt' => $result->receipt,
                    'smsreceipt' => $result->smsreceipt,
                    'user_id' => $request->user()->id,
                    'voucher_list_id' => null,
                    'type' => 'Tv',
                    'smartCard' => $request->smartCardnumber
                ]);

                $log = new Record();
                $log->createLog([
                    'description' => 'Transaction' . $recarga->reqMsgID . ' Nr Cliente: ' . $request->smartCardNumber . 'code: ' . $request->productNumber, 'price: ' . $request->purchaseValue,
                    'details' => "Pacote Activado Com Sucesso",
                    'operation' => 'Buy StarTimes',
                    'status' => 'Success',
                    'origin_ip' => $request->ip(),
                    'origin_request' => $request->url(),
                    'user_id' => $request->user()->id
                ]);

                $notification = array(
                    'icon' => 'ic_i_mali_cover',
                    'title' => 'i.Mali recargas',
                    'body' => 'Parabéns, ' . ' comprou recarga ' . $recarga->vouchername . '.' . ' i.Mali é o Futuro',
                    'click_action' => 'com.imali.payapp.payment_RECARGA_NOTIFICATION',
                    'color' => '#ffffff'
                );

                $data = array(
                    'reqMsgID' => $recarga->reqMsgID,
                    'vouchername' => $recarga->vouchername,
                    'vouchercode' => (double)$recarga->vouchercode,
                    'data' => $recarga->created_at,
                    'price' => $recarga->price,
                    'vouchervalue' => $recarga->vouchervalue,
                    'sms' => $recarga->receipt,
                    'terminal' => 'firebase'
                );
                $this->pushNotifification($request->user()->firebase_token, $notification, $data);

                return response()->json([
                    'payment' => $response,
                    'message' => 'Pacote activado com Sucesso',
                    'reqMsgID' => $recarga->reqMsgID,
                    'sms' => $recarga->smsreceipt
                ], 200);
            } else {

                if ($response->StarPaymentResult->fault->mustALR == true) {

                    $imaliUser = ImaliAccount::query()->where('user_id', $request->user()->id)->first();
                    $totalAmount = $request->amount + $imaliUser->taxa;
                    DB::table('imali_accounts')->where('user_id', $request->user()->id)->increment('captive_balance', $totalAmount);

                    $g = GeneralAdvice::create([
                        'reqMsgID' => $response->StarPaymentResult->reqMsgID,
                        'reqterminalID' => $response->StarPaymentResult->reqterminalID,
                        'receiptFormat' => $response->StarPaymentResult->receiptFormat,
                        'respDateTime' => $response->StarPaymentResult->respDateTime,
                        'faultNumber' => $response->StarPaymentResult->fault->faultnumber,
                        'message' => $response->StarPaymentResult->fault->POR_operatorMsg,
                        'user_id' => $request->user()->id,
                        'type' => 'direct',
                        'amount' => $request->purchaseValue,
                        'msno' => $request->productNumber,
                        'description' => $request->productDesc
                    ]);

                    $log = new Record();
                    $log->createLog([
                        'description' => 'Transaction ' . $g->reqMsgID . ' Voucher: ' . $request->productDesc,
                        'details' => $response->StarPaymentResult->fault->POR_operatorMsg,
                        'operation' => 'Buy StarTimes',
                        'status' => 'Error',
                        'origin_ip' => $request->ip(),
                        'origin_request' => $request->url(),
                        'user_id' => $request->user()->id
                    ]);


                } else {

                    return response()->json([
//                        'payment' => $response,
                        'message' => $response->StarPaymentResult->fault->POR_operatorMsg
                    ], 400);
                }
            }

        }


    }


    public function getDstvProducts($number, $type)
    {
        $msidGeneration = new TransactionGeneration();
        $transaction = $msidGeneration->generateMSID();
        $directRecharge = array(
            'req' => array(
                'authCred' => array(
                    'opName' => $this->username,
                    'password' => $this->password
                ),
                'terminalID' => 'i.Mali',
                'terminalMsgID' => '2020010700060',
                'msgID' => $msidGeneration->generateMSID(),
                'test' => true,
//                'paymentType' => 'GOTV',
//                'smartCardnumber' => '1001003' // HAS BOX OFFICE
//                'smartCardnumber' => '1001008'
                'smartCardnumber' => $number
//                'smartCardnumber' => '7026701729' // ORIGINAL MAFUMO
//                'smartCardnumber' => '1001006' // HAS NO BOX OFFICE
            )
        );

        $data = json_decode(json_encode($directRecharge), true);

        $response = $this->client->DSTVConfirmCustomer($data);

        $result = $response->DSTVConfirmCustomerResult;

        $responseNew = null;
        $responseDue = null;
        $amount = null;

        if ($result->hasFault == true) {
            return response()->json(['message' => $response->DSTVConfirmCustomerResult->fault->POR_operatorMsg], 400);
        } else {

            if ($type == "GOTV") {

                if ($result->hasGoTV == true) {

                    if ($result->hasNormal == false and $result->statusGoTV == 'Open' and $result->hasBoxOffice == false) {

                        $dataSumbit = array(
                            'req' => array(
                                'authCred' => array(
                                    'opName' => $this->username,
                                    'password' => $this->password
                                ),
                                'terminalID' => 'i.Mali',
                                'terminalMsgID' => '2020010700060',
                                'msgID' => $msidGeneration->generateMSID(),
                                'test' => true,
                                'customerNumber' => $result->customerNumber
                            )
                        );
                        $data = json_decode(json_encode($dataSumbit), true);
                        $responseNew = $this->client->DSTVAccountPmntQuote($data);
                    }

                    if ($result->hasNormal == false and $result->statusGoTV == 'Suspended' and $result->hasBoxOffice == false) {

                        $dataSumbit = array(
                            'req' => array(
                                'authCred' => array(
                                    'opName' => $this->username,
                                    'password' => $this->password
                                ),
                                'terminalID' => 'i.Mali',
                                'terminalMsgID' => '2020010700060',
                                'msgID' => $msidGeneration->generateMSID(),
                                'test' => true,
                                'customerNumber' => $result->customerNumber
                            )
                        );
                        $data = json_decode(json_encode($dataSumbit), true);
                        $client = new SoapClient($this->topUpUrl, ['trace' => true]);
                        $responseDue = $client->DSTVAccountPmntDue($data);

                    }


                    if ($responseDue != null) {
                        $responseDue = $responseDue->DSTVAccountPmntDueResult;
                    }

                    $dataNew = null;
                    if ($responseNew != null) {
                        $responseNew = $responseNew->DSTVAccountPmntQuoteResult->products->DSTVProduct;
                    }

//                    $product = $this->getDstvProduct($type, $result->customerNumber);
                    $product = $this->getDstvProduct($type, $number);

                    if ($product->DSTVAvailableProductsResult->hasFault == true) {
                        return response()->json([
//                        'product' => $product->DSTVAvailableProductsResult->products->DSTVProduct,
//                        'product' => $product->DSTVAvailableProductsResult,
                            'products' => $product,
                            'client_result' => $result,
                            'payment_quote' => $responseNew,
                            'data' => array($responseNew),
                            'payment_due' => $responseDue
                        ]);
                    } else {
                        return response()->json([
                            'product' => $product->DSTVAvailableProductsResult->products->DSTVProduct,
//                        'product' => $product->DSTVAvailableProductsResult,
                            'products' => $product,
                            'client_result' => $result,
                            'payment_quote' => $responseNew,
                            'data' => array($responseNew),
                            'payment_due' => $responseDue
                        ]);
                    }


                } else {
                    return response()->json(['message' => 'Este número não tem GOTV'], 400);
                }

            } elseif ($type == "DSTV") {

                if ($result->hasGoTV == false and $result->hasNormal == true) {

//                    if ($result->hasNormal == true and $result->statusGoTV == 'Open' and $result->hasBoxOffice == false) {
                    if ($result->hasNormal == true and $result->statusNormal == 'Open' and $result->hasBoxOffice == false) {

                        $dataSumbit = array(
                            'req' => array(
                                'authCred' => array(
                                    'opName' => $this->username,
                                    'password' => $this->password
                                ),
                                'terminalID' => 'i.Mali',
                                'terminalMsgID' => '2020010700060',
                                'msgID' => $msidGeneration->generateMSID(),
                                'test' => true,
                                'customerNumber' => $result->customerNumber
                            )
                        );
                        $data = json_decode(json_encode($dataSumbit), true);
                        $responseNew = $this->client->DSTVAccountPmntQuote($data);
                    }

//                    if ($result->hasNormal == false and $result->statusNormal == 'Suspended' and $result->hasBoxOffice == false) {
                    if ($result->hasNormal == true and $result->statusNormal == 'Suspended' and $result->hasBoxOffice == false) {

                        $dataSumbit = array(
                            'req' => array(
                                'authCred' => array(
                                    'opName' => $this->username,
                                    'password' => $this->password
                                ),
                                'terminalID' => 'i.Mali',
                                'terminalMsgID' => '2020010700060',
                                'msgID' => $msidGeneration->generateMSID(),
                                'test' => true,
                                'customerNumber' => $result->customerNumber
                            )
                        );
                        $data = json_decode(json_encode($dataSumbit), true);
                        $client = new SoapClient($this->topUpUrl, ['trace' => true]);
                        $responseDue = $client->DSTVAccountPmntDue($data);
                    }

                    if ($responseDue != null) {
                        $responseDue = $responseDue->DSTVAccountPmntDueResult;
                    }

                    if ($responseNew != null) {
                        $responseNew = $responseNew->DSTVAccountPmntQuoteResult->products->DSTVProduct;
                    }
                    $product = $this->getDstvProduct($type, $number);


//                    return response()->json([
//                        'product' => $product->DSTVAvailableProductsResult->products->DSTVProduct,
//                        'products' => $product,
//                        'client_result' => $result,
//                        'payment_quote' => array($responseNew),
//                        'data' => array($responseNew),
//                        'payment_due' => $responseDue
//                    ]);

                    if ($product->DSTVAvailableProductsResult->hasFault == true) {
                        return response()->json([
                            'products' => $product,
                            'client_result' => $result,
                            'payment_quote' => $responseNew,
                            'data' => array($responseNew),
                            'payment_due' => $responseDue
                        ]);
                    } else {
                        return response()->json([
                            'product' => $product->DSTVAvailableProductsResult->products->DSTVProduct,
                            'products' => $product,
                            'client_result' => $result,
                            'payment_quote' => $responseNew,
                            'data' => array($responseNew),
                            'payment_due' => $responseDue
                        ]);
                    }


                } else {
                    return response()->json(['message' => 'Este número não tem DSTV'], 400);
                }
            }
        }

    }

    private function getDstvProduct($type, $customerSmartCard)
    {
        $msidGeneration = new TransactionGeneration();

        $transaction = $msidGeneration->generateMSID();

        $dataSumbit = array(
            'req' => array(
                'authCred' => array(
                    'opName' => $this->username,
                    'password' => $this->password
                ),
                'terminalID' => 'i.Mali',
                'terminalMsgID' => '2020010700060',
                'msgID' => $transaction,
                'test' => false,
                'paymentType' => $type,
                'customerNumber' => '',
                'smartCardnumber' => $customerSmartCard

            )
        );
        //                'customerNumber' => "7026701729",
//                'customerNumber' => '',
//                'smartCardnumber' => "7026701729",
//                'smartCardnumber' => "4620835248",

        $data = json_decode(json_encode($dataSumbit), true);

        $response = $this->client->DSTVAvailableProducts($data);

        return $response;

    }

    public function buyDstvPacote(Request $request)
    {

        $kyc = new Kyc();
        $result = $kyc->checkUserBuyDSTV($request);
        if ($result) {
            return $result;
        } else {

            $msidGeneration = new TransactionGeneration();

            $directRecharge = array(
                'req' => array(
                    'authCred' => array(
                        'opName' => $this->username,
                        'password' => $this->password
                    ),
                    'terminalID' => 'i.Mali',
                    'terminalMsgID' => '2020010700060',
                    'msgID' => $msidGeneration->generateMSID(),
                    'test' => true,
                    'customerNumber' => $request->customerNumber,
                    'paymentType' => $request->payment_type,
                    'purchaseValue' => $request->price,
//                    'period' => $request->period,
                    'period' => 1,
                    'receiptFormat' => 'POR_FORMATED_50',
                    'terminalLocation' => 'Maputo Cidade',
                    'terminalChannel' => "i.Mali",
                    'terminalCompanyName' => 'Paytek Africa',
                    'terminalOperator' => 'i.Mali',
                )
            );

            $data = json_decode(json_encode($directRecharge), true);
            $response = $this->client->DSTVSubmitPayment($data);

            $result = $response->DSTVSubmitPaymentResult;

            if ($result->hasFault == true and $result->fault->mustALR == true) {

                GeneralAdvice::create([
                    'reqMsgID' => $result->reqMsgID,
                    'reqterminalID' => $result->reqterminalID,
                    'receiptFormat' => $result->receiptFormat,
                    'respDateTime' => $result->respDateTime,
                    'faultnumber' => $result->fault->faultnumber,
                    'user_id' => $request->user()->id,
                    'type' => 'dstv',
                    'amount' => $request->price,
                    'msno' => $request->code,
                    'description' => $request->description
                ]);

                $imaliUser = ImaliAccount::query()->where('user_id', $request->user()->id)->first();

                $totalAmount = $request->price + $imaliUser->taxa;
                DB::table('imali_accounts')->where('user_id', $request->user()->id)->increment('captive_balance', $totalAmount);


                $log = new Record();
                $log->createLog([
                    'description' => 'Nr Cliente: ' . $request->customerNumber . 'code: ' . $request->code, 'price: ' . $request->price,
                    'details' => $result->fault->POR_operatorMsg,
                    'operation' => 'Compra do Pacote '.$request->payment_type,
                    'status' => 'Error',
                    'origin_ip' => $request->ip(),
                    'origin_request' => $request->url(),
                    'user_id' => $request->user()->id
                ]);

//                if ($result->fault->faultnumber == "9") {
//
//                    // TRATAMOS O METODO DE MUSTALT
//
//                    return response()->json([
//                        'message' => $result->fault->POR_operatorMsg,
//                        'description' => 'VAmos tratar o must Alr',
//                        'faultnumber' => $result->fault->faultnumber,
//                        'data' => $response
//                    ], 400);
//                }

                // DEVEMOS TRATAR O MUSTALR
                return response()->json([
                    'message' => $result->fault->POR_operatorMsg,
                    'faultnumber' => $result->fault->faultnumber,
                    'description2' => 'VAmos tratar o must Alr',
                    'data' => $response
                ], 400);

            } elseif ($result->hasFault == true and $result->fault->mustALR == false) {

                if ($result->fault->faultnumber == "35") {
                    return response()->json([
                        'message' => "Serviço de recargas indisponível.",
                        'faultnumber' => $result->fault->faultnumber,
                        'data' => $response
                    ], 400);

                } else {
                    return response()->json([
                        'message' => $result->fault->POR_operatorMsg,
                        'faultnumber' => $result->fault->faultnumber,
                        'data' => $response
                    ], 400);

                }

            } else {


                // METODOS DE PAGAMENTO
                $this->imaliUser = ImaliAccount::query()->where('user_id', $request->user()->id)->first();
                $totalAmount = $request->price + $this->imaliUser->taxa;

                DB::table('imali_accounts')->where('user_id', $request->user()->id)->decrement('balance', $totalAmount);

                $recarga = PurchaseVoucher::create([
                    'transaction' => $result->transactionNo,
                    'vouchername' => $request->payment_type . '-' . $request->description,
//                    'vouchername' =>$request->description,
                    'vouchercode' => $request->code,
                    'vouchervalue' => $request->price,
                    'price' => $request->price,
                    'datepurchased' => $result->respDateTime,
                    'reqterminalMsgID' => $result->reqterminalMsgID,
                    'reqterminalID' => $result->reqterminalID,
                    'reqMsgID' => $result->reqMsgID,
                    'respDateTime' => $result->respDateTime,
                    'receiptFormat' => $result->receiptFormat,
                    'receipt' => $result->receipt,
                    'smsreceipt' => $result->smsreceipt,
                    'user_id' => $request->user()->id,
                    'voucher_list_id' => null,
                    'type' => 'Tv',
                    'smartCard' => $request->customerNumber
                ]);

                $log = new Record();
                $log->createLog([
                    'description' => 'Transaction' . $recarga->reqMsgID . ' Nr Cliente: ' . $request->customerNumber . 'code: ' . $request->code, 'price: ' . $request->price,
                    'details' => "Pacote Activado Com Sucesso",
                    'operation' => 'Buy StarTimes',
                    'status' => 'Success',
                    'origin_ip' => $request->ip(),
                    'origin_request' => $request->url(),
                    'user_id' => $request->user()->id
                ]);

                $notification = array(
                    'icon' => 'ic_i_mali_cover',
                    'title' => 'i.Mali recargas',
                    'body' => 'Parabéns, ' . ' comprou recarga ' . $recarga->vouchername . '.' . ' i.Mali é o Futuro',
                    'click_action' => 'com.imali.payapp.payment_RECARGA_NOTIFICATION',
                    'color' => '#ffffff'
                );

                $data = array(
                    'reqMsgID' => $recarga->reqMsgID,
                    'vouchername' => $recarga->vouchername,
                    'vouchercode' => (double)$recarga->vouchercode,
                    'data' => $recarga->created_at,
                    'price' => $recarga->price,
                    'vouchervalue' => $recarga->vouchervalue,
                    'sms' => $recarga->receipt,
                    'terminal' => 'firebase'
                );
                $this->pushNotifification($request->user()->firebase_token, $notification, $data);


                return response()->json([
                    'message' => 'Pagamento Feito com Sucesso',
                    'data' => $response,
                    'reqMsgID' => $response->DSTVSubmitPaymentResult->reqMsgID], 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);

    }
}