webhook route reforma code added
This commit is contained in:
@@ -21,24 +21,16 @@ class StripeWebhookController extends Controller
|
||||
//
|
||||
public function handleWebhook(Request $request)
|
||||
{
|
||||
Log::info("webhook At line 1");
|
||||
|
||||
// Verify the webhook signature for security
|
||||
$secret = config('constants.subscription.webhook_secret'); // Your webhook secret key
|
||||
Log::info('Stripe Webhook Received: ' . $request->getContent());
|
||||
|
||||
$payload = $request->getContent();
|
||||
$sigHeader = $request->header('Stripe-Signature');
|
||||
$event = null;
|
||||
|
||||
|
||||
try {
|
||||
$event = Event::constructFrom(
|
||||
json_decode($payload, true),
|
||||
$sigHeader,
|
||||
config('constants.subscription.webhook_secret')
|
||||
json_decode($payload, true)
|
||||
);
|
||||
|
||||
|
||||
} catch (\UnexpectedValueException $e) {
|
||||
// Invalid payload
|
||||
return response()->json(['error' => 'Invalid payload'], 400);
|
||||
@@ -46,97 +38,23 @@ 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') {
|
||||
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 ');
|
||||
$session = $event->data->object;
|
||||
$metadata = $session->metadata;
|
||||
Log::info('Meta data ' . json_encode($metadata));
|
||||
|
||||
|
||||
|
||||
$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);
|
||||
$stripeSecret = (config('constants.subscription.stripe_secret_key'));
|
||||
|
||||
// $stripe = new StripeClient($stripeSecret);
|
||||
$stripe = new \Stripe\StripeClient($stripeSecret);
|
||||
|
||||
|
||||
|
||||
|
||||
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('Webhook received', 200);
|
||||
return response()->json(['status' => 'Webhook received']);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -32,7 +32,7 @@ class onesignalhelper
|
||||
];
|
||||
|
||||
$result = OneSignal::sendPush($fields, null ,env('ONE_SIGNAL_APP_ID'));
|
||||
Log::info($result);
|
||||
// Log::info($result);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ class CheckStatus
|
||||
{
|
||||
|
||||
$admin = auth()->guard('admin')->user();
|
||||
Log::info($admin);
|
||||
// Log::info($admin);
|
||||
if ($admin && $admin->is_active == 1 && ($admin->principal_type_xid == 1 || $admin->principal_type_xid == 2)) {
|
||||
return $next($request);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user