208 lines
7.0 KiB
PHP
208 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Str;
|
|
use Stripe\Stripe;
|
|
use Stripe\Checkout\Session;
|
|
use Stripe\StripeClient;
|
|
use Exception;
|
|
use PayPal\Api\Amount;
|
|
use PayPal\Api\Payer;
|
|
use PayPal\Api\Payment;
|
|
use PayPal\Api\RedirectUrls;
|
|
use PayPal\Api\Transaction;
|
|
use PayPal\Rest\ApiContext;
|
|
use PayPal\Auth\OAuthTokenCredential;
|
|
use App\Models\Tenant;
|
|
use App\Models\EventPurchase;
|
|
|
|
class MarketingController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
\Stripe\Stripe::setApiKey(config('services.stripe.key'));
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$packages = [
|
|
['id' => 'basic', 'name' => 'Basic', 'events' => 1, 'price' => 0, 'description' => '1 Event, 100 Fotos, Grundfunktionen'],
|
|
['id' => 'standard', 'name' => 'Standard', 'events' => 10, 'price' => 99, 'description' => '10 Events, Unbegrenzt Fotos, Erweiterte Features'],
|
|
['id' => 'premium', 'name' => 'Premium', 'events' => 50, 'price' => 199, 'description' => '50 Events, Support & Custom, Alle Features'],
|
|
];
|
|
|
|
return view('marketing', compact('packages'));
|
|
}
|
|
|
|
public function contact(Request $request)
|
|
{
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'email' => 'required|email|max:255',
|
|
'message' => 'required|string|max:1000',
|
|
]);
|
|
|
|
Mail::raw("Kontakt-Anfrage von {$request->name} ({$request->email}): {$request->message}", function ($message) use ($request) {
|
|
$message->to('admin@fotospiel.de')
|
|
->subject('Neue Kontakt-Anfrage');
|
|
});
|
|
|
|
return redirect()->back()->with('success', 'Nachricht gesendet!');
|
|
}
|
|
|
|
public function checkout(Request $request, $package)
|
|
{
|
|
$packages = [
|
|
'basic' => ['name' => 'Basic', 'price' => 0, 'events' => 1],
|
|
'standard' => ['name' => 'Standard', 'price' => 9900, 'events' => 10], // cents
|
|
'premium' => ['name' => 'Premium', 'price' => 19900, 'events' => 50],
|
|
];
|
|
|
|
if (!isset($packages[$package])) {
|
|
abort(404);
|
|
}
|
|
|
|
$pkg = $packages[$package];
|
|
|
|
if ($pkg['price'] == 0) {
|
|
// Free package: create tenant and event
|
|
$tenant = Tenant::create([
|
|
'name' => $request->input('tenant_name', 'New Tenant'),
|
|
'slug' => Str::slug('new-' . now()),
|
|
'email' => $request->input('email'),
|
|
'events_remaining' => $pkg['events'],
|
|
]);
|
|
|
|
// Create initial event
|
|
$event = $tenant->events()->create([
|
|
'name' => $request->input('event_name', 'My Event'),
|
|
'slug' => Str::slug($request->input('event_name', 'my-event')),
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$purchase = EventPurchase::create([
|
|
'tenant_id' => $tenant->id,
|
|
'events_purchased' => $pkg['events'],
|
|
'amount' => 0,
|
|
'currency' => 'EUR',
|
|
'provider' => 'free',
|
|
'status' => 'completed',
|
|
'purchased_at' => now(),
|
|
]);
|
|
|
|
return redirect("/admin/tenants/{$tenant->id}/edit")->with('success', 'Konto erstellt! Willkommen bei Fotospiel.');
|
|
}
|
|
|
|
$stripe = new \Stripe\StripeClient(config('services.stripe.secret'));
|
|
$session = $stripe->checkout->sessions->create([
|
|
'payment_method_types' => ['card'],
|
|
'line_items' => [[
|
|
'price_data' => [
|
|
'currency' => 'eur',
|
|
'product_data' => [
|
|
'name' => $pkg['name'] . ' Package',
|
|
],
|
|
'unit_amount' => $pkg['price'],
|
|
],
|
|
'quantity' => 1,
|
|
]],
|
|
'mode' => 'payment',
|
|
'success_url' => route('marketing.success', $package),
|
|
'cancel_url' => route('marketing'),
|
|
'metadata' => [
|
|
'package' => $package,
|
|
'events' => $pkg['events'],
|
|
],
|
|
]);
|
|
|
|
return redirect($session->url, 303);
|
|
}
|
|
|
|
public function stripeCheckout($sessionId)
|
|
{
|
|
// Handle Stripe success
|
|
return view('marketing.success', ['provider' => 'Stripe']);
|
|
}
|
|
|
|
public function paypalCheckout(Request $request, $package)
|
|
{
|
|
$packages = [
|
|
'basic' => ['name' => 'Basic', 'price' => 0, 'events' => 1],
|
|
'standard' => ['name' => 'Standard', 'price' => 99, 'events' => 10],
|
|
'premium' => ['name' => 'Premium', 'price' => 199, 'events' => 50],
|
|
];
|
|
|
|
if (!isset($packages[$package])) {
|
|
abort(404);
|
|
}
|
|
|
|
$pkg = $packages[$package];
|
|
|
|
if ($pkg['price'] == 0) {
|
|
// Free package: create tenant and event
|
|
$tenant = Tenant::create([
|
|
'name' => $request->input('tenant_name', 'New Tenant'),
|
|
'slug' => Str::slug('new-' . now()),
|
|
'email' => $request->input('email'),
|
|
'events_remaining' => $pkg['events'],
|
|
]);
|
|
|
|
// Create initial event
|
|
$event = $tenant->events()->create([
|
|
'name' => $request->input('event_name', 'My Event'),
|
|
'slug' => Str::slug($request->input('event_name', 'my-event')),
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$purchase = EventPurchase::create([
|
|
'tenant_id' => $tenant->id,
|
|
'events_purchased' => $pkg['events'],
|
|
'amount' => 0,
|
|
'currency' => 'EUR',
|
|
'provider' => 'free',
|
|
'status' => 'completed',
|
|
'purchased_at' => now(),
|
|
]);
|
|
|
|
return redirect("/admin/tenants/{$tenant->id}/edit")->with('success', 'Konto erstellt! Willkommen bei Fotospiel.');
|
|
}
|
|
|
|
$apiContext = new ApiContext(
|
|
new OAuthTokenCredential(
|
|
config('services.paypal.client_id'),
|
|
config('services.paypal.secret')
|
|
)
|
|
);
|
|
|
|
$payment = new Payment();
|
|
$payer = new Payer();
|
|
$payer->setPaymentMethod('paypal');
|
|
|
|
$amountObj = new Amount();
|
|
$amountObj->setCurrency('EUR');
|
|
$amountObj->setTotal($pkg['price']);
|
|
|
|
$transaction = new Transaction();
|
|
$transaction->setAmount($amountObj);
|
|
|
|
$redirectUrls = new RedirectUrls();
|
|
$redirectUrls->setReturnUrl(route('marketing.success', $package));
|
|
$redirectUrls->setCancelUrl(route('marketing'));
|
|
|
|
$payment->setIntent('sale')
|
|
->setPayer($payer)
|
|
->setTransactions([$transaction])
|
|
->setRedirectUrls($redirectUrls);
|
|
|
|
try {
|
|
$payment->create($apiContext);
|
|
return redirect($payment->getApprovalLink());
|
|
} catch (Exception $e) {
|
|
return back()->with('error', 'Zahlung fehlgeschlagen');
|
|
}
|
|
}
|
|
}
|