webseite funktioniert, pay sdk, blog backend funktioniert
This commit is contained in:
@@ -30,6 +30,13 @@ class StripeWebhookController extends Controller
|
||||
}
|
||||
|
||||
switch ($event['type']) {
|
||||
case 'checkout.session.completed':
|
||||
$session = $event['data']['object'];
|
||||
if ($session['mode'] === 'subscription' && isset($session['metadata']['subscription']) && $session['metadata']['subscription'] === 'true') {
|
||||
$this->handleSubscriptionStarted($session);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'payment_intent.succeeded':
|
||||
$paymentIntent = $event['data']['object'];
|
||||
$this->handlePaymentIntentSucceeded($paymentIntent);
|
||||
@@ -167,4 +174,74 @@ class StripeWebhookController extends Controller
|
||||
// TODO: Deactivate package or notify tenant
|
||||
// e.g., TenantPackage::where('provider_id', $subscription)->update(['active' => false]);
|
||||
}
|
||||
|
||||
private function handleSubscriptionStarted($session)
|
||||
{
|
||||
$metadata = $session['metadata'];
|
||||
if (!$metadata['user_id'] && !$metadata['tenant_id'] || !$metadata['package_id']) {
|
||||
Log::warning('Missing metadata in Stripe checkout session: ' . $session['id']);
|
||||
return;
|
||||
}
|
||||
|
||||
$userId = $metadata['user_id'] ?? null;
|
||||
$tenantId = $metadata['tenant_id'] ?? null;
|
||||
$packageId = $metadata['package_id'];
|
||||
$type = $metadata['type'] ?? 'reseller_subscription';
|
||||
|
||||
if ($userId && !$tenantId) {
|
||||
$tenant = \App\Models\Tenant::where('user_id', $userId)->first();
|
||||
if ($tenant) {
|
||||
$tenantId = $tenant->id;
|
||||
} else {
|
||||
Log::error('Tenant not found for user_id: ' . $userId);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$tenantId) {
|
||||
Log::error('No tenant_id found for Stripe checkout session: ' . $session['id']);
|
||||
return;
|
||||
}
|
||||
|
||||
$subscriptionId = $session['subscription']['id'] ?? null;
|
||||
if (!$subscriptionId) {
|
||||
Log::error('No subscription ID in checkout session: ' . $session['id']);
|
||||
return;
|
||||
}
|
||||
|
||||
// Activate TenantPackage for initial subscription
|
||||
\App\Models\TenantPackage::updateOrCreate(
|
||||
[
|
||||
'tenant_id' => $tenantId,
|
||||
'package_id' => $packageId,
|
||||
],
|
||||
[
|
||||
'purchased_at' => now(),
|
||||
'expires_at' => now()->addYear(),
|
||||
'active' => true,
|
||||
]
|
||||
);
|
||||
|
||||
// Create initial PackagePurchase
|
||||
\App\Models\PackagePurchase::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'package_id' => $packageId,
|
||||
'provider_id' => $subscriptionId,
|
||||
'price' => $session['amount_total'] / 100,
|
||||
'type' => $type,
|
||||
'purchased_at' => now(),
|
||||
'refunded' => false,
|
||||
]);
|
||||
|
||||
// Update tenant subscription fields if needed
|
||||
$tenant = \App\Models\Tenant::find($tenantId);
|
||||
if ($tenant) {
|
||||
$tenant->update([
|
||||
'subscription_id' => $subscriptionId,
|
||||
'subscription_status' => 'active',
|
||||
]);
|
||||
}
|
||||
|
||||
Log::info('Initial subscription activated via Stripe checkout session: ' . $session['id'] . ' for tenant ' . $tenantId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user