cart->count() === 0) { return redirect()->route('checkout.index'); } $subtotal = $this->cart->subtotal(); $country = $checkoutData['shipping']['country'] ?? $checkoutData['billing']['country'] ?? 'DE'; $shippingRate = ShippingRate::forCart($subtotal, $country); $shippingCost = $shippingRate ? $shippingRate->getCostForTotal($subtotal) : 0; $discount = 0; if ($code = session('coupon_code')) { $coupon = Coupon::findValid($code); if ($coupon) $discount = $coupon->calcDiscount($subtotal); } $total = max(0, $subtotal - $discount) + $shippingCost; $provider = new PayPalClient; $provider->setApiCredentials(config('paypal')); $provider->getAccessToken(); $order = $provider->createOrder([ 'intent' => 'CAPTURE', 'purchase_units' => [[ 'amount' => [ 'currency_code' => 'EUR', 'value' => number_format($total, 2, '.', ''), ], 'description' => config('app.name') . ' Bestellung', ]], 'application_context' => [ 'return_url' => route('paypal.success'), 'cancel_url' => route('checkout.confirm'), ], ]); if (isset($order['id'])) { session(['paypal_order_id' => $order['id']]); foreach ($order['links'] as $link) { if ($link['rel'] === 'approve') { return redirect($link['href']); } } } return redirect()->route('checkout.confirm')->with('error', 'PayPal-Verbindung fehlgeschlagen. Bitte versuchen Sie es erneut.'); } public function success(Request $request) { $checkoutData = session('checkout_data'); $paypalOrderId = session('paypal_order_id'); if (!$checkoutData || !$paypalOrderId) { return redirect()->route('checkout.index'); } $provider = new PayPalClient; $provider->setApiCredentials(config('paypal')); $provider->getAccessToken(); $result = $provider->capturePaymentOrder($paypalOrderId); if (($result['status'] ?? '') !== 'COMPLETED') { return redirect()->route('checkout.confirm')->with('error', 'PayPal-Zahlung nicht abgeschlossen.'); } $checkoutData['payment_method'] = 'paypal'; $order = $this->orderService->createFromSession($checkoutData, 'paypal'); $order->update([ 'payment_id' => $paypalOrderId, 'status' => 'payment_received', 'paid_at' => now(), ]); session()->forget(['checkout_data', 'checkout_country', 'paypal_order_id']); return redirect()->route('checkout.thankyou', $order->order_number); } public function cancel() { session()->forget('paypal_order_id'); return redirect()->route('checkout.confirm')->with('error', 'PayPal-Zahlung abgebrochen.'); } }