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

namespace App\Http\Controllers\User;

use App\Constants\PaymentGatewayConst;
use Exception;
use Illuminate\Http\Request;
use App\Http\Helpers\Response;
use App\Models\MerchantApiKey;
use App\Models\MerchantDetails;
use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;

class MerchantDetailsController extends Controller
{
    /**
     * Method for view index page 
     * @return view
     */
    public function index(){
        $page_title     = "Merchant Details";
        $user           = User::with(['merchant_api_keys','merchant_details'])->where('id',auth()->user()->id)->first();

        return view('user.sections.merchant-details.index',compact(
            'page_title',
            'user',
        ));
    }
    /**
     * Method for update merchant details information
     * @param $id
     * @param Illuminate\Http\Request $request
     */
    public function update(Request $request,$id){
        $data                   = MerchantDetails::where('user_id',$id)->first();
        if(!$data){
            $image_validate_roles = 'required|image';
        }else{
            $image_validate_roles = 'nullable|image';
        }
        $validator              = Validator::make($request->all(),[
           'merchant_name'      => 'required|string',
           'image'              => $image_validate_roles
        ]);
        if($validator->fails()) return back()->withErrors($validator)->withInput($request->all());
        
        if (!$data) {
            $data = new MerchantDetails();
            $data->user_id = auth()->user()->id;
            $data->merchant_id = "MI" . generate_random_number(10);
        }
        if(MerchantDetails::whereNot('id',$data->id)->where('merchant_id',$data->merchat_id)->exists()){
            throw ValidationException::withMessages([
                'name'  => "Merchant already exists!",
            ]);
        }
        if ($request->hasFile("image")) {
            $image = $this->imageValidate($request, "image", $data->image ?? null);
            $data->image = $image;
        }
        $data->merchant_name = $request->merchant_name;
    
        try {
            $data->save();
        } catch (Exception $e) {
            return back()->with(['error' => ['Something went wrong! Please try again.']]);
        }
        return back()->with(['success' => ['Information Updated.']]);
        
    }
    /**
     * Method for update payment configuartion information
     * @param $user_id
     * @param Illuminate\Http\Request $request
     */
    public function paymentConfiguartionUpdate(Request $request,$user_id){
        $validator  = Validator::make($request->all(),[
            'secret_key'     => 'required|string'
        ]);
        if($validator->fails()){
            return back()->withErrors($validator)->withInput($request->all());
        }
        $validated      = $validator->validate();
        $data           = MerchantDetails::where('user_id',$user_id)->first();
        if (!$data) {
            $data = new MerchantDetails();
            $data->user_id = auth()->user()->id;
        }
        
        $data->payment_gateway          = [
            'payment_method'            => PaymentGatewayConst::STRIPE,
            'stripe_secret_key'         => $request->secret_key,
        ];
        
        try{
            $data->save();
        }catch(Exception $e){
            return back()->with(['error'   => ['Something went wrong! Please try again.']]);
        }
        return back()->with(['success'  => ['Payment Gateway configuration successfully.']]);
    }
    /**
     * Method for statusUpdate
     */
    public function statusUpdate(Request $request){
       
        $validator = Validator::make($request->all(),[
            'status'            => 'required',
            'data_target'       => 'required|string',
        ]);
        
        if ($validator->stopOnFirstFailure()->fails()) {
            return Response::error($validator->errors()->all(),null,400);
        }
        $validated = $validator->validate();
        $merchant_api_key   = MerchantApiKey::where('id',$validated['data_target'])->first();
        if (!$merchant_api_key) {
            $error = ['error' => [__('Merchant api key not found!')]];
            return Response::error($error, null, 404);
        }
        try{
            $merchant_api_key->update([
                'env' => $validated['status'],
            ]);
            
        }catch(Exception $e){
            $error = ['error' => [__('Something went wrong!. Please try again.')]];
            return Response::error($error, null, 500);
        }
        $success = ['success' => [__('Environment updated successfully!')]];
        return Response::success($success, null, 200);
        
    }
    /**
     * Method for validate request image if have
     * @param object $request
     * @param string $input_name
     * @param string $old_image
     * @return boolean|string $upload
     */
    public function imageValidate($request,$input_name,$old_image) {
        if($request->hasFile($input_name)) {
            $image_validated = Validator::make($request->only($input_name),[
                $input_name         => "image|mimes:png,jpg,webp,jpeg,svg",
            ])->validate();

            $image = get_files_from_fileholder($request,$input_name);
            $upload = upload_files_from_path_dynamic($image,'merchant-details',$old_image);
            return $upload;
        }

        return false;
    }
}
About
top

About NFC Pay: Our Story and Mission

NFC Pay was founded with a vision to transform the way people handle transactions. Our journey is defined by a commitment to innovation, security, and convenience. We strive to deliver seamless, user-friendly payment solutions that make everyday transactions effortless and secure. Our mission is to empower you to pay with ease and confidence, anytime, anywhere.

  • Simplifying Payments, One Tap at a Time.
  • Reinventing Your Wallet for Modern Convenience.
  • Smart Payments for a Effortless Lifestyle.
  • Experience the Ease of Tap and Pay.
  • Innovative Solutions for Your Daily Transactions.

Frequently Asked Questions About NFC Pay

Here are answers to some common questions about NFC Pay. We aim to provide clear and concise information to help you understand how our platform works and how it can benefit you. If you have any further inquiries, please don’t hesitate to contact our support team.

faq-img

How do I register for NFC Pay?

Download the app and sign up using your email or phone number, then complete the verification process.

Is my payment information secure?

Yes, we use advanced encryption and security protocols to protect your payment details.

Can I add multiple cards to my NFC Pay wallet?

Absolutely, you can link multiple debit or credit cards to your wallet.

How do I transfer money to another user?

Go to the transfer section, select the recipient, enter the amount, and authorize the transfer.

What should I do if I forget my PIN?

Use the “Forgot PIN” feature in the app to reset it following the provided instructions.

How can I activate my merchant account?

Sign up for a merchant account through the app and follow the setup instructions to start accepting payments.

Can I track my payment status?

Yes, you can view and track your payment status in the account dashboard