
PK 
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
class PaymentController extends Controller
{
// Make sure your Razorpay keys are in your .env file
private $keyId;
private $keySecret;
public function __construct()
{
// This is necessary for any server-side payment logic
$this->keyId = 'rzp_test_RR2Mg928x34ZPR';///env('RAZORPAY_KEY_ID');
$this->keySecret = '5kDXZID1gujIFY7wrCP5gv19';///env('RAZORPAY_SECRET');
}
public function showPaymentForm(Request $request)
{
return view('frontend.orders.payment');
}
public function successPayment(Request $request)
{
return view('frontend.orders.success-rzp');
}
// Step 1: Create the Razorpay Order ID
public function createOrder(Request $request)
{
$amountInPaise = $request->amount * 100;
$data = [
'amount' => $amountInPaise,
'currency' => 'INR',
'receipt' => 'rcpt_' . time(),
'payment_capture' => 1
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.razorpay.com/v1/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->keyId . ":" . $this->keySecret); // Set Basic Auth
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$order = json_decode($response, true);
if ($httpCode !== 200 || !isset($order['id'])) {
// Log the error response for debugging
\Log::error('Razorpay Order Creation Failed: ' . $response);
return response()->json([
'error' => 'Failed to create Razorpay Order.',
'details' => $order
], 500);
}
return response()->json([
'key' => $this->keyId,
'amount' => $order['amount'],
'orderId' => $order['id']
]);
/*$client = new Client();
$response = $client->request('POST', 'https://api.razorpay.com/v1/orders', [
'auth' => [$this->keyId, $this->keySecret],
'json' => $data
]);
$order = json_decode($response->getBody()->getContents(), true);
return response()->json([
'key' => $this->keyId,
'amount' => $order['amount'],
'orderId' => $order['id']
]);*/
}
// Step 2: Verify the payment signature
public function verifyPayment(Request $request)
{
// Get the data sent by the Razorpay handler function
$signature = $request->razorpay_signature;
$orderId = $request->razorpay_order_id;
$paymentId = $request->razorpay_payment_id;
// The hash must be generated using the SECRET key on the SERVER side
$message = $orderId . '|' . $paymentId;
$generatedSignature = hash_hmac('sha256', $message, $this->keySecret);
if ($generatedSignature === $signature) {
// Payment is SUCCESSFUL and VERIFIED.
// You can now update your database and redirect.
return redirect('/payment/success')->with('message', 'Payment successful!');
}
// Payment verification FAILED.
return redirect('/payment')->with('error', 'Payment verification failed!');
}
}


PK 99