340 lines
12 KiB
PHP
340 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\APIs\Customer_API;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\APIs\CustomerAPIs\RulesApiServices;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Http\Request;
|
|
|
|
class SubscriptionController extends Controller
|
|
{
|
|
|
|
|
|
//created by; Hritik
|
|
//On - 28th June ,2024
|
|
//use - to get Data of User in Webview and show list of product
|
|
public function mySubscription(Request $request)
|
|
{
|
|
try {
|
|
|
|
|
|
|
|
return view('Admin.pages.subscriptions.my-subscription');
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
|
|
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
//created by; Hritik
|
|
//On - 28th June ,2024
|
|
//use - to get Data of User in Webview and show list of product
|
|
|
|
|
|
public function listOfProduct(Request $request)
|
|
{
|
|
try {
|
|
|
|
|
|
|
|
return view('Admin.pages.subscriptions.list-of-products');
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
|
|
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//created by; Hritik
|
|
//On - 18th May ,2024
|
|
//use - to get Data of Detailed List Of Product , Monthly & Yearly
|
|
public function nextPage(Request $request)
|
|
{
|
|
|
|
try {
|
|
// $token = readHeaderToken();
|
|
$token = true;
|
|
// dd($request);
|
|
if ($token) {
|
|
// $user_id = $token['sub'];
|
|
|
|
// $validator = $this->validateSubscriptionsForm($request);
|
|
|
|
// if ($validator->fails()) {
|
|
// $validationErrors = $validator->errors()->all();
|
|
// Log::error("Razorpay subscriptions plan validation error: " . implode(", ", $validationErrors));
|
|
// return jsonResponseWithErrorMessageApi($validationErrors, 203);
|
|
// }
|
|
|
|
// $request['iam_principal_id'] = $user_id;
|
|
$productTypeXid = $request->query('selected_product_type_id');
|
|
|
|
$userId = $request->query('user_id');
|
|
$products = Products::with('productType', 'productTier')
|
|
->where('product_type_xid', $productTypeXid)
|
|
->where('is_active', '1')->get();
|
|
|
|
foreach ($products as $k => $val) {
|
|
|
|
$products[$k]['product_image'] = ListingImageUrl('product_image', $val['product_image']);
|
|
}
|
|
|
|
$user = IamPrincipal::where('id', $userId)->where('is_active', 1)->first();
|
|
|
|
$user_name = $user->user_name;
|
|
$user_contact_no = $user->phone_number;
|
|
$user_email = $user->email_address;
|
|
|
|
|
|
return view(
|
|
'admin-dashboard.pages.razorpay-subscription.subscription-product-detail-pay',
|
|
compact(
|
|
'products',
|
|
'userId',
|
|
'user_name',
|
|
'user_contact_no',
|
|
'user_email',
|
|
|
|
)
|
|
);
|
|
} else {
|
|
return jsonResponseWithErrorMessageApi(__('auth.user_deleted'), 409);
|
|
}
|
|
} catch (Exception $e) {
|
|
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
|
|
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
//created by :-Hritik
|
|
// On - 16th may ,2024
|
|
//subscription to a particular project based on plans and returning the subscription id to web to open Subscription screen
|
|
public function subscribeToProduct(Request $request)
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
// $token = readHeaderToken();
|
|
|
|
$iamId = $request->iam_principal_xid;
|
|
$razorPayPlanId = $request->razorpay_plan_id;
|
|
$productId = $request->product_id;
|
|
|
|
|
|
$user = IamPrincipal::where('id', $iamId)->where('is_active', 1)->first();
|
|
$id = $user->id;
|
|
$phone = $user->phone_number;
|
|
$email = $user->email_address;
|
|
|
|
$product = Products::where('razorpay_plan_id', $razorPayPlanId)
|
|
->where('is_active', 1)
|
|
->first();
|
|
|
|
if ($product && $product->product_tier_xid == 1) {
|
|
//if product tier is monthly then we are setting count to 1200 means 100 years
|
|
$total_count = 1200;
|
|
} else if ($product && $product->product_tier_xid == 2) {
|
|
//if product tier is Yearly then we are setting count to 100 means 100 years
|
|
|
|
$total_count = 100;
|
|
} else {
|
|
|
|
//if product tier is not found then we are setting to 1
|
|
|
|
$total_count = 1;
|
|
}
|
|
|
|
|
|
$api = new Api(config('constants.razorpay.key_id'), config('constants.razorpay.key_secret'));
|
|
$plan_id = $product->razorpay_plan_id;
|
|
|
|
// Call the subscription create method with request parameters
|
|
$response = $api->subscription->create([
|
|
'plan_id' => $plan_id,
|
|
'total_count' => $total_count,
|
|
'quantity' => '1',
|
|
|
|
'notes' => [
|
|
'product_id' => intval($product->id),
|
|
'iam_principal_xid' => intval($iamId)
|
|
],
|
|
'notify_info' => [
|
|
'notify_phone' => $phone,
|
|
'notify_email' => $email
|
|
],
|
|
// 'callback_url' => route('my-subscription-page')
|
|
]);
|
|
|
|
$notes = $response->notes;
|
|
$notesArray = [
|
|
'product_id' => $notes->product_id,
|
|
'iam_principal_xid' => $notes->iam_principal_xid,
|
|
];
|
|
|
|
DB::commit();
|
|
|
|
// return response()->json($response);
|
|
|
|
return response()->json([
|
|
'subId' => $response->id,
|
|
'userId' => $iamId,
|
|
|
|
|
|
]);
|
|
return jsonResponseWithSuccessMessage(__('success.save_data', $response->short_url));
|
|
|
|
// return Redirect::away($response->short_url);
|
|
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
|
|
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
|
|
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
|
|
public function validateSubscriptionsForm(Request $request)
|
|
{
|
|
return Validator::make(
|
|
$request->all(),
|
|
[
|
|
'plan_id' => 'required',
|
|
]
|
|
);
|
|
}
|
|
|
|
public function thankyou(Request $request)
|
|
{
|
|
$userId = $request->query('user_xid');
|
|
if ($userId) {
|
|
//store data in DATABSE
|
|
|
|
RazorpayWebhookUsers::updateOrCreate(
|
|
['iam_principal_xid' => $userId],
|
|
['isSuccessPayment' => 1]
|
|
);
|
|
}
|
|
return view('admin-dashboard.pages.razorpay-subscription.thankyou');
|
|
}
|
|
|
|
public function cancelThankYou(Request $request)
|
|
{
|
|
$endAt = $request->query('end_date');
|
|
if ($endAt) {
|
|
$endDate = $endAt;
|
|
} else {
|
|
$endDate = null;
|
|
}
|
|
return view('admin-dashboard.pages.razorpay-subscription.cancel-thank-you', compact('endDate'));
|
|
}
|
|
|
|
|
|
|
|
|
|
//created by; Hritik
|
|
//On - 28th May ,2024
|
|
//use - To cancel the subscription of user on end of subscription end period
|
|
|
|
public function cancelSubscription(Request $request)
|
|
{
|
|
try {
|
|
Log::info("Razorpay Cancel subscriptions Begin: ");
|
|
DB::beginTransaction();
|
|
|
|
$validator = $this->validateCancelSubscriptionForm($request);
|
|
|
|
if ($validator->fails()) {
|
|
$validationErrors = $validator->errors()->all();
|
|
Log::error("Razorpay Cancel subscriptions validation error: " . implode(", ", $validationErrors));
|
|
return jsonResponseWithErrorMessageApi($validationErrors, 203);
|
|
}
|
|
|
|
$iamPrincipalId = $request->iam_principal_xid;
|
|
$razorpaySubscriptionId = $request->subscription_id;
|
|
$subscriptionXid = $request->subscription_xid;
|
|
|
|
$getSubscriptionData = RazorpaySubscriptions::where(['id' => $subscriptionXid, 'iam_principal_xid' => $iamPrincipalId, 'subscription_id' => $razorpaySubscriptionId, 'isCancelledSubscription' => 0])->first();
|
|
|
|
|
|
if (!$getSubscriptionData) {
|
|
return response()->json(['status' => 502, 'message' => 'Something went wrong while cancelling Subscription']);
|
|
}
|
|
|
|
$api = new Api(config('constants.razorpay.key_id'), config('constants.razorpay.key_secret'));
|
|
|
|
$options = ['cancel_at_cycle_end' => 1];
|
|
// Call the subscription create method with request parameters
|
|
$response = $api->subscription->fetch($razorpaySubscriptionId)->cancel($options);
|
|
|
|
$endAt = date('Y-m-d H:i:s', $response->end_at);
|
|
$dateTime = now();
|
|
$currentDateTime = $dateTime->format('Y-m-d H:i:s');
|
|
$getSubscriptionData->isCancelledSubscription = 1;
|
|
$getSubscriptionData->cancelled_at = $currentDateTime;
|
|
$getSubscriptionData->save();
|
|
|
|
DB::commit();
|
|
Log::info("Razorpay Cancel subscriptions end: ");
|
|
|
|
return response()->json(['status' => 200, 'end_date' => $endAt]);
|
|
// return response()->json(['status' => 200, 'message' => 'Your Subscription has been cancelled Sucessfully']);
|
|
|
|
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage(), ['exception' => $e]);
|
|
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
|
|
}
|
|
}
|
|
public function validateCancelSubscriptionForm(Request $request)
|
|
{
|
|
return Validator::make(
|
|
$request->all(),
|
|
[
|
|
'iam_principal_xid' => 'required',
|
|
'subscription_id' => 'required',
|
|
'subscription_xid' => 'required',
|
|
]
|
|
);
|
|
}
|
|
|
|
|
|
// public function subscriptionUpgrade(Request $request)
|
|
// {
|
|
// // dd($request->all());
|
|
|
|
// $razorPayPlanId = $request->razorpay_plan_id;
|
|
// $productTypeId = $request->product_type_xid;
|
|
// $productTierId = $request->product_tier_xid;
|
|
// $iamId = $request->iam_principal_xid;
|
|
|
|
// $user_id = 12; // Change this to dynamic user ID
|
|
|
|
// // Retrieve the current monthly subscription
|
|
// $currentSubscription = RazorpaySubscriptions::where('iam_principal_xid', $user_id)->first();
|
|
|
|
// if (!$currentSubscription) {
|
|
// return response()->json(['status' => 502, 'message' => 'Something went wrong current user Subscription not found']);
|
|
// }
|
|
|
|
// $products = Products::where('product_type_xid', $productTypeId)
|
|
// ->where('product_tier_xid', $productTierId)
|
|
// ->where('razorpay_plan_id', $razorPayPlanId)
|
|
// ->where('is_active', '1')->first();
|
|
|
|
// if (!$products) {
|
|
// return response()->json(['status' => 502, 'message' => 'Something went wrong current product not found']);
|
|
// }
|
|
|
|
// $api = new Api(config('constants.razorpay.key_id'), config('constants.razorpay.key_secret'));
|
|
// $plan_id = $products->razorpay_plan_id;
|
|
|
|
// // Call the subscription create method with request parameters
|
|
|
|
// }
|
|
}
|