146 lines
5.3 KiB
PHP
146 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
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 App\Models\API\SubscriptionPayModel;
|
|
use Stripe\Event;
|
|
use Stripe\Stripe;
|
|
|
|
use App\Models\API\PatientModel;
|
|
use App\Models\API\PatientCareGiverLinkModel;
|
|
use App\Models\API\CareGiverModel;
|
|
|
|
use App\Models\API\SubscriptionModel;
|
|
// /var/www/html/simplitend/app/Http/Controllers/API/;
|
|
|
|
use App\Http\Helpers\MyHelper;
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class StripeWebhookController extends Controller
|
|
{
|
|
//
|
|
public function handleWebhook(Request $request)
|
|
{
|
|
|
|
|
|
// Verify the webhook signature for security
|
|
$secret = config('constant.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),
|
|
$sigHeader,
|
|
config('constant.subscription.webhook_secret')
|
|
);
|
|
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
// Invalid payload
|
|
return response()->json(['error' => 'Invalid payload'], 400);
|
|
} catch (\Stripe\Exception\SignatureVerificationException $e) {
|
|
// Signature verification failed
|
|
return response()->json(['error' => 'Signature verification failed'], 400);
|
|
}
|
|
$stripeSecret = config('services.stripe.key');
|
|
|
|
|
|
|
|
$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 ');
|
|
|
|
|
|
|
|
$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 ' . $getCaregiverData->iamprincipal->user_name);
|
|
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);
|
|
}
|
|
} |