| Server IP : 13.235.167.51 / Your IP : 216.73.217.116 Web Server : Apache System : Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64 User : root ( 0) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/app.machinox.in/app/Helpers/ |
Upload File : |
<?php
namespace App\Helpers;
use App\Models\RazorpayWebhook;
use App\Models\Transaction;
use App\Models\UserSubscription;
use App\Traits\SubscriptionTrait;
use Razorpay\Api\Api;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
class RazorpayHelper
{
protected static $razorpayInstance;
use SubscriptionTrait;
public static function getInstance()
{
if (!self::$razorpayInstance) {
$key = config('services.razorpay.key');
$secret = config('services.razorpay.secret');
self::$razorpayInstance = new Api($key, $secret);
}
return self::$razorpayInstance;
}
public static function createCustomer($data)
{
try {
$customer = self::getInstance()->customer->create([
'name' => data_get($data, 'name'),
'email' => data_get($data, 'email'),
'contact' => data_get($data, 'contact')
]);
Log::info('Razorpay Customer Created', ['customer' => $customer]);
return $customer;
} catch (Exception $e) {
Log::error('Error creating Razorpay customer: ' . $e->getMessage());
throw $e;
}
}
public static function createOrder($data)
{
try {
// dd($data);
$order = self::getInstance()->order->create([
'receipt' => data_get($data, 'receipt', 'rcpt_' . uniqid()),
'amount' => data_get($data, 'amount'),
'currency' => data_get($data, 'currency', 'INR'),
'payment_capture' => data_get($data, 'payment_capture', 1),
'notes' => data_get($data, 'notes', []),
]);
Log::info('Razorpay Order Created', ['order' => $order]);
return $order;
} catch (Exception $e) {
Log::error('Error creating Razorpay order: ' . $e->getMessage());
throw $e;
}
}
public static function getPaymentDetails($paymentId)
{
try {
$payment = self::getInstance()->payment->fetch($paymentId);
Log::info('Razorpay Payment Fetched', ['payment' => $payment]);
return $payment;
} catch (Exception $e) {
Log::error('Error fetching Razorpay payment: ' . $e->getMessage());
return null;
}
}
public static function initiateRefund($paymentId, $amount = null, $notes = [])
{
try {
$data = ['payment_id' => $paymentId, 'notes' => $notes];
if ($amount) {
$data['amount'] = $amount;
}
// If amount is not provided, fetch the payment details to get the full amount
$refund = self::getInstance()->refund->create($data);
Log::info('Razorpay Refund Created', ['refund' => $refund]);
return $refund;
} catch (Exception $e) {
Log::error('Error initiating Razorpay refund: ' . $e->getMessage());
return null;
}
}
public function handleWebhook($request)
{
try {
$requestUrl = $request->fullUrl();
$ip = $request->ip();
$requestBody = json_encode($request->all());
$eventName = $request->input('event');
$resource = $request->input('payload');
// Log the webhook info
RazorpayWebhook::setInfo(
$requestUrl,
$ip,
$requestBody,
null,
$eventName
);
// Handle supported events
switch ($eventName) {
case 'payment.captured':
case 'order.paid':
$paymentEntity = data_get($resource, 'payment.entity', []);
Log::info('Razorpay Payment Captured', ['paymentEntity' => $paymentEntity]);
// Token to identify the payment record
$transactionToken = data_get($paymentEntity, 'notes.ref_id');
$orderPayment = Transaction::where('uuid', $transactionToken)->first();
if (!$orderPayment) {
return ['status' => 'error', 'message' => 'Transaction not found.'];
}
if ($orderPayment->status === 'SUCCESS') {
return ['status' => 'success', 'message' => 'Payment already processed.'];
}
// Update the order payment record
$orderPayment->payment_status === 'SUCCESS';
$orderPayment->razorpay_payment_id = data_get($paymentEntity, 'id');
$orderPayment->razorpay_order_id = data_get($paymentEntity, 'order_id');
$orderPayment->save();
if($orderPayment->type == 'BOOST'){
$this->setBoost($orderPayment);
} elseif ($orderPayment->type == 'SEEKER'){
$this->setSeekerSubscription($orderPayment);
} else {
$this->setUserSubscription($orderPayment);
}
$orderPayment->trx_status = 'SUCCESS';
$orderPayment->trx_date_time = now();
$orderPayment->save();
// Other handled events (no additional action)
return ['status' => 'success', 'message' => 'Payment processed successfully.'];
case 'payment.failed':
$paymentEntity = data_get($resource, 'payment.entity', []);
Log::info('Razorpay Payment Captured', ['paymentEntity' => $paymentEntity]);
// Token to identify the payment record
$transactionToken = data_get($paymentEntity, 'notes.ref_id');
$orderPayment = Transaction::where('uuid', $transactionToken)->first();
if (!$orderPayment) {
return ['status' => 'error', 'message' => 'Transaction not found.'];
}
if ($orderPayment->status === 'FAILED') {
return ['status' => 'success', 'message' => 'Payment already processed.'];
}
// Update the order payment record
$orderPayment->trx_status === 'FAILED';
$orderPayment->razorpay_payment_id = data_get($paymentEntity, 'id');
$orderPayment->razorpay_order_id = data_get($paymentEntity, 'order_id');
$orderPayment->save();
return ['status' => 'success', 'message' => 'Payment failed.'];
default:
return [
'type' => 'ignored',
'message' => 'Event not handled: ' . $eventName,
];
}
} catch (Exception $ex) {
Log::error("Razorpay Webhook Error", [
'error' => $ex->getMessage(),
'trace' => $ex->getTraceAsString(),
]);
return [
'type' => 'error',
'message' => 'Webhook processing failed.',
];
}
}
}