create order -> redirect to PayPal's approve page -> * PayPal redirects back here with ?paypal=success&token= -> * this file captures the order server-side and shows the result. * (Redirect-based, not the JS SDK popup/iframe button, so it can be * styled as a plain themed link instead of PayPal's own widget.) */ require_once __DIR__ . '/paypal_checkout_config.php'; if (session_status() === PHP_SESSION_NONE) { session_start(); } $paypal_currency = $paypal_currency ?? PAYPAL_CURRENCY; $paypal_description = $paypal_description ?? 'Order Payment'; $paypal_button_text = $paypal_button_text ?? 'PayPal / Credit Card'; $_SESSION['paypal_checkout_amount'] = $owing; $_SESSION['paypal_checkout_trxnum'] = $trxnum; $_SESSION['paypal_checkout_currency'] = $paypal_currency; $_SESSION['paypal_checkout_description'] = $paypal_description; $containerId = 'pp-btn-' . uniqid(); // -- Handle the return trip from PayPal (redirect-based approval) ---------- $pppCaptureResult = null; if (isset($_GET['paypal']) && $_GET['paypal'] === 'success' && isset($_GET['token'])) { $orderId = $_GET['token']; if (isset($_SESSION['paypal_order_id']) && $_SESSION['paypal_order_id'] === $orderId) { require_once __DIR__ . '/paypal_checkout_api.php'; try { $paypalApi = new PayPalCheckoutAPI(); $capture = $paypalApi->captureOrder($orderId); if ($capture['status'] === 'COMPLETED') { $pppCaptureResult = ['success' => true]; } else { $pppCaptureResult = ['success' => false, 'error' => 'Payment was not completed. Status: ' . $capture['status']]; } } catch (Exception $e) { $pppCaptureResult = ['success' => false, 'error' => $e->getMessage()]; } unset($_SESSION['paypal_order_id'], $_SESSION['paypal_order_trxnum'], $_SESSION['paypal_order_amount']); } } elseif (isset($_GET['paypal']) && $_GET['paypal'] === 'cancel') { $pppCaptureResult = ['success' => false, 'error' => 'Payment was cancelled.']; } ?>
Payment Successful!