85 lines
2.7 KiB
PHP
85 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Stripe\Stripe;
|
|
use Stripe\PaymentIntent;
|
|
use App\Models\Package;
|
|
use App\Models\Tenant;
|
|
|
|
class StripePaymentController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
Stripe::setApiKey(config('services.stripe.secret'));
|
|
}
|
|
|
|
public function createPaymentIntent(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'package_id' => 'required|integer|exists:packages,id',
|
|
]);
|
|
|
|
$user = Auth::user();
|
|
if (!$user) {
|
|
return response()->json(['error' => 'Nicht authentifiziert'], 401);
|
|
}
|
|
|
|
$tenant = $user->tenant;
|
|
if (!$tenant) {
|
|
return response()->json(['error' => 'Kein Tenant gefunden'], 403);
|
|
}
|
|
|
|
$package = Package::findOrFail($request->package_id);
|
|
|
|
// Kostenlose Pakete brauchen kein Payment Intent
|
|
if ($package->price <= 0) {
|
|
return response()->json([
|
|
'type' => 'free',
|
|
'message' => 'Kostenloses Paket - kein Payment Intent nötig'
|
|
]);
|
|
}
|
|
|
|
try {
|
|
$paymentIntent = PaymentIntent::create([
|
|
'amount' => (int)($package->price * 100), // In Cent
|
|
'currency' => 'eur',
|
|
'metadata' => [
|
|
'package_id' => $package->id,
|
|
'tenant_id' => $tenant->id,
|
|
'user_id' => $user->id,
|
|
'type' => $package->type === 'endcustomer' ? 'endcustomer_event' : 'reseller_subscription',
|
|
],
|
|
'automatic_payment_methods' => [
|
|
'enabled' => true,
|
|
],
|
|
'description' => "Paket: {$package->name}",
|
|
'receipt_email' => $user->email,
|
|
]);
|
|
|
|
Log::info('Payment Intent erstellt', [
|
|
'payment_intent_id' => $paymentIntent->id,
|
|
'package_id' => $package->id,
|
|
'tenant_id' => $tenant->id,
|
|
'amount' => $package->price
|
|
]);
|
|
|
|
return response()->json([
|
|
'clientSecret' => $paymentIntent->client_secret,
|
|
'paymentIntentId' => $paymentIntent->id,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Stripe Payment Intent Fehler', [
|
|
'error' => $e->getMessage(),
|
|
'package_id' => $request->package_id,
|
|
'user_id' => $user->id
|
|
]);
|
|
|
|
return response()->json(['error' => $e->getMessage()], 400);
|
|
}
|
|
}
|
|
} |