/home/kueuepay/www/app/Http/Controllers/User/LoginController.php
<?php

namespace App\Http\Controllers\User;

use Exception;
use Carbon\Carbon;
use App\Models\User;
use Illuminate\Support\Str;
use Jenssegers\Agent\Agent;
use App\Models\UserLoginLog;
use Illuminate\Http\Request;
use App\Models\TemporaryData;
use App\Constants\GlobalConst;
use App\Models\MerchantApiKey;
use Illuminate\Cache\RateLimiter;
use Illuminate\Http\JsonResponse;
use Illuminate\Auth\Events\Lockout;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class LoginController extends Controller
{
    protected $request_data;
    protected $lockoutTime = 1;
    protected $cache;
    /**
     * Method for view show login form page
     * @param Illuminate\Http\Request $request
     */
    public function showLoginPage(Request $request,$order_id){
        $page_title     = "Login Page";
        $data           = TemporaryData::where('identifier',$order_id)->first();
        if(!$data) return back()->with(['error' => ['Order not found.']]);
        
        if (Carbon::parse($data->data->expiration) < Carbon::now()) {
            $data->delete();
            return redirect()->away($data->data->cancel_url)->with(['error'=> ['Sorry! Link is expired.']]);
        }
        if(auth()->check()){
            
            $user   = auth()->user();
            $update_data = [
                'merchant_api_key'  => [
                    'client_id'     => $data->data->merchant_api_key->client_id,
                    'secret_id'     => $data->data->merchant_api_key->secret_id,
                    'env'           => $data->data->merchant_api_key->env,
                    'stripe_secret' => $data->data->merchant_api_key->stripe_secret
                ],
                'merchant_account'  => [
                    'name'          => $data->data->merchant_account->name,
                    'merchant_id'   => $data->data->merchant_account->merchant_id
                ],
                'expiration'        => $data->data->expiration,
                'amount'            => $data->data->amount,
                'fixed_charge'      => $data->data->fixed_charge,
                'percent_charge'    => $data->data->percent_charge,
                'total_charge'      => $data->data->total_charge,
                'total_payable'     => $data->data->total_payable,
                'currency'          => $data->data->currency,
                'success_url'       => $data->data->success_url,
                'cancel_url'        => $data->data->cancel_url,
                'token'             => $data->data->token
            ];
            $data->update([
                'data' => $update_data
            ]);
            $user->update([
                'two_factor_verified'   => false,
            ]);
            $this->createLoginLog($user);
           
            return redirect()->route('order.checkout.index',$order_id);
        }else{
            
            return view('order.auth.login',compact(
                'page_title',
                'data'
            ));
        }
        
    }
    /**
     * Method for user login with order id
     * @param $order_id
     * @param Illuminate\Http\Request $request
     */
    public function submit(Request $request,$order_id){
        $data       = TemporaryData::where('identifier',$order_id)->first();
        if(!$data) return back()->with(['error' => ['Data not found!']]);

        $this->validateLogin($request);
        

        if (method_exists($this, 'hasTooManyLoginAttempts') &&
        $this->hasTooManyLoginAttempts($request)) {
           
            $this->fireLockoutEvent($request);
            return $this->sendLockoutResponse($request);
        }
        
        if ($this->attemptLogin($request)) {
            if ($request->hasSession()) {
                $request->session()->put('auth.password_confirmed_at', time());
            }

            return $this->sendLoginResponse($request,$data);
        }

        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }
    /**
     * Attempt to log the user into the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    protected function attemptLogin($request)
    {
        return $this->guard()->attempt(
            $this->credentials($request), $request->boolean('remember')
        );
    }
    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard("web");
    }

    /**
     * Validate the user login request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function validateLogin($request)
    {
       
        $this->request_data = $request;
        $request->validate([
            'credentials'   => 'required|string',
            'password'      => 'required|string',
        ]);

        // if user exists with banner
        if(User::where($this->username(),$request->credentials)->where('status',GlobalConst::BANNED)->exists()) {
            throw ValidationException::withMessages([
                'credentials'   => 'Your account has been suspended!',
            ]);
        }

    }
    /**
     * Get the login username to be used by the controller.
     *
     * @return string
     */
    public function username()
    {
        $request = $this->request_data->all();
        $credentials = $request['credentials'];
        if(filter_var($credentials,FILTER_VALIDATE_EMAIL)) {
            return "email";
        }
        return "username";
    }
    /**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function credentials(Request $request)
    {
        $request->merge(['status' => true]);
        $request->merge([$this->username() => $request->credentials]);
        return $request->only($this->username(), 'password','status');
    }
    /**
     * Send the response after the user was authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
     */
    protected function sendLoginResponse($request,$order)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        if ($response = $this->authenticated($request, $this->guard()->user(),$order)) {
            return $response;
        }

        return $request->wantsJson()
                    ? new JsonResponse([], 204)
                    : redirect()->intended($this->redirectPath());
    }
    /**
     * Clear the login locks for the given user credentials.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function clearLoginAttempts(Request $request)
    {
        $this->limiter()->clear($this->throttleKey($request));
    }
    /**
     * Fire an event when a lockout occurs.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function fireLockoutEvent($request)
    {
        event(new Lockout($request));
    }

    /**
     * Get the throttle key for the given request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function throttleKey(Request $request)
    {
        return Str::transliterate(Str::lower($request->input($this->username())).'|'.$request->ip());
    }

    /**
     * Get the rate limiter instance.
     *
     * @return \Illuminate\Cache\RateLimiter
     */
    protected function limiter()
    {
        return app(RateLimiter::class);
    }
    /**
     * Increment the login attempts for the user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function incrementLoginAttempts($request)
    {
        $this->limiter()->hit(
            $this->throttleKey($request), $this->decayMinutes() * 60
        );
    }
    /**
     * Increment the counter for a given key for a given decay time.
     *
     * @param  string  $key
     * @param  int  $decaySeconds
     * @return int
     */
    public function hit($key, $decaySeconds = 60)
    {
        $key = $this->cleanRateLimiterKey($key);

        $this->cache->add(
            $key.':timer', $this->availableAt($decaySeconds), $decaySeconds
        );

        $added = $this->cache->add($key, 0, $decaySeconds);

        $hits = (int) $this->cache->increment($key);

        if (! $added && $hits == 1) {
            $this->cache->put($key, 1, $decaySeconds);
        }

        return $hits;
    }
    /**
     * Get the number of minutes to throttle for.
     *
     * @return int
     */
    public function decayMinutes()
    {
        return property_exists($this, 'decayMinutes') ? $this->decayMinutes : 1;
    }
    /**
     * Get the failed login response instance.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function sendFailedLoginResponse($request)
    {
        throw ValidationException::withMessages([
            "credentials" => [trans('auth.failed')],
        ]);
    }
    /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user,$order)
    {
        $order_data     = TemporaryData::where('identifier',$order->identifier)->first();
        $update_data    = [
            'merchant_api_key'  => [
                'client_id'     => $order_data->data->merchant_api_key->client_id,
                'secret_id'     => $order_data->data->merchant_api_key->secret_id,
                'env'           => $order_data->data->merchant_api_key->env,
                'stripe_secret' => $order_data->data->merchant_api_key->stripe_secret,
            ],
            'merchant_account'  => [
                'name'          => $order_data->data->merchant_account->name,
                'merchant_id'   => $order_data->data->merchant_account->merchant_id
            ],
            'expiration'        => $order_data->data->expiration,
            'amount'            => $order_data->data->amount,
            'fixed_charge'      => $order_data->data->fixed_charge,
            'percent_charge'    => $order_data->data->percent_charge,
            'total_charge'      => $order_data->data->total_charge,
            'total_payable'     => $order_data->data->total_payable,
            'currency'          => $order_data->data->currency,
            'success_url'       => $order_data->data->success_url,
            'cancel_url'        => $order_data->data->cancel_url,
            'token'             => $order_data->data->token
        ];
        $order_data->update([
            'data'  => $update_data
        ]);
        $user->update([
            'two_factor_verified'   => false,
        ]);
        $this->createLoginLog($user);
        $this->refreshMerchant($user);
        return redirect()->intended(route('order.checkout.index',$order_data->identifier));
    }
    /**
     * Method for create login logs
     */
    protected function createLoginLog($user) {
        $client_ip = request()->ip() ?? false;
        $location = geoip()->getLocation($client_ip);

        $agent = new Agent();

        $mac = "";

        $data = [
            'user_id'       => $user->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() ?? "",
        ];

        try{
            UserLoginLog::create($data);
        }catch(Exception $e) {
            // return false;
        }
    }

    /**
     * refresh merchant info
     */
    protected function refreshMerchant($user){
        $data   = MerchantApiKey::where('user_id',$user->id)->first();
        if(!$data){
            $merchant['user_id']   = $user->id;
            $merchant['client_id'] = generate_unique_string_number();
            $merchant['secret_id'] = generate_unique_string_number();
            $merchant['env']       = global_const()::ENV_SANDBOX;
            try{
                MerchantApiKey::insert($merchant);
            }catch(Exception $e){
                throw new Exception("Failed to create merchant! Please try again.");
            }
        }
    }
}
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.