/home/kueuepay/public_html/app/Http/Controllers/User/TransferMoneyController.php
<?php

namespace App\Http\Controllers\User;

use Exception;
use App\Models\User;
use App\Models\UserWallet;
use Jenssegers\Agent\Agent;
use Illuminate\Http\Request;
use App\Constants\GlobalConst;
use App\Models\UserNotification;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Constants\PaymentGatewayConst;
use App\Models\Admin\BasicSettings;
use App\Models\Admin\TransactionSetting;
use App\Models\Transaction;
use App\Notifications\User\TransferMoneyNotification;
use App\Notifications\User\TransferMoneyReceivedNotification;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Validator;

class TransferMoneyController extends Controller
{
    /**
     * Method for view transfer money page
     * @return view
     */
    public function index(){
        $page_title             = "Transfer Money";
        $transaction_settings   = TransactionSetting::where('slug',GlobalConst::TRANSFER_MONEY)->first();
        $transactions           = Transaction::auth()->where('type',PaymentGatewayConst::TYPETRANSFERMONEY)->latest()->take(3)->get();
        
        return view('user.sections.transfer-money.index',compact(
            'page_title',
            'transaction_settings',
            'transactions'
        ));
    }
    /**
     * Method for confirm transfer money
     * @param Illuminate\Http\Request $request
     */
    public function confirm(Request $request){
        $validator          = Validator::make($request->all(),[
            'amount'        => 'required|numeric',
            'email'         => 'required|email',
            'remark'        => 'nullable'
        ]);
        if($validator->fails()) return back()->withErrors($validator)->withInput($request->all());
        $validated          = $validator->validate();
        if(auth()->user()->email == $validated['email']){
            return back()->with(['error' => ['Sorry! You can not transfer money to your own account.']]);
        }
        $user_wallet        = UserWallet::auth()->first();
        if(!$user_wallet) return back()->with(['error' => ['Sorry! Wallet not found.']]);
        $amount             = $validated['amount'];
        $receiver           = User::with(['wallet'])->where('email',$validated['email'])->first();
        if(!$receiver) return back()->with(['error' => ['Sorry! Receiver not found.']]);
        $transaction_settings   = TransactionSetting::where('slug',GlobalConst::TRANSFER_MONEY)->first();
        if(!$transaction_settings) return back()->with(['Sorry! Transaction charges not found.']);

        if($user_wallet->balance < $amount){
            return back()->with(['error' => ['Sorry! Insufficient balance.']]);
        }
        $basic_settings      = BasicSettings::first();
        $fixed_charge        = $transaction_settings->fixed_charge;
        $percent_charge      = ($amount * $transaction_settings->percent_charge) / 100;
        $total_charge        = $fixed_charge + $percent_charge;
        $payable_amount      = $amount + $total_charge;

        if($transaction_settings->min_limit > $payable_amount || $transaction_settings->max_limit < $payable_amount){
            return back()->with(['error' => ['Please follow the transaction limit.']]);
        }

        if($user_wallet->balance < $payable_amount){
            return back()->with(['error' => ['Sorry! Insufficient balance.']]);
        }
        if(isset($validated['remark']) == '' || isset($validated['remark']) == null){
            $remark = null;
        }else{
            $remark = $validated['remark'];
        }
        try{
            // for sender
            $transaction      = $this->insertSenderRecord($amount,$user_wallet,$receiver,$fixed_charge,$percent_charge,$total_charge,$payable_amount,$remark);
            $this->transactionDevice($transaction);
            $this->userNotification($transaction,$amount,$receiver);
            if($basic_settings->email_notification){
                try{
                    Notification::route('mail',auth()->user()->email)->notify(new TransferMoneyNotification($amount,$receiver,$payable_amount));
                }catch(Exception $e){}
            }
            
            // for receiver
            $receiver_transaction      = $this->insertReceiverRecord($amount,$user_wallet,$receiver,$fixed_charge,$percent_charge,$total_charge,$payable_amount,$remark);
            $this->userReceiverNotification($receiver_transaction,$amount,$receiver);
            if($basic_settings->email_notification){
                try{
                    Notification::route('mail',$receiver->email)->notify(new TransferMoneyReceivedNotification($amount,$receiver));
                }catch(Exception $e){}
            }
        }catch(Exception $e){
            return back()->with(['error' => ['Something went wrong! Please try again.']]);
        }
        return back()->with(['success' => ['Successfully transfered money.']]);
    }
    //insert transaction data
    function insertSenderRecord($amount,$user_wallet,$receiver,$fixed_charge,$percent_charge,$total_charge,$payable_amount,$remark){
        $available_balance = $user_wallet->balance - $payable_amount;
        DB::beginTransaction();
        try{
            $trx_id         = generateTrxString("transactions","trx_id","TM",8);
            $id = DB::table('transactions')->insertGetId([
                'trx_id'                => $trx_id,
                'user_id'               => auth()->user()->id,
                'user_wallet_id'        => $user_wallet->id,
                'type'                  => PaymentGatewayConst::TYPETRANSFERMONEY,
                'request_amount'        => $amount,
                'fixed_charge'          => $fixed_charge,
                'percent_charge'        => $percent_charge,
                'total_charge'          => $total_charge,
                'total_payable'         => $payable_amount,
                'request_currency'      => get_default_currency_code(),
                'available_balance'     => $available_balance,
                'payment_currency'      => get_default_currency_code(),
                'remark'                => ucwords(remove_special_char("Transfer Money"," ")) . " Send to" . " " . $receiver->email,
                'details'               => json_encode(['remark' => $remark,'receiver' => [
                    'fullname'          => $receiver->fullname,
                    'email'             => $receiver->email
                ]]),
                'attribute'             => PaymentGatewayConst::SEND,
                'status'                => PaymentGatewayConst::STATUSSUCCESS,
                'created_at'            => now()
            ]);
            $this->updateSenderWalletBalance($user_wallet,$payable_amount);
            DB::commit();
        }catch(Exception $e){
            DB::rollBack();
            throw new Exception(__("Something went wrong! Please try again."));
        }
        return $id;
    }
    // update sender wallet balance
    function updateSenderWalletBalance($user_wallet,$amount){
        $user_wallet->update([
            'balance'   => $user_wallet->balance - $amount,
        ]);
    }
    // save the transaction device information
    function transactionDevice($id){
        $client_ip = request()->ip() ?? false;
        $location = geoip()->getLocation($client_ip);
        $agent = new Agent();
        $mac = "";
        DB::beginTransaction();
        try{
            DB::table("transaction_devices")->insert([
                'transaction_id'=> $id,
                'ip'            => $client_ip,
                'mac'           => $mac,
                'city'          => $location['city'] ?? "",
                'country'       => $location['country'] ?? "",
                'longitude'     => $location['lon'] ?? "",
                'latitude'      => $location['lat'] ?? "",
                'timezone'      => $location['timezone'] ?? "",
                'browser'       => $agent->browser() ?? "",
                'os'            => $agent->platform() ?? "",
            ]);
            DB::commit();
        }catch(Exception $e) {
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }
    //user notification
    public function userNotification($id,$amount,$receiver){
        UserNotification::create([
            'user_id'           => auth()->user()->id,
            'transaction_id'    => $id,
            'details'           => [
                'title'         => 'Transfer Money to ' .$receiver->email,
                'amount'        => floatval($amount),
                'currency'      => get_default_currency_code(),
                'message'       => "Successfully Send."
            ],
        ]);
    }
    //insert transaction data
    function insertReceiverRecord($amount,$user_wallet,$receiver,$fixed_charge,$percent_charge,$total_charge,$payable_amount,$remark){
        $available_balance = $receiver->wallet->balance + $amount;
        DB::beginTransaction();
        try{
            $trx_id         = generateTrxString("transactions","trx_id","TM",8);
            
            $id = DB::table('transactions')->insertGetId([
                'trx_id'                => $trx_id,
                'user_id'               => $receiver->id,
                'user_wallet_id'        => $receiver->wallet->id,
                'type'                  => PaymentGatewayConst::TYPETRANSFERMONEY,
                'request_amount'        => $amount,
                'fixed_charge'          => $fixed_charge,
                'percent_charge'        => $percent_charge,
                'total_charge'          => $total_charge,
                'total_payable'         => $payable_amount,
                'request_currency'      => get_default_currency_code(),
                'available_balance'     => $available_balance,
                'payment_currency'      => get_default_currency_code(),
                'remark'                => ucwords(remove_special_char("Received Money"," ")) . " From" . " " . auth()->user()->email,
                'details'               => json_encode(['remark' => $remark,'sender' => [
                    'fullname'          => auth()->user()->fullname,
                    'email'             => auth()->user()->email
                ]]),
                'attribute'             => PaymentGatewayConst::RECEIVED,
                'status'                => PaymentGatewayConst::STATUSSUCCESS,
                'created_at'            => now()
            ]);
            $this->updateReceiverWalletBalance($receiver,$amount);

            DB::commit();
        }catch(Exception $e){
            DB::rollBack();
            throw new Exception(__("Something went wrong! Please try again."));
        }
        return $id;
    }
    //update receiver wallet balance
    function updateReceiverWalletBalance($receiver,$amount){
        $update_balance     = $receiver->wallet->balance + $amount;
        $receiver->wallet->update([
            'balance' => $update_balance,
        ]);
    }
    //user notification
    public function userReceiverNotification($id,$amount,$receiver){
        UserNotification::create([
            'user_id'           => $receiver->id,
            'transaction_id'    => $id,
            'details'           => [
                'title'         => 'Received Money From ' .auth()->user()->email,
                'amount'        => floatval($amount),
                'currency'      => get_default_currency_code(),
                'message'       => "Successfully Received."
            ],
        ]);
    }
}
FAQ

FAQ

1. What is the Kueue Pay Payment Gateway?

The Kueue Pay Payment Gateway is an innovative technology that facilitates seamless and secure transactions between merchants and their customers. It enables businesses to accept debit and credit card payments both online and in physical stores.

2. How does the Kueue Pay Payment Gateway work?

The Kueue Pay Payment Gateway acts as a bridge between a merchant’s website or point-of-sale system and the payment processing network. It securely transmits payment information, authorizes transactions, and provides real-time status updates.

3. What is the advantage of using Kueue Pay’s Developer API?

The Kueue Pay Developer API empowers developers and entrepreneurs to integrate the Kueue Pay Payment Gateway directly into their websites or applications. This streamlines the payment process for customers and provides businesses with a customizable and efficient payment solution.

4. How can I access the Kueue Pay Developer API?

To access the Kueue Pay Developer API, you need to sign up for a developer account on our platform. Once registered, you’ll receive an API key that you can use to authenticate your API requests.

5. What types of transactions can I handle with the Kueue Pay Developer API?

The Kueue Pay Developer API allows you to initiate payments, check the status of payments, and process refunds. You can create a seamless payment experience for your customers while maintaining control over transaction management.

6. Is the Kueue Pay Developer API suitable for my business size and industry?

Yes, the Kueue Pay Developer API is designed to accommodate businesses of varying sizes and industries. Whether you’re a small online store or a large enterprise, our API can be tailored to fit your specific payment needs.

7. How user-friendly is the Kueue Pay Developer API integration process?

The Kueue Pay Developer API is designed with simplicity and ease of use in mind. Our comprehensive documentation, code samples, and developer support resources ensure a smooth integration process for any web platform.

8. Are there any fees associated with using the Kueue Pay Payment Gateway and API?

We offer competitive pricing plans for using the Kueue Pay Payment Gateway and Developer API. Details about fees and pricing tiers can be found on our developer portal.

9. Can I customize the payment experience for my customers using the Kueue Pay API?

Absolutely, the Kueue Pay Developer API offers customization options that allow you to tailor the payment experience to match your brand and user interface. You can create a seamless and cohesive payment journey for your customers.

10. What kind of support is available if I encounter issues during API integration?

We provide dedicated developer support to assist you with any issues or questions you may have during the API integration process. Reach out to our support team at developersupport@NFCPay.com for prompt assistance.

Remember, our goal is to empower your business with a robust and efficient payment solution. If you have any additional questions or concerns, feel free to explore our developer portal or contact our support team.