<?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;
}
}
Initiates a new payment transaction.
create-order
| Parameter | Type | Details |
|---|---|---|
| amount | decimal | Your Amount , Must be rounded at 2 precision. |
| currency | string | Currency Code, Must be in Upper Case (Alpha-3 code) |
| success_url | string | Enter your return or success URL |
| cancel_url | string (optional) | Enter your cancel or failed URL |
Request Example (guzzle)
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url.'create-order', [
'headers' => [
'Authorization' => 'Bearer '. $authorizationToken,
'accept' => 'application/json',
'content-type' => 'application/json',
],
'form_params' => [
'amount' => '$amount',
'currency' => 'currency',
'success_url' => 'success_url',
'cancel_url' => 'cancel_url',
],
]);
echo $response->getBody();
**Response: SUCCESS (200 OK)**
{
"message": {
"success": [
"Order created successfully."
]
},
"data": {
"redirect_url":"https://example.com/login/OISADFDFSDFSF",
"order_details":{
"amount" : "10",
"fixed_charge" : 2,
"percent_charge" : 1,
"total_charge" : 3,
"total_payable" : 13,
"currency" : "USD",
"expiry_time": "2024-04-25T06:48:35.984285Z",
"success_url": "http://127.0.0.1/nfcpay/user/transaction/success",
"cancel_url": "http://127.0.0.1/nfcpay/user/transaction/cancel"
}
},
"type": "success"
}
**Response: ERROR (400 FAILED)**
{
"message": {
"error": [
"Invalid token."
]
},
"data": null,
"type": "error"
}