Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/public_html/app/Modules/Schedules/Http/Controllers/Admin/ |
<?php
namespace App\Modules\Schedules\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Modules\Testimonials\Http\Requests\TestimonialRequest;
use App\Modules\Testimonials\Models\Testimonial;
use App\Modules\Testimonials\Models\SalonReviews;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laracasts\Flash\Flash;
use Yajra\Datatables\Datatables;
use App\PendingPayment;
class PaymentsController extends AdminController
{
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$this->data['page_title'] = 'Payments';
return view('schedules::admin.payment.index', $this->data);
}
public function show(Request $request) {
// dd("here");
//create object contact
$obj = $this->getData($request);
// dd($obj);
//create array with permissions access
$this->data['can'] = [
'take_payment' => $this->data['user']->roles->pluck('slug')[0] == 'developer' || $this->data['user']->roles->pluck('slug')[0] == 'salon',
'delete' => $this->data['user']->roles->pluck('slug')[0] == 'developer' || $this->data['user']->roles->pluck('slug')[0] == 'salon',
];
//return datatables data
return Datatables::of($obj)
->editColumn('order_id', function($o){
return $o->order_id;
})
->editColumn('user', function($o){
return !empty($o->user->name) ? $o->user->name : '';
})
->addColumn('actions', function ($o) {
$this->data['o']=$o;
return view('schedules::admin.payment.payment_list_actions',$this->data)->render();
})
->rawColumns([
'actions'
])
->removeColumn('id')
->removeColumn('updated_at')
->make(true);
}
/**
* Show a list of all companies
*
* @return mixed
*/
public function data(Request $request)
{
// dd("here ind datra");
//create object contact
$obj = $this->getData($request);
// dd($obj);
//create array with permissions access
$this->data['can'] = [
'edit' => $this->data['user']->roles->pluck('slug')[0] == 'developer' || $this->data['user']->roles->pluck('slug')[0] == 'salon',
'delete' => $this->data['user']->roles->pluck('slug')[0] == 'developer' || $this->data['user']->roles->pluck('slug')[0] == 'salon',
];
//return datatables data
return Datatables::of($obj)
->editColumn('order_id', function($o){
return $o->order_id;
})
->addColumn('actions', function ($o) {
$this->data['o']=$o;
return view('testimonials::admin.reviews_list_actions',$this->data)->render();
})
->removeColumn('id')
->removeColumn('updated_at')
->make(true);
}
/**
* Get object entries
* @param Request $request
*/
protected function getData(Request $request)
{
$obj = PendingPayment::where('status', 0)->get();
//return object
return $obj;
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$this->data['page_title'] = 'Reviews';
//show page
return view('testimonials::admin.reviews_create_edit', $this->data);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store(Request $request)
{
// dd($request->all());
if(Auth::check()) {
if(Auth::user()->hasRole('salon')) {
$data = [
'user_name' => $request->user_name,
'therapist_name' => $request->therapist_name,
'salon_rating' => $request->salon_rating,
'atmosphere' => $request->atmosphere,
'sanitation' => $request->sanitation,
'employees' => $request->employees,
'satisfaction' => $request->satisfaction,
'review' => $request->review,
'is_approved' => (int)$request->is_approved,
'salon_id' => Auth::user()->id
];
} else {
$data = [
'user_name' => $request->user_name,
'therapist_name' => $request->therapist_name,
'salon_rating' => $request->salon_rating,
'atmosphere' => $request->atmosphere,
'sanitation' => $request->sanitation,
'employees' => $request->employees,
'satisfaction' => $request->satisfaction,
'review' => $request->review,
'is_approved' => (int)$request->is_approved,
];
}
}
//save new service type
$obj = SalonReviews::create($data);
//redirect
if ($request->save) {
return redirect(route('admin.reviews.edit', ['id' => $obj->id]));
} elseif ($request->save_exit) {
return redirect(route('admin.reviews.index'));
}
}
/**
* Edit Service Type
*
* @param $label
* @return \BladeView|bool|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit($id)
{
$this->data['page_title'] = 'Reviews';
$salonreview = SalonReviews::where('id',$id)->first();
$this->data['obj'] = $salonreview;
// dd($this->data['obj'], $id);
//show page
return view('testimonials::admin.reviews_create_edit', $this->data);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update(Request $request, SalonReviews $salonreview, $id)
{
// dd($request->all(), $salonreview, $id);
//fields to be updated
$fillable = $salonreview['fillable'];
$fields = array();
foreach ($request->all() as $field => $f) {
if (in_array($field, $fillable)) {
$fields[$field] = $f;
}
} //end foreach
$fields['is_approved'] = (int)$request->is_approved;
// $fields['posted_at'] = Carbon::createFromFormat('d F Y',$request->posted_at)->format('Y-m-d H:i:s');
//update label
SalonReviews::where('id', $id)->update($fields);
//redirect
if ($request->save) {
return redirect(route('admin.reviews.edit', ['id' => $testimonials->id]));
} elseif ($request->save_exit) {
return redirect(route('admin.reviews.index'));
}
}
/**
* Delete page
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function delete($id)
{
// dd($id);
$this->data['obj'] = PendingPayment::where('id', $id)->first();
return view('schedules::admin.payment.delete', $this->data);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$salonreview = SalonReviews::where('id', $id)->first();
//delete contact
$salonreview->delete();
//set success message
Flash::info("The user review has been successfully deleted");
//redirect to contacts list
return redirect(route('admin.reviews.index'));
}
public function paid($id) {
$payment = PendingPayment::where('id', $id)->first();
$payment->status = 1;
$payment->update();
//set success message
Flash::info("Status updated successfully");
//redirect to contacts list
return redirect(route('admin.schedules.payments.index'));
}
public function take_payment($id) {
// dd("id", $id);
$payment = PendingPayment::where('id', $id)->first();
if($payment->payment_id) {
$payment_intent = $this->retrievePaymentIntent($payment->payment_id);
// dd($payment_intent);
$responseData = json_decode($payment_intent->getContent(), true);
// dd($responseData);
if(!empty($responseData['payment_intent']) && $responseData['payment_intent']['status'] == 'requires_capture') {
$capturePayment = $this->capturePayment($payment->payment_id);
} else {
$capturePayment = $this->confirmPaymentIntent($payment->payment_id); // here need to uncomment
}
// Get the data from the JsonResponse and decode it
$responseData = json_decode($capturePayment->getContent(), true); // here need to uncomment
// $responseData = json_decode($capturePayment, true);
// dd($responseData);
if(!empty($responseData['error'])) {
//set success message
Flash::info($responseData['error']);
//redirect to contacts list
return redirect(route('admin.schedules.payments.index'));
} elseif (!empty($responseData['status'])) {
//set success message
Flash::info("Captured Payment Successfully");
//redirect to contacts list
return redirect(route('admin.schedules.payments.index'));
} else {
//set success message
Flash::info("Something went wrong! Do it manualy through stripe");
//redirect to contacts list
return redirect(route('admin.schedules.payments.index'));
}
} else {
//set success message
Flash::info("Unable to take payment");
//redirect to contacts list
return redirect(route('admin.schedules.payments.index'));
}
}
public function retrievePaymentIntent($paymentIntentId)
{
// Your Stripe secret key
$secretKey = env('stripe_secret_key');
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/payment_intents/$paymentIntentId");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $secretKey,
]);
// Execute cURL and capture the response
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
return response()->json(['error' => 'cURL error: ' . curl_error($ch)], 500);
}
// Close cURL session
curl_close($ch);
// Decode the response JSON
$paymentIntent = json_decode($response);
// Handle the PaymentIntent data as needed
// For example, return it to the client or perform further actions
return response()->json(['payment_intent' => $paymentIntent]);
}
public function confirmAndCapturePayment($payment_intent_id)
{
$paymentIntentId = $payment_intent_id;
$stripeSecretKey = env('stripe_secret_key');
// Step 1: Retrieve the PaymentIntent to check its status
$retrieveUrl = "https://api.stripe.com/v1/payment_intents/$paymentIntentId";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $retrieveUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $stripeSecretKey,
]);
try {
$response = curl_exec($ch);
if ($response === false) {
throw new \Exception(curl_error($ch));
}
$responseData = json_decode($response, true);
if (isset($responseData['error'])) {
return response()->json(['error' => $responseData['error']['message']], 500);
} else {
$paymentIntentStatus = $responseData['status'];
// Step 2: Check if the PaymentIntent is in the correct state for capture
if ($paymentIntentStatus === 'requires_capture') {
// Step 3: Capture the payment
$captureUrl = "https://api.stripe.com/v1/payment_intents/$paymentIntentId/capture";
curl_setopt($ch, CURLOPT_URL, $captureUrl);
curl_setopt($ch, CURLOPT_POST, true);
$captureResponse = curl_exec($ch);
if ($captureResponse === false) {
throw new \Exception(curl_error($ch));
}
$captureData = json_decode($captureResponse, true);
if (isset($captureData['error'])) {
return response()->json(['error' => $captureData['error']['message']], 500);
} else {
return response()->json(['message' => 'Payment captured successfully']);
}
} else {
return response()->json(['error' => 'PaymentIntent is not in the correct state for capture'], 500);
}
}
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
} finally {
curl_close($ch);
}
}
public function confirmPaymentIntent($payment_intent_id)
{
$paymentIntentId = $payment_intent_id;
// return $this->capturePayment($paymentIntentId);
$stripeSecretKey = env('stripe_secret_key');
$confirmUrl = "https://api.stripe.com/v1/payment_intents/$paymentIntentId/confirm";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $confirmUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $stripeSecretKey,
]);
try {
$response = curl_exec($ch);
if ($response === false) {
throw new \Exception(curl_error($ch));
}
$responseData = json_decode($response, true);
if (isset($responseData['error'])) {
// dd("inside confirmation",$responseData);
return response()->json(['error' => $responseData['error']['message']], 500);
} else {
dd($responseData);
// dd("hre");
// Now that the PaymentIntent is confirmed, you can proceed to capture it.
return $this->capturePayment($paymentIntentId);
}
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
} finally {
curl_close($ch);
}
}
public function capturePayment($payment_intent_id)
{
$paymentIntentId = $payment_intent_id;
// dd($paymentIntentId);
$stripeSecretKey = env('stripe_secret_key');
$captureUrl = "https://api.stripe.com/v1/payment_intents/$paymentIntentId/capture";
// dd($captureUrl);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $captureUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $stripeSecretKey,
]);
try {
$response = curl_exec($ch);
if ($response === false) {
throw new \Exception(curl_error($ch));
}
$responseData = json_decode($response, true);
// dd($responseData);
if (isset($responseData['error'])) {
return response()->json(['error' => $responseData['error']['message']], 500);
} else {
$payment = PendingPayment::where('payment_id', $paymentIntentId)->first();
$payment->status = 2;
$payment->response_data = $response;
$payment->update();
return response()->json(['status' => true,'message' => 'Payment captured successfully']);
}
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 500);
} finally {
curl_close($ch);
}
}
}