<?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;
}
}
To access the Kueue Pay Developer API, you’ll need an API key. You can obtain your API key by logging in to your Kueue Pay merchant account and navigating to the API section. Collect Client ID , Secret ID & Merchant ID Carefully. Keep your API key confidential and do not share it publicly.