Merge branch 'main' of https://github.com/WDI-Ideas/cheerstothe_season_laravel11 into sayli
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -96,6 +96,8 @@ class SubscriptionController extends Controller
|
||||
|
||||
public function createStripeProduct(Request $request)
|
||||
{
|
||||
|
||||
|
||||
try {
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
@@ -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,
|
||||
],
|
||||
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
@@ -31,14 +31,14 @@ use App\Http\Controllers\Admin\ManageRulesController;
|
||||
|
||||
|
||||
//webhook
|
||||
Route::post('/stripe-webhook', [StripeWebhookController::class, 'handleWebhook']);
|
||||
Route::post('/stripe/webhook', [StripeWebhookController::class, 'handleWebhook']);
|
||||
// Route::post('/stripe-webhook', [StripeWebhookController::class, 'handleWebhook']);
|
||||
// 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');
|
||||
|
||||
Reference in New Issue
Block a user