switched to paddle inline checkout, removed paypal and most of stripe. added product sync between app and paddle.
This commit is contained in:
@@ -3,42 +3,36 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Mail\ContactConfirmation;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Stripe\Stripe;
|
||||
use Stripe\StripeClient;
|
||||
use Exception;
|
||||
use PayPalHttp\Client;
|
||||
use PayPalHttp\HttpException;
|
||||
use PayPalCheckout\OrdersCreateRequest;
|
||||
use PayPalCheckout\OrdersCaptureRequest;
|
||||
use App\Models\Tenant;
|
||||
use App\Models\BlogPost;
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Models\Package;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Models\PackagePurchase;
|
||||
use App\Models\TenantPackage;
|
||||
use App\Services\Checkout\CheckoutSessionService;
|
||||
use App\Services\Paddle\PaddleCheckoutService;
|
||||
use App\Support\Concerns\PresentsPackages;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Inertia\Inertia;
|
||||
use League\CommonMark\Environment\Environment;
|
||||
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
||||
use League\CommonMark\Extension\Table\TableExtension;
|
||||
use League\CommonMark\Extension\Autolink\AutolinkExtension;
|
||||
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
|
||||
use League\CommonMark\Extension\Strikethrough\StrikethroughExtension;
|
||||
use League\CommonMark\Extension\Table\TableExtension;
|
||||
use League\CommonMark\Extension\TaskList\TaskListExtension;
|
||||
use League\CommonMark\MarkdownConverter;
|
||||
use App\Support\Concerns\PresentsPackages;
|
||||
|
||||
class MarketingController extends Controller
|
||||
{
|
||||
use PresentsPackages;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Stripe::setApiKey(config('services.stripe.key'));
|
||||
}
|
||||
public function __construct(
|
||||
private readonly CheckoutSessionService $checkoutSessions,
|
||||
private readonly PaddleCheckoutService $paddleCheckout,
|
||||
) {}
|
||||
|
||||
public function index()
|
||||
{
|
||||
@@ -69,7 +63,7 @@ class MarketingController extends Controller
|
||||
'email' => $request->email,
|
||||
'message' => $request->message,
|
||||
], $locale),
|
||||
function ($message) use ($request, $contactAddress, $locale) {
|
||||
function ($message) use ($contactAddress, $locale) {
|
||||
$message->to($contactAddress)
|
||||
->subject(__('emails.contact.subject', [], $locale));
|
||||
}
|
||||
@@ -94,22 +88,22 @@ class MarketingController extends Controller
|
||||
*/
|
||||
public function buyPackages(Request $request, $packageId)
|
||||
{
|
||||
Log::info('Buy packages called', ['auth' => Auth::check(), 'package_id' => $packageId, 'provider' => $request->input('provider', 'stripe')]);
|
||||
Log::info('Buy packages called', ['auth' => Auth::check(), 'package_id' => $packageId]);
|
||||
$package = Package::findOrFail($packageId);
|
||||
|
||||
if (!Auth::check()) {
|
||||
if (! Auth::check()) {
|
||||
return redirect()->route('register', ['package_id' => $package->id])
|
||||
->with('message', __('marketing.packages.register_required'));
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
if (!$user->email_verified_at) {
|
||||
if (! $user->email_verified_at) {
|
||||
return redirect()->route('verification.notice')
|
||||
->with('message', __('auth.verification_required'));
|
||||
}
|
||||
|
||||
$tenant = $user->tenant;
|
||||
if (!$tenant) {
|
||||
if (! $tenant) {
|
||||
abort(500, 'Tenant not found');
|
||||
}
|
||||
|
||||
@@ -130,6 +124,7 @@ class MarketingController extends Controller
|
||||
PackagePurchase::create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider' => 'free',
|
||||
'provider_id' => 'free',
|
||||
'price' => $package->price,
|
||||
'type' => $package->type === 'endcustomer' ? 'endcustomer_event' : 'reseller_subscription',
|
||||
@@ -140,246 +135,49 @@ class MarketingController extends Controller
|
||||
return redirect('/event-admin')->with('success', __('marketing.packages.free_assigned'));
|
||||
}
|
||||
|
||||
if ($package->type === 'reseller') {
|
||||
return $this->stripeSubscription($request, $packageId);
|
||||
if (! $package->paddle_price_id) {
|
||||
Log::warning('Package missing Paddle price id', ['package_id' => $package->id]);
|
||||
|
||||
return redirect()->route('packages', ['highlight' => $package->slug])
|
||||
->with('error', __('marketing.packages.paddle_not_configured'));
|
||||
}
|
||||
|
||||
if ($request->input('provider') === 'paypal') {
|
||||
return $this->paypalCheckout($request, $packageId);
|
||||
}
|
||||
$session = $this->checkoutSessions->createOrResume($user, $package, [
|
||||
'tenant' => $tenant,
|
||||
]);
|
||||
|
||||
return $this->checkout($request, $packageId);
|
||||
}
|
||||
$this->checkoutSessions->selectProvider($session, CheckoutSession::PROVIDER_PADDLE);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Checkout for Stripe with auth metadata.
|
||||
*/
|
||||
public function checkout(Request $request, $packageId)
|
||||
{
|
||||
$package = Package::findOrFail($packageId);
|
||||
$user = Auth::user();
|
||||
$tenant = $user->tenant;
|
||||
|
||||
$stripe = new StripeClient(config('services.stripe.secret'));
|
||||
$session = $stripe->checkout->sessions->create([
|
||||
'payment_method_types' => ['card'],
|
||||
'line_items' => [[
|
||||
'price_data' => [
|
||||
'currency' => 'eur',
|
||||
'product_data' => [
|
||||
'name' => $package->name,
|
||||
],
|
||||
'unit_amount' => $package->price * 100,
|
||||
],
|
||||
'quantity' => 1,
|
||||
]],
|
||||
'mode' => 'payment',
|
||||
'success_url' => route('marketing.success', $packageId),
|
||||
'cancel_url' => route('packages'),
|
||||
$checkout = $this->paddleCheckout->createCheckout($tenant, $package, [
|
||||
'success_url' => route('marketing.success', ['packageId' => $package->id]),
|
||||
'return_url' => route('packages', ['highlight' => $package->slug]),
|
||||
'metadata' => [
|
||||
'user_id' => $user->id,
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'type' => $package->type,
|
||||
'checkout_session_id' => $session->id,
|
||||
],
|
||||
]);
|
||||
|
||||
Log::info('Stripe Checkout initiated', ['package_id' => $packageId, 'session_id' => $session->id, 'tenant_id' => $tenant->id]);
|
||||
$session->forceFill([
|
||||
'paddle_checkout_id' => $checkout['id'] ?? $session->paddle_checkout_id,
|
||||
'provider_metadata' => array_merge($session->provider_metadata ?? [], array_filter([
|
||||
'paddle_checkout_id' => $checkout['id'] ?? null,
|
||||
'paddle_checkout_url' => $checkout['checkout_url'] ?? null,
|
||||
'paddle_expires_at' => $checkout['expires_at'] ?? null,
|
||||
])),
|
||||
])->save();
|
||||
|
||||
return redirect($session->url, 303);
|
||||
}
|
||||
$redirectUrl = $checkout['checkout_url'] ?? null;
|
||||
|
||||
/**
|
||||
* PayPal checkout with v2 Orders API (one-time payment).
|
||||
*/
|
||||
public function paypalCheckout(Request $request, $packageId)
|
||||
{
|
||||
$package = Package::findOrFail($packageId);
|
||||
$user = Auth::user();
|
||||
$tenant = $user->tenant;
|
||||
|
||||
$client = Client::create([
|
||||
'clientId' => config('services.paypal.client_id'),
|
||||
'clientSecret' => config('services.paypal.secret'),
|
||||
'environment' => config('services.paypal.sandbox', true) ? 'sandbox' : 'live',
|
||||
]);
|
||||
|
||||
$ordersController = $client->orders();
|
||||
|
||||
$metadata = json_encode([
|
||||
'user_id' => $user->id,
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'type' => $package->type,
|
||||
]);
|
||||
|
||||
$createRequest = new OrdersCreateRequest();
|
||||
$createRequest->prefer('return=representation');
|
||||
$createRequest->body = [
|
||||
"intent" => "CAPTURE",
|
||||
"purchase_units" => [[
|
||||
"amount" => [
|
||||
"currency_code" => "EUR",
|
||||
"value" => number_format($package->price, 2, '.', ''),
|
||||
],
|
||||
"description" => "Package: " . $package->name,
|
||||
"custom_id" => $metadata,
|
||||
]],
|
||||
"application_context" => [
|
||||
"return_url" => route('marketing.success', $packageId),
|
||||
"cancel_url" => route('packages'),
|
||||
],
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $ordersController->createOrder($createRequest);
|
||||
$order = $response->result;
|
||||
|
||||
Log::info('PayPal Checkout initiated', ['package_id' => $packageId, 'order_id' => $order->id, 'tenant_id' => $tenant->id]);
|
||||
|
||||
session(['paypal_order_id' => $order->id]);
|
||||
|
||||
foreach ($order->links as $link) {
|
||||
if ($link->rel === 'approve') {
|
||||
return redirect($link->href);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception('No approve link found');
|
||||
} catch (HttpException $e) {
|
||||
Log::error('PayPal Orders API error: ' . $e->getMessage());
|
||||
return back()->with('error', 'Zahlung fehlgeschlagen');
|
||||
} catch (Exception $e) {
|
||||
Log::error('PayPal checkout error: ' . $e->getMessage());
|
||||
return back()->with('error', 'Zahlung fehlgeschlagen');
|
||||
if (! $redirectUrl) {
|
||||
throw ValidationException::withMessages([
|
||||
'paddle' => __('marketing.packages.paddle_checkout_failed'),
|
||||
]);
|
||||
}
|
||||
|
||||
return redirect()->away($redirectUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stripe subscription checkout for reseller packages.
|
||||
*/
|
||||
public function stripeSubscription(Request $request, $packageId)
|
||||
{
|
||||
$package = Package::findOrFail($packageId);
|
||||
$user = Auth::user();
|
||||
$tenant = $user->tenant;
|
||||
|
||||
$stripe = new StripeClient(config('services.stripe.secret'));
|
||||
$session = $stripe->checkout->sessions->create([
|
||||
'payment_method_types' => ['card'],
|
||||
'line_items' => [[
|
||||
'price_data' => [
|
||||
'currency' => 'eur',
|
||||
'product_data' => [
|
||||
'name' => $package->name . ' (Annual Subscription)',
|
||||
],
|
||||
'unit_amount' => $package->price * 100,
|
||||
'recurring' => [
|
||||
'interval' => 'year',
|
||||
'interval_count' => 1,
|
||||
],
|
||||
],
|
||||
'quantity' => 1,
|
||||
]],
|
||||
'mode' => 'subscription',
|
||||
'success_url' => route('marketing.success', $packageId),
|
||||
'cancel_url' => route('packages'),
|
||||
'metadata' => [
|
||||
'user_id' => $user->id,
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'type' => $package->type,
|
||||
'subscription' => 'true',
|
||||
],
|
||||
]);
|
||||
|
||||
return redirect($session->url, 303);
|
||||
}
|
||||
|
||||
public function stripeCheckout($sessionId)
|
||||
{
|
||||
// Handle Stripe success
|
||||
return view('marketing.success', ['provider' => 'Stripe']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle success after payment (capture PayPal, redirect if verified).
|
||||
*/
|
||||
public function success(Request $request, $packageId = null)
|
||||
{
|
||||
$provider = session('paypal_order_id') ? 'paypal' : 'stripe';
|
||||
Log::info('Payment Success: Provider processed', ['provider' => $provider, 'package_id' => $packageId]);
|
||||
|
||||
if (session('paypal_order_id')) {
|
||||
$orderId = session('paypal_order_id');
|
||||
$client = Client::create([
|
||||
'clientId' => config('services.paypal.client_id'),
|
||||
'clientSecret' => config('services.paypal.secret'),
|
||||
'environment' => config('services.paypal.sandbox', true) ? 'sandbox' : 'live',
|
||||
]);
|
||||
|
||||
$ordersController = $client->orders();
|
||||
|
||||
$captureRequest = new OrdersCaptureRequest($orderId);
|
||||
$captureRequest->prefer('return=minimal');
|
||||
|
||||
try {
|
||||
$captureResponse = $ordersController->captureOrder($captureRequest);
|
||||
$capture = $captureResponse->result;
|
||||
|
||||
Log::info('PayPal Capture completed', ['order_id' => $orderId, 'status' => $capture->status]);
|
||||
|
||||
if ($capture->status === 'COMPLETED') {
|
||||
$customId = $capture->purchaseUnits[0]->customId ?? null;
|
||||
if ($customId) {
|
||||
$metadata = json_decode($customId, true);
|
||||
$package = Package::find($metadata['package_id']);
|
||||
$tenant = Tenant::find($metadata['tenant_id']);
|
||||
|
||||
if ($package && $tenant) {
|
||||
TenantPackage::updateOrCreate(
|
||||
[
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
],
|
||||
[
|
||||
'price' => $package->price,
|
||||
'active' => true,
|
||||
'purchased_at' => now(),
|
||||
'expires_at' => now()->addYear(), // One-time as annual for reseller too
|
||||
]
|
||||
);
|
||||
|
||||
PackagePurchase::create([
|
||||
'tenant_id' => $tenant->id,
|
||||
'package_id' => $package->id,
|
||||
'provider_id' => 'paypal',
|
||||
'price' => $package->price,
|
||||
'type' => $package->type === 'endcustomer' ? 'endcustomer_event' : 'reseller_subscription',
|
||||
'purchased_at' => now(),
|
||||
'refunded' => false,
|
||||
]);
|
||||
|
||||
session()->forget('paypal_order_id');
|
||||
$request->session()->flash('success', __('marketing.packages.purchased_successfully', ['name' => $package->name]));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Log::error('PayPal capture failed: ' . $capture->status);
|
||||
$request->session()->flash('error', 'Zahlung konnte nicht abgeschlossen werden.');
|
||||
}
|
||||
} catch (HttpException $e) {
|
||||
Log::error('PayPal capture error: ' . $e->getMessage());
|
||||
$request->session()->flash('error', 'Zahlung konnte nicht abgeschlossen werden.');
|
||||
} catch (\Exception $e) {
|
||||
Log::error('PayPal success error: ' . $e->getMessage());
|
||||
$request->session()->flash('error', 'Fehler beim Abschliessen der Zahlung.');
|
||||
}
|
||||
}
|
||||
|
||||
// Common logic: Redirect to admin if verified
|
||||
if (Auth::check() && Auth::user()->email_verified_at) {
|
||||
return redirect('/event-admin')->with('success', __('marketing.success.welcome'));
|
||||
}
|
||||
@@ -392,7 +190,7 @@ class MarketingController extends Controller
|
||||
$locale = $request->get('locale', app()->getLocale());
|
||||
Log::info('Blog Index Debug - Initial', [
|
||||
'locale' => $locale,
|
||||
'full_url' => $request->fullUrl()
|
||||
'full_url' => $request->fullUrl(),
|
||||
]);
|
||||
|
||||
$query = BlogPost::query()
|
||||
@@ -424,6 +222,7 @@ class MarketingController extends Controller
|
||||
$post->title = $post->getTranslation('title', $locale) ?? $post->getTranslation('title', 'de') ?? '';
|
||||
$post->excerpt = $post->getTranslation('excerpt', $locale) ?? $post->getTranslation('excerpt', 'de') ?? '';
|
||||
$post->content = $post->getTranslation('content', $locale) ?? $post->getTranslation('content', 'de') ?? '';
|
||||
|
||||
// Author name is a string, no translation needed; author is loaded via with('author')
|
||||
return $post;
|
||||
});
|
||||
@@ -432,7 +231,7 @@ class MarketingController extends Controller
|
||||
'count' => $posts->count(),
|
||||
'total' => $posts->total(),
|
||||
'posts_data' => $posts->toArray(),
|
||||
'first_post_title' => $posts->count() > 0 ? $posts->first()->title : 'No posts'
|
||||
'first_post_title' => $posts->count() > 0 ? $posts->first()->title : 'No posts',
|
||||
]);
|
||||
|
||||
return Inertia::render('marketing/Blog', compact('posts'));
|
||||
@@ -456,24 +255,24 @@ class MarketingController extends Controller
|
||||
// Transform to array with translated strings for the current locale
|
||||
$markdown = $postModel->getTranslation('content', $locale) ?? $postModel->getTranslation('content', 'de') ?? '';
|
||||
|
||||
$environment = new Environment();
|
||||
$environment->addExtension(new CommonMarkCoreExtension());
|
||||
$environment->addExtension(new TableExtension());
|
||||
$environment->addExtension(new AutolinkExtension());
|
||||
$environment->addExtension(new StrikethroughExtension());
|
||||
$environment->addExtension(new TaskListExtension());
|
||||
$environment = new Environment;
|
||||
$environment->addExtension(new CommonMarkCoreExtension);
|
||||
$environment->addExtension(new TableExtension);
|
||||
$environment->addExtension(new AutolinkExtension);
|
||||
$environment->addExtension(new StrikethroughExtension);
|
||||
$environment->addExtension(new TaskListExtension);
|
||||
|
||||
$converter = new MarkdownConverter($environment);
|
||||
$contentHtml = (string) $converter->convert($markdown);
|
||||
|
||||
|
||||
// Debug log for content_html
|
||||
\Log::info('BlogShow Debug: content_html type and preview', [
|
||||
'type' => gettype($contentHtml),
|
||||
'is_string' => is_string($contentHtml),
|
||||
'length' => strlen($contentHtml ?? ''),
|
||||
'preview' => substr((string)$contentHtml, 0, 200) . '...'
|
||||
'preview' => substr((string) $contentHtml, 0, 200).'...',
|
||||
]);
|
||||
|
||||
|
||||
$post = [
|
||||
'id' => $postModel->id,
|
||||
'title' => $postModel->getTranslation('title', $locale) ?? $postModel->getTranslation('title', 'de') ?? '',
|
||||
@@ -484,17 +283,17 @@ class MarketingController extends Controller
|
||||
'published_at' => $postModel->published_at->toDateString(),
|
||||
'slug' => $postModel->slug,
|
||||
'author' => $postModel->author ? [
|
||||
'name' => $postModel->author->name
|
||||
'name' => $postModel->author->name,
|
||||
] : null,
|
||||
];
|
||||
|
||||
|
||||
// Debug log for final postArray
|
||||
\Log::info('BlogShow Debug: Final post content_html', [
|
||||
'type' => gettype($post['content_html']),
|
||||
'is_string' => is_string($post['content_html']),
|
||||
'length' => strlen($post['content_html'] ?? ''),
|
||||
]);
|
||||
|
||||
|
||||
return Inertia::render('marketing/BlogShow', compact('post'));
|
||||
}
|
||||
|
||||
@@ -527,11 +326,11 @@ class MarketingController extends Controller
|
||||
'locale' => app()->getLocale(),
|
||||
'url' => request()->fullUrl(),
|
||||
'route' => request()->route()->getName(),
|
||||
'isInertia' => request()->header('X-Inertia')
|
||||
'isInertia' => request()->header('X-Inertia'),
|
||||
]);
|
||||
|
||||
$validTypes = ['hochzeit', 'geburtstag', 'firmenevent'];
|
||||
if (!in_array($type, $validTypes)) {
|
||||
if (! in_array($type, $validTypes)) {
|
||||
Log::warning('Invalid occasion type accessed', ['type' => $type]);
|
||||
abort(404, 'Invalid occasion type');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user