diff --git a/app/Http/Controllers/APIs/Customer_API/StripeWebhookController.php b/app/Http/Controllers/APIs/Customer_API/StripeWebhookController.php index 2e0e385..8258df1 100644 --- a/app/Http/Controllers/APIs/Customer_API/StripeWebhookController.php +++ b/app/Http/Controllers/APIs/Customer_API/StripeWebhookController.php @@ -2,13 +2,17 @@ namespace App\Http\Controllers\APIs\Customer_API; +use App\Helpers\onesignalhelper; use App\Http\Controllers\Controller; - +use App\Models\IamPrincipal; +use App\Models\SubscriptionProducts; +use App\Models\Subscriptions; use Illuminate\Http\Request; use Stripe\Event; use Stripe\Stripe; +use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\DB; @@ -16,24 +20,32 @@ class StripeWebhookController extends Controller { // + public function getWebhook(Request $request){ + // dd("ssssss",$request); Log::info('Stripe Webhook Received= in getWebhook '); Log::info('Stripe Webhook Received: ' . $request->getContent()); } - - public function handleWebhook(Request $request) { - Log::info('Stripe Webhook Received: ' . $request->getContent()); + Log::info("webhook At line 1"); + + // Verify the webhook signature for security + $secret = config('constants.subscription.webhook_secret'); // Your webhook secret key $payload = $request->getContent(); $sigHeader = $request->header('Stripe-Signature'); $event = null; + try { $event = Event::constructFrom( - json_decode($payload, true) + json_decode($payload, true), + $sigHeader, + config('constants.subscription.webhook_secret') ); + + } catch (\UnexpectedValueException $e) { // Invalid payload return response()->json(['error' => 'Invalid payload'], 400); @@ -41,23 +53,97 @@ class StripeWebhookController extends Controller // Signature verification failed return response()->json(['error' => 'Signature verification failed'], 400); } + // $stripeSecret = config('services.stripe.key'); + + $stripeSecret = (config('constants.subscription.stripe_secret_key')); + + + + + Log::info("webhook called"); + $stripe = new \Stripe\StripeClient($stripeSecret); if ($event->type === 'checkout.session.completed') { - $session = $event->data->object; - $metadata = $session->metadata; - Log::info('Meta data ' . json_encode($metadata)); + try { + + DB::beginTransaction(); + // Handle successful subscription payment + // You can access event data like $event->data->object + + // Session::flush(); + $session = $event->data->object; + + $metadata = $session->metadata; + if ($metadata == null || empty($metadata)) { + return response('Webhook Metadata received at null ', 200); + } + $userId = $metadata->userId; + $userEmail = $metadata->userEmail; + $subscriptionProductId = $metadata->subscriptionProductXid; + + $subscriptionProductData = SubscriptionProducts::where('id', $subscriptionProductId)->first(); + + //checkout store in db + $subscriptionData = $stripe->checkout->sessions->retrieve($session->id, []); + $SubscriptionObject = $stripe->subscriptions->retrieve($subscriptionData->subscription, []); + $priceObject = $stripe->prices->retrieve($SubscriptionObject->plan->id, []); + + $amountSubtotalDollars = $subscriptionData->amount_total / 100; + // Log::info('Subscription has been started '); - $stripeSecret = (config('constants.subscription.stripe_secret_key')); - // $stripe = new StripeClient($stripeSecret); - $stripe = new \Stripe\StripeClient($stripeSecret); + $subscriptionObjectFromInvoice = $stripe->subscriptions->retrieve($subscriptionData->subscription, []); + + $upcoming_invoice = $stripe->invoices->upcoming([ + 'subscription' => $subscriptionData->subscription, // use retrieved id from subscription + ]); + + $id = Subscriptions::updateOrCreate( + ['iam_principal_xid' => $userId, 'subscription_product_xid' => $subscriptionProductId], + [ + 'subscription_id' => $subscriptionData->subscription, + 'amount' => $amountSubtotalDollars, + 'stripe_customer_id' => $subscriptionData->customer, + 'subscription_status' => $subscriptionObjectFromInvoice->status, + 'current_period_start' => date('Y-m-d H:i:s', $SubscriptionObject->current_period_start), + 'current_period_end' => date('Y-m-d H:i:s', $SubscriptionObject->current_period_end), + + 'status' => 'complete', + 'next_payment_date' => date('Y-m-d H:i:s', $upcoming_invoice->next_payment_attempt) + + ] + ); + + $getUserData = IamPrincipal::where('id', $userId)->first(); + + $title = "Congratulations you subscription is now active"; + $message = $getUserData->first_name . " has subscribed for " . $subscriptionProductData->_name . " subscription"; + $content_type = "new_subscription"; + + onesignalhelper::sendNotificationApi($getUserData->one_signal_player_id, $title, $message, $content_type, $image = null, $id = null); + + + Log::info('Subscription Taken Successfully by '); + DB::commit(); + } catch (\Exception $e) { + Log::error("An error occurred in " . __METHOD__ . ": " . $e->getMessage()); + // return response()->json(['error' => __('something_went_wrong')], 500); + Log::error('Customer Subscription Checkout session function failed: ' . $e->getMessage()); + DB::rollBack(); + + + } + //end } - return response()->json(['status' => 'Webhook received']); + + + + + + return response('Webhook received', 200); } - - } \ No newline at end of file diff --git a/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php b/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php index f69d5f0..c91b757 100644 --- a/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php +++ b/app/Http/Controllers/APIs/Customer_API/SubscriptionController.php @@ -27,14 +27,14 @@ class SubscriptionController extends Controller { try { // dd($request->header('access-token')); - // $token = readHeaderToken(); + $token = readHeaderToken(); // dd("acc",$token); - $token = true; + // $token = true; // dd($token, Session::get('vendorToken')); if ($token) { - $user_id = 12; - // $user_id = $token['sub']; + // $user_id = 12; + $user_id = $token['sub']; $dateTime = now(); $formattedDateTime = $dateTime->format('Y-m-d H:i:s'); @@ -93,6 +93,8 @@ class SubscriptionController extends Controller public function createStripeProduct(Request $request) { + + try { DB::beginTransaction(); @@ -174,7 +176,7 @@ class SubscriptionController extends Controller //created by; Hritik //On - 02th July ,2024 - //use - to Create Subscription + //use - to Create Subscription public function subscriptionToPlan(Request $request) { @@ -217,7 +219,7 @@ class SubscriptionController extends Controller 'subscriptionProductXid' => $subscriptionProductXid ], 'success_url' => route('thankyou'), - + // 'cancel_url' => "http://localhost/cheerstothe_season_2_o/my-subscription-page", ]); diff --git a/app/Http/Controllers/Admin/ManageNotificationsController.php b/app/Http/Controllers/Admin/ManageNotificationsController.php index cf1a2ad..f60bcfe 100644 --- a/app/Http/Controllers/Admin/ManageNotificationsController.php +++ b/app/Http/Controllers/Admin/ManageNotificationsController.php @@ -81,78 +81,94 @@ class ManageNotificationsController extends Controller * Use : To add notification . */ public function store_notificaton_data(Request $request) -{ - try { - $request->validate([ - 'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', - ]); + { + try { + $request->validate([ + 'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', + ]); - DB::beginTransaction(); + DB::beginTransaction(); - if (isset($request->image)) { - $image = $request->image; - $image_db = null; - } else { - $image = null; - $image_db = $request->image; - } - - $tnormalImage = saveSingleImageWithoutCrop($image, 'notification_images', $image_db); - $imagePath = ListingImageUrl('notification_images', $tnormalImage); - - // Fetch OneSignal IDs based on the selected states and user type - $states = $request->states; - - $userQuery = IamPrincipal::where('is_active', 1) - ->where('notification_status', 1) - ->where('principal_type_xid', 3) - ->whereIn('state_xid', $states); - - - if ($request->user_type == 1) { - $allCustomerOneSignalIds = $userQuery->pluck('id'); - $UserData = IamPrincipal::whereIn('id', $allCustomerOneSignalIds)->get(); - - foreach ($UserData as $customerIdItem) { - if ($customerIdItem->one_signal_player_id) { - onesignalhelper::sendNotificationApi( - $customerIdItem->one_signal_player_id, - $request->title, - $request->description, - 'Dashboard Notification', - $imagePath, - $id = null - ); - } - onesignalhelper::StoreNotificationDetails($customerIdItem->id, 'Notification', $request->title, $imagePath); + if (isset($request->image)) { + $image = $request->image; + $image_db = null; + } else { + $image = null; + $image_db = $request->image; } - } elseif ($request->user_type == 2) { - $allRestaurantOneSignalIds = $userQuery->pluck('id'); - $restaurantData = IamPrincipal::whereIn('id', $allRestaurantOneSignalIds)->get(); - foreach ($restaurantData as $restIdItem) { - if ($restIdItem->one_signal_player_id) { - onesignalhelper::sendNotificationApi( - $restIdItem->one_signal_player_id, - $request->title, - $request->description, - 'Dashboard Notification', - $imagePath, - $id = null - ); + $tnormalImage = saveSingleImageWithoutCrop($image, 'notification_images', $image_db); + $imagePath = ListingImageUrl('notification_images', $tnormalImage); + + $states = $request->states; + + $userQuery = IamPrincipal::where('is_active', 1) + ->where('notification_status', 1) + ->where('principal_type_xid', 3) + ->whereIn('state_xid', $states); + + if ($request->user_type == 1) { + $allCustomerOneSignalIds = $userQuery->pluck('id'); + $UserData = IamPrincipal::whereIn('id', $allCustomerOneSignalIds)->get(); + + foreach ($UserData as $customerIdItem) { + if ($customerIdItem->one_signal_player_id) { + onesignalhelper::sendNotificationApi( + $customerIdItem->one_signal_player_id, + $request->title, + $request->description, + 'Dashboard Notification', + $imagePath, + $id = null + ); + } + onesignalhelper::StoreNotificationDetails($customerIdItem->id, 'Notification', $request->title, $imagePath); } - onesignalhelper::StoreNotificationDetails($restIdItem->id, 'Notification', $request->title, $imagePath); - } - } + } elseif ($request->user_type == 2) { + $allRestaurantOneSignalIds = $userQuery->pluck('id'); + $restaurantData = IamPrincipal::whereIn('id', $allRestaurantOneSignalIds)->get(); - DB::commit(); - return jsonResponseWithSuccessMessage(__('success.save_data')); - } catch (Exception $e) { - DB::rollBack(); - Log::error("Notification send Failed " . $e->getMessage()); - return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); + foreach ($restaurantData as $restIdItem) { + if ($restIdItem->one_signal_player_id) { + onesignalhelper::sendNotificationApi( + $restIdItem->one_signal_player_id, + $request->title, + $request->description, + 'Dashboard Notification', + $imagePath, + $id = null + ); + } + onesignalhelper::StoreNotificationDetails($restIdItem->id, 'Notification', $request->title, $imagePath); + } + } elseif ($request->user_type == 3) { + $allUserOneSignalIds = $userQuery->pluck('id'); + $UserData = IamPrincipal::whereIn('id', $allUserOneSignalIds)->get(); + + foreach ($UserData as $userItem) { + if ($userItem->one_signal_player_id) { + onesignalhelper::sendNotificationApi( + $userItem->one_signal_player_id, + $request->title, + $request->description, + 'Dashboard Notification', + $imagePath, + $id = null + ); + } + onesignalhelper::StoreNotificationDetails($userItem->id, 'Notification', $request->title, $imagePath); + } + } + + DB::commit(); + return jsonResponseWithSuccessMessage(__('success.save_data')); + } catch (Exception $e) { + DB::rollBack(); + Log::error("Notification send Failed " . $e->getMessage()); + return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); + } } -} + diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 569dc4b..b5c64f6 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -34,7 +34,7 @@ class Kernel extends HttpKernel \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, - \App\Http\Middleware\VerifyCsrfToken::class, + // \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php index 98c2c13..3736564 100644 --- a/app/Http/Middleware/VerifyCsrfToken.php +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -13,6 +13,8 @@ class VerifyCsrfToken extends Middleware */ protected $except = [ 'stripe/webhook', - 'stripe-webhooks' + 'stripe-webhooks', + '/stripe/webhook', + '/stripe-webhooks' ]; } diff --git a/resources/views/Admin/pages/manage_notification/manage_notifications_add.blade.php b/resources/views/Admin/pages/manage_notification/manage_notifications_add.blade.php index b7d5484..d71ef1b 100644 --- a/resources/views/Admin/pages/manage_notification/manage_notifications_add.blade.php +++ b/resources/views/Admin/pages/manage_notification/manage_notifications_add.blade.php @@ -71,13 +71,19 @@ users who are in their respective states
@@ -77,7 +79,7 @@
--> -
+ {{--

Subscription FAQs

@@ -93,7 +95,7 @@ @endforeach
-
+
--}}