| 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/Http/Controllers/WebService/ |
Upload File : |
<?php
namespace App\Http\Controllers\WebService;
use App\Actions\WebService\SelectSubscriptionAction;
use App\Http\Controllers\Controller;
use App\Http\Requests\WebService\SubscriptionRequest;
use App\Models\Subscription;
use App\Models\SubscriptionPlan;
use App\Models\UserSubscription;
use Exception;
use Illuminate\Http\Request;
use App\Helpers\RazorpayHelper;
use App\Models\Advertisement;
use App\Models\Notification;
use App\Models\Transaction;
use App\Traits\SubscriptionTrait;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Nonstandard\Uuid;
class SubscriptionController extends Controller
{
use SubscriptionTrait;
public function setInfo(SubscriptionRequest $request, SelectSubscriptionAction $action)
{
try {
$response = $action->handle($request);
return $this->sendSuccess($response->message, ['uuid' => $response->uuid]);
} catch (Exception $e) {
return response()->json(['status' => 'error', 'message' => $e->getMessage()], 500);
}
}
public function GetList()
{
try {
// $list = SubscriptionPlan::select('id', 'sub_id', 'type', 'description', 'amount', 'discount_amount', 'duration')
// ->get()
// ->groupBy('duration');
$list = Subscription::with('plans')->first();
return $this->sendSuccess(trans('messages.OK'), $list);
} catch (Exception $e) {
return response()->json(['status' => 'error', 'message' => $e->getMessage()], 500);
}
}
public function getPlanInfo(Request $request)
{
$userId = USER_ID();
$actionType = strtoupper($request->input('action_type', 'NEW'));
$plansInput = $request->input('plans', []);
$ad_id = $request->input('ad_id', null);
if (!is_array($plansInput) || empty($plansInput)) {
return response()->json(['message' => 'No plans provided.'], 400);
}
$validPlans = [];
$errors = [];
foreach ($plansInput as $planEntry) {
$planId = $planEntry['plan_id'] ?? null;
if (!$planId) {
$errors[] = ['plan_id' => null, 'message' => 'Plan ID missing.'];
continue;
}
$plan = SubscriptionPlan::find($planId);
if (!$plan) {
$errors[] = ['plan_id' => $planId, 'message' => 'Plan not found.'];
continue;
}
$planType = $plan->type;
if ($actionType === 'NEW') {
$hasActive = UserSubscription::where('user_id', $userId)
->where('status', 'ACTIVE')
->whereHas('subscription', fn($q) => $q->where('type', $planType))
->exists();
if ($hasActive) {
$errors[] = ['plan_id' => $planId, 'message' => "Already active $planType plan."];
continue;
}
}
if ($actionType === 'UPGRADE') {
$active = UserSubscription::where('user_id', $userId)
->where('status', 'ACTIVE')
->whereHas('subscription', fn($q) => $q->where('type', $planType))
->first();
if (!$active) {
$errors[] = ['plan_id' => $planId, 'message' => "No active $planType plan to upgrade."];
continue;
}
}
if ($actionType === 'RENEW') {
$userSub = UserSubscription::where('user_id', $userId)
->where('subs_id', $planId)
->first();
if (!$userSub || $userSub->status !== 'EXPIRED') {
$errors[] = ['plan' => $plan->duration, 'type' => $plan->type, 'status' => $userSub->status, 'message' => 'Only expired plans can be renewed.'];
continue;
}
$plan = $userSub->subscription; // Use actual plan
}
$validPlans[] = $plan;
}
if (empty($validPlans)) {
return response()->json(['message' => 'No valid plans to process.', 'errors' => $errors], 400);
}
$allFree = collect($validPlans)->every(function ($plan) {
return ($plan->discount_amount ?? 0) == 0;
});
if ($allFree) {
$free = $this->setFreeSubscription($validPlans, $ad_id);
foreach ($free as $item) {
$plan = $item['plan'];
$start = $item['start_date'];
$end = $item['end_date'];
Notification::SetNotification(
$userId,
"New Subscription Activated",
"Your {$plan->type} plan has been successfully activated from {$start->format('d M Y')} to {$end->format('d M Y')}.",
"SUBSCRIPTION_VIEW",
$plan->id,
"1"
);
}
return $this->sendSuccess(trans('messages.SUBSCRIPTION_ACTIVATED'));
}
$existing = $this->checkPendingTransaction($userId, $validPlans, $actionType);
$trx = $existing ?: $this->generateTransaction($validPlans, $actionType);
return $this->sendSuccess(trans('messages.OK'), [
'trx_id' => $trx->uuid,
'order_id' => $trx->trx_id,
'amount' => $trx->grand_total,
'taxes' => $trx->tax_amount,
'tax_percent' => $trx->taxes,
'currency' => $trx->currency,
'razorpay_key' => config('services.razorpay.key'),
'plans' => $validPlans,
'errors' => $errors,
]);
}
private function checkPendingTransaction($userId, $planId, $type)
{
$pending = Transaction::where('user_id', $userId)
->where('trx_status', 'PENDING')
->where('type', $type)
->whereHas('plans', function ($query) use ($planId) {
$query->where('subs_plan_id', $planId);
})
->first();
if ($pending) {
// Check if older than 1 day
if ($pending->created_at->lt(now()->subDay())) {
$pending->delete();
return null;
}
}
return $pending;
}
public function getRenewPlanInfo($planId, Request $request)
{
$userSubscription = UserSubscription::with('subscription')->findOrFail($planId);
if (!$userSubscription->status == 'EXPIRED') {
return response()->json(['message' => 'Only expired plans can be renewed.'], 400);
}
return $this->generateTransaction($userSubscription->subscription->id, 'RENEW');
}
private function generateTransaction($plans, $type)
{
$trx = app(\App\Services\TransactionService::class)->generate([
'trx_type' => 'SUBSCRIPTION',
'type' => $type,
'plans' => $plans,
]);
return $trx;
}
private function setFreeSubscription($validPlans, $ad_id = null)
{
$userId = USER_ID();
$startDate = now();
$subscriptions = [];
foreach ($validPlans as $plan) {
$duration = $plan->duration;
$endDate = getEndDateByDuration($duration, $startDate);
$userSub = UserSubscription::create([
'user_id' => $userId,
'subs_id' => $plan->id,
'total_price' => $plan->discount_amount ?? 0,
'start_date' => $startDate,
'end_date' => $endDate,
'status' => 'ACTIVE',
]);
$subscriptions[] = [
'plan' => $plan,
'start_date' => $startDate,
'end_date' => $endDate,
'user_sub' => $userSub,
];
}
if ($ad_id) {
$ad = Advertisement::where('uuid', $ad_id)->first();
$ad->is_draft = false;
$ad->is_publish = true;
$ad->save();
}
return $subscriptions;
}
}