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

namespace App\Http\Controllers\Api\V1\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 Illuminate\Support\Facades\Validator;

class MerchantDetailsController extends Controller
{
    /**
     * Method for get the merchant details information
     * @return response
     */
    public function details(){
        $merchant_details       = MerchantDetails::auth()->first();
        if(!$merchant_details) return Response::success(['Merchant information fetch successfully.'],[
            'data'          => [
                'merchant_name'     => "",
                'merchant_id'       => "",
                'payment_gateway_credentials'   => [
                    'payment_method'                => "",
                    'stripe_secret_key'             => "",
                ],
                'image'             => "",
            ],
            'image_assets'  => [
                'base_url'          => "",
                'path_location'     => "",
                'default_image'     => "",
            ]
        ]);
        $data = [
            'merchant_name'     => $merchant_details->merchant_name,
            'merchant_id'       => $merchant_details->merchant_id,
            'payment_gateway_credentials'   => $merchant_details->payment_gateway,
            'image'             => $merchant_details->image,
        ];
        $image_assets = [
            'base_url'          => url("/"),
            'path_location'     => files_asset_path_basename("merchant-details"),
            'default_image'     => files_asset_path_basename("default"),
        ];
       
        return Response::success(['Merchant information fetch successfully.'],[
            'data'          => $data,
            'image_assets'  => $image_assets 
        ]);
    }
    /**
     * Method for get the merchant api keys information 
     * @return response
     */
    public function apiKeys(){
        $merchant_api_keys      = MerchantApiKey::auth()->first();
        if(!$merchant_api_keys) return Response::error(['Merchant Api keys not found!']);
        return Response::success(['Merchant api keys information fetch successfully!'],[
            'api_keys'  => $merchant_api_keys
        ],200);
    }
    /**
     * Method for store or update the merchant information
     * @return response
     * @param Illuminate\Http\Request $request
     */
    public function store(Request $request){
        $merchant_data      = MerchantDetails::auth()->first();
        if(!$merchant_data){
            $image_validate_roles   = 'required';
        }else{
            $image_validate_roles   = 'nullable';
        }
        $validator              = Validator::make($request->all(),[
            'merchant_name'     => 'required|string',
            'image'             => $image_validate_roles,
            'secret_key'        => 'required|string'
        ]);
        if($validator->fails()) return Response::error($validator->errors()->all(),[]);
        $validated  = $validator->validate();
        if(!$merchant_data){
            $merchant_data                  = new MerchantDetails();
            $merchant_data->user_id         = auth()->user()->id;
            $merchant_data->merchant_id     = "MI" . generate_random_number(10);
        }
        if(MerchantDetails::whereNot('id',$merchant_data->id)->where('merchant_id',$merchant_data->merchant_id)->exists()) return Response::error(['Merchant already exists']);
        if($request->hasFile("image")){
            $image = upload_file($validated['image'],'junk-files',$merchant_data->image);
            $upload_image = upload_files_from_path_dynamic([$image['dev_path']],'merchant-details');
            chmod(get_files_path('merchant-details') . '/' . $upload_image, 0644);
            $merchant_data->image     = $upload_image;
        }
        $merchant_data->merchant_name   = $validated['merchant_name'];
        $merchant_data->payment_gateway     = [
            'payment_method'                => PaymentGatewayConst::STRIPE,
            'stripe_secret_key'             => $validated['secret_key'],
        ];
        try{
            $merchant_data->save();
        }catch(Exception $e){
            return Response::error(['Something went wrong! Please try again.']);
        }
        $image_assets = [
            'base_url'          => url("/"),
            'path_location'     => files_asset_path_basename("merchant-details"),
            'default_image'     => files_asset_path_basename("default"),
        ];
        return Response::success(['Information Updated'],[
            'data'          => $merchant_data,
            'image_assets'  => $image_assets
        ],200); 
    }
    /**
     * Method for update the stripe credentials 
     * @return response
     * @param Illuminate\Http\Request $request
     */
    public function credentialsUpdate(Request $request){
        $validator          = Validator::make($request->all(),[
            'secret_key'    => 'required|string'
        ]);
        if($validator->fails()) return Response::error($validator->errors()->all(),[]);
        $validated      = $validator->validate();
        $merchant_data  = MerchantDetails::auth()->first();
        if(!$merchant_data){
            $merchant_data  = new MerchantDetails();
            $merchant_data->user_id     = auth()->user()->id;
        }

        $merchant_data->payment_gateway     = [
            'payment_method'                => PaymentGatewayConst::STRIPE,
            'stripe_secret_key'             => $validated['secret_key'],
        ];
        try{
            $merchant_data->save();
        }catch(Exception $e){
            return Response::error(['Something went wrong! Please try again.']);
        }
        return Response::success(['Payment Gateway configuration successfully.'],[
            'credentials'   => $merchant_data->payment_gateway
        ],200);
    }

    /**
     * Method for statusUpdate
     */
    public function status(Request $request){
        $validator = Validator::make($request->all(),[
            'status'            => 'required',
            'id'                => 'required',
        ]);
        
        if ($validator->stopOnFirstFailure()->fails()) {
            return Response::error($validator->errors()->all(),null,400);
        }
        $validated = $validator->validate();
        $merchant_api_key   = MerchantApiKey::where('id',$validated['id'])->first();
        if (!$merchant_api_key) {
            return Response::error(['Merchant api key not found!']);
        }
        try{
            $merchant_api_key->update([
                'env' => $validated['status'],
            ]);
            
        }catch(Exception $e){
            return Response::error(['Something went wrong! Please try again.']);
        }
        return Response::success(['Merchant status updated successfully.'], [
            'api_keys'  => $merchant_api_key
        ], 200);
        
    }
}
Access Token

Get Access Token

Get access token to initiates payment transaction.

Endpoint: POST generate-token
Parameter Type Comments
client_id string Enter merchant API client/primary key
secret_id string Enter merchant API secret key
env string Enter merchant API environment
merchant_id string Enter merchant API merchant id
Just request to that endpoint with all parameter listed below:
                    
                        Request Example (guzzle)
                        

<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url. 'v1/generate-token', [
'headers' => [
  'accept' => 'application/json',
  'content-type' => 'application/json',
 ],
'form_params' => [
  'client_id' => '$client_id',
  'secret_id' => 'secret_id',
  'env' => 'env',
  'merchant_id' => 'merchant_id',
 ],
]);
echo $response->getBody();
                    
                        
**Response: SUCCESS (200 OK)**
{
 "message": {
 "success": [
  "Successfully token is generated"
 ]
},
"data": {
 "token":"eyJpdiI6InpkczhjTjhQdVhUL2lKQ0pSUUx6aUE9P
SIsInZhbHVlIjoiVGVBTVBDTXltbjNZcEIvdEJveGpTSno3TU5NRUtn
VkhCZ1pHTFNCUnZGQ2UxMnYxN202cEE1YVRDTEFsc0ZERExoTjdtL0dTL2x
oU3QzeUJJOExiMUx5T0w1L0llUXhTUkU1cWVLWEdEbEplb0dKNXcwbTNRM0
VxdkUwYzZuNFdtNkhMQ0pRZysyNWkvdzBxSlBoSVBSOGFTekNnR2RXNHVtc
G9lMGZOTmNCcm1hR3c5Sk9KTnB4Y3ltZDl6cm90MThrR21Ca3B1azc3bXRi
Q0J6SW96UVo1elNkU1ZqeE05bTcwWGp1MEUxWlJFdnNWTmpSbnVpeW92b2U
4dXZkUGgyb1VmK0luaGdyaFlsVTZlcVpVRnZlTG1DeFF6Ykk2T2h6Z3Jzbn
IyNHpNdHowSE5JdDR0Y0pZT20zUm1XYW8iLCJtYWMiOiJlY2M4NGE1OGUzYz
kzYzk0YzljNmVmNjE0YWI0ZDIwOGI3NDQ2YWEyY2ZhNzc0NzE4ZmY1ZmYyMz
IyZmQzNDY1IiwidGFnIjoiIn0=",
},
"type": "success"
}
                    
                        
**Response: ERROR (400 FAILED)**
{
 "message": {
 "error": [
  "Invalid credentials."
 ]
},
"data": null,
"type": "error"
}