<?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;
}
}
Get access token to initiates payment transaction.
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 |
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":"eyJpdiI6InpkczhjTjhQdVhUL2lKQ0pSUUx6aUE9PSIsInZhbHVlIjoiVGVBTVBDTXltbjNZcEIvdEJveGpTSno3TU5NRUtnVkhCZ1pHTFNCUnZGQ2UxMnYxN202cEE1YVRDTEFsc0ZERExoTjdtL0dTL2xoU3QzeUJJOExiMUx5T0w1L0llUXhTUkU1cWVLWEdEbEplb0dKNXcwbTNRM0VxdkUwYzZuNFdtNkhMQ0pRZysyNWkvdzBxSlBoSVBSOGFTekNnR2RXNHVtcG9lMGZOTmNCcm1hR3c5Sk9KTnB4Y3ltZDl6cm90MThrR21Ca3B1azc3bXRiQ0J6SW96UVo1elNkU1ZqeE05bTcwWGp1MEUxWlJFdnNWTmpSbnVpeW92b2U4dXZkUGgyb1VmK0luaGdyaFlsVTZlcVpVRnZlTG1DeFF6Ykk2T2h6Z3JzbnIyNHpNdHowSE5JdDR0Y0pZT20zUm1XYW8iLCJtYWMiOiJlY2M4NGE1OGUzYzkzYzk0YzljNmVmNjE0YWI0ZDIwOGI3NDQ2YWEyY2ZhNzc0NzE4ZmY1ZmYyMz
IyZmQzNDY1IiwidGFnIjoiIn0=",
},
"type": "success"
}
**Response: ERROR (400 FAILED)**
{
"message": {
"error": [
"Invalid credentials."
]
},
"data": null,
"type": "error"
}