39 lines
1.2 KiB
PHP
39 lines
1.2 KiB
PHP
<?php
|
|
$config = require __DIR__ . '/config.php';
|
|
require __DIR__ . '/lib/PayPalClient.php';
|
|
require __DIR__ . '/lib/Storage.php';
|
|
|
|
$orderId = $_GET['token'] ?? $_GET['orderId'] ?? null; // token param from PayPal return
|
|
if (!$orderId) {
|
|
http_response_code(400);
|
|
echo 'Missing orderId/token';
|
|
exit;
|
|
}
|
|
|
|
$client = new PayPalClient($config);
|
|
$storage = new Storage($config['storage_path']);
|
|
|
|
// Capture the order
|
|
$resp = $client->api('POST', '/v2/checkout/orders/' . urlencode($orderId) . '/capture');
|
|
|
|
if ($resp['status'] < 200 || $resp['status'] >= 300) {
|
|
http_response_code(500);
|
|
echo 'Capture failed: ' . htmlspecialchars(json_encode($resp));
|
|
exit;
|
|
}
|
|
|
|
$capture = $resp['body'];
|
|
|
|
// Update local order store
|
|
$orders = $storage->get('orders', []);
|
|
if (isset($orders[$orderId])) {
|
|
$orders[$orderId]['status'] = $capture['status'] ?? 'CAPTURED';
|
|
$storage->set('orders', $orders);
|
|
}
|
|
|
|
// Redirect back to home with a status indicator
|
|
$baseUrl = rtrim($config['base_url'], '/');
|
|
$status = isset($capture['status']) ? $capture['status'] : 'CAPTURED';
|
|
header('Location: ' . $baseUrl . '/index.php?captured=1&order=' . urlencode($orderId) . '&status=' . urlencode($status));
|
|
exit;
|