Restspecific

This commit is contained in:
sayaliparab
2024-07-03 19:31:08 +05:30
9 changed files with 275 additions and 114 deletions

View File

@@ -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);
}
}

View File

@@ -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",
]);

View File

@@ -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);
}
}
}

View File

@@ -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,
],

View File

@@ -13,6 +13,8 @@ class VerifyCsrfToken extends Middleware
*/
protected $except = [
'stripe/webhook',
'stripe-webhooks'
'stripe-webhooks',
'/stripe/webhook',
'/stripe-webhooks'
];
}

View File

@@ -71,13 +71,19 @@
users who are in their respective states</label><br>
<div id="dropdown-1"
style="display: none; max-height: 200px; overflow-y: auto;">
<div class="form-check">
<input class="form-check-input select-all-checkbox" type="checkbox"
id="select-all-1" />
<label class="form-check-label" for="select-all-1">Select All
States</label>
</div>
@foreach ($state as $states)
<div class="form-check">&nbsp;
<input class="form-check-input" type="checkbox" name="states[]"
value="{{ $states['id'] }}"
id="state-{{ $states['id'] }}" />
<div class="form-check">
<input class="form-check-input state-checkbox" type="checkbox"
name="states[]" value="{{ $states['id'] }}"
id="state-1-{{ $states['id'] }}" />
<label class="form-check-label"
for="state-{{ $states['id'] }}">{{ $states['name'] }}</label>
for="state-1-{{ $states['id'] }}">{{ $states['name'] }}</label>
</div>
@endforeach
</div>
@@ -87,13 +93,41 @@
users who are in their respective states</label><br>
<div id="dropdown-2"
style="display: none; max-height: 200px; overflow-y: auto;">
@foreach ($state as $states)
<div class="form-check">&nbsp;
<input class="form-check-input" type="checkbox" name="states[]"
value="{{ $states['id'] }}"
id="state-{{ $states['id'] }}" />
<div class="form-check">
<input class="form-check-input select-all-checkbox" type="checkbox"
id="select-all-2" />
<label class="form-check-label" for="select-all-2">Select All
States</label>
</div>
@foreach ($state as $states)
<div class="form-check">
<input class="form-check-input state-checkbox" type="checkbox"
name="states[]" value="{{ $states['id'] }}"
id="state-2-{{ $states['id'] }}" />
<label class="form-check-label"
for="state-{{ $states['id'] }}">{{ $states['name'] }}</label>
for="state-2-{{ $states['id'] }}">{{ $states['name'] }}</label>
</div>
@endforeach
</div>
<input class="form-check-input" type="radio" name="user_type"
id="select-ids" value="3" />
<label class="form-check-label" for="select-ids">Send to subscribed and
unsubscribe users who are in their respective states</label><br>
<div id="dropdown-3"
style="display: none; max-height: 200px; overflow-y: auto;">
<div class="form-check">
<input class="form-check-input select-all-checkbox"
type="checkbox" id="select-all-3" />
<label class="form-check-label" for="select-all-3">Select All
States</label>
</div>
@foreach ($state as $states)
<div class="form-check">
<input class="form-check-input state-checkbox" type="checkbox"
name="states[]" value="{{ $states['id'] }}"
id="state-3-{{ $states['id'] }}" />
<label class="form-check-label"
for="state-3-{{ $states['id'] }}">{{ $states['name'] }}</label>
</div>
@endforeach
</div>
@@ -216,9 +250,11 @@
function handleUserTypeChange() {
var dropdown1 = document.getElementById('dropdown-1');
var dropdown2 = document.getElementById('dropdown-2');
var dropdown3 = document.getElementById('dropdown-3');
dropdown1.style.display = this.value === '1' ? 'block' : 'none';
dropdown2.style.display = this.value === '2' ? 'block' : 'none';
dropdown3.style.display = this.value === '3' ? 'block' : 'none';
}
var userTypeRadios = document.getElementsByName('user_type');
@@ -230,6 +266,19 @@
if (checkedRadio) {
handleUserTypeChange.call(checkedRadio);
}
function handleSelectAllChange() {
var dropdown = this.closest('div[id^="dropdown-"]');
var checkboxes = dropdown.querySelectorAll('.state-checkbox');
for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = this.checked;
}
}
var selectAllCheckboxes = document.querySelectorAll('.select-all-checkbox');
for (var i = 0; i < selectAllCheckboxes.length; i++) {
selectAllCheckboxes[i].addEventListener('change', handleSelectAllChange);
}
});
</script>
@endsection

View File

@@ -7,7 +7,7 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="{{ asset('resources/views/Admin/pages/subscriptions/subscription-style.css') }}" />
<title>My Subscription</title>
</head>
@@ -27,11 +27,13 @@
</div>
<div class="inn-group my-4">
<label class="comm-head" for="">Subscription Charge</label>
<input type="text" class="form-control other-input mt-3" value="$5000">
<input type="text" class="form-control other-input mt-3" value="{{$isSubscribedUser->amount}}">
</div>
<div class="inn-group my-4">
<label class="comm-head" for="">Renewal date</label>
<input type="text" class="form-control other-input mt-3" value="September 15th, 2024">
<input type="text" class="form-control other-input mt-3" value="{{
\Carbon\Carbon::parse($isSubscribedUser->next_payment_date)->format('j M, Y') }}">
</div>
</div>
<div class="my-5">
@@ -77,7 +79,7 @@
</div>
</div>
</section> -->
<section class="mt-3 mb-5">
{{-- <section class="mt-3 mb-5">
<div class="container">
<h3 class="head my-4">Subscription FAQs</h3>
<div data-ui-tablist class="ui-accordion ui-accordion--outlined" data-ui-transition="collapse-fade">
@@ -93,7 +95,7 @@
@endforeach
</div>
</div>
</section>
</section> --}}
<!-- Modal start -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
@@ -107,8 +109,7 @@
</button>
</div>
<div class="modal-body">
<p class="text-center para">If you cancel now, you can still access your subscription until September
15th, 2024. After this date, you will no longer be able to redeem cocktails.
<p class="text-center para">If you cancel now, you can still access your subscription until {{\Carbon\Carbon::now()->format('M d ,Y')}} After this date, you will no longer be able to redeem cocktails.
</p>
<p class="text-center mt-3 para">Are you sure you want to cancel?
</p>
@@ -167,4 +168,4 @@
</body>
</html>
</html>

View File

@@ -9,9 +9,16 @@ use App\Http\Controllers\APIs\Customer_API\NotificationController;
use App\Http\Controllers\APIs\Customer_API\RestaurantControllerApi;
use App\Http\Controllers\APIs\Customer_API\RulesControllerAPI;
use App\Http\Controllers\Admin\ReferralCodeController;
use App\Http\Controllers\APIs\Customer_API\StripeWebhookController;
use App\Http\Controllers\APIs\Customer_API\SubscriptionController;
use Illuminate\Support\Facades\Route;
// Route::post('/v1/stripe/webhook', [StripeWebhookController::class, 'getWebhook']);
Route::post('/v1/stripe-webhook', [StripeWebhookController::class, 'handleWebhook']);
Route::post('/v1/stripe/webhook', [StripeWebhookController::class, 'getWebhook']);
Route::middleware(['customerApiBasicAuth'])->group(function () {

View File

@@ -35,10 +35,10 @@ use App\Http\Controllers\Admin\ManageRulesController;
// Route::post('/stripe/webhook', [StripeWebhookController::class, 'handleWebhook']);
// Route::post('/stripe-webhooks', [StripeWebhookController::class, 'getWebhook']);
Route::middleware('webhook')->group(function () {
Route::post('/stripe/webhook', [StripeWebhookController::class, 'handleWebhook']);
Route::post('/stripe-webhooks', [StripeWebhookController::class, 'getWebhook']);
});
// Route::middleware('webhook')->group(function () {
// Route::post('/stripe/webhook', [StripeWebhookController::class, 'handleWebhook']);
// Route::post('/stripe-webhooks', [StripeWebhookController::class, 'getWebhook']);
// });
//stripe webhook end
@@ -172,8 +172,6 @@ Route::group(['middleware' => ['checkStatus']], function () {
//*******************************************************manage reports********************************************************
Route::get('/manage-reports', [ManageReportsController::class, 'index'])->name('manage.reports');
Route::post('/export-reports', [ManageReportsController::class, 'exportReports'])->name('export.reports');
//*******************************************************manage feedback********************************************************
Route::get('/manage-feedback', [ManageFeedbackController::class, 'index'])->name('manage.feedback');
Route::post('/delete_feedback/{id}', [ManageFeedbackController::class, 'delete_feedback'])->name('delete.feedback');
@@ -220,10 +218,10 @@ Route::group(['middleware' => ['checkStatus']], function () {
//subscription ROutes
// Route::group(['middleware' => ['customer.jwt.verify']], function () {
Route::group(['middleware' => ['customer.jwt.verify']], function () {
Route::get('my-subscription-page', [SubscriptionController::class, 'mySubscription'])->name('my-subscription-page');
// });
});