-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstripe_return.php
More file actions
36 lines (31 loc) · 1.57 KB
/
stripe_return.php
File metadata and controls
36 lines (31 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/stripe_lib.php';
require_once __DIR__ . '/provision_storefront.php';
$pdo = db();
$orderId = (int)($_GET['order'] ?? 0);
$sessionId = trim((string)($_GET['session_id'] ?? ''));
$st = $pdo->prepare("SELECT * FROM orders WHERE id=?");
$st->execute([$orderId]);
$order = $st->fetch();
if (!$order) die('Order not found');
if ($sessionId === '') $sessionId = (string)($order['provider_txn'] ?? '');
try {
$session = stripe_get_checkout_session($sessionId);
$mode = strtolower((string)($session['mode'] ?? ''));
$status = strtolower((string)($session['payment_status'] ?? ''));
$subscriptionId = '';
if (is_array($session['subscription'] ?? null)) $subscriptionId = (string)($session['subscription']['id'] ?? '');
else $subscriptionId = (string)($session['subscription'] ?? '');
if ($mode !== 'subscription' || $subscriptionId === '') throw new Exception('Stripe subscription checkout not completed.');
if (!in_array($status, ['paid', 'no_payment_required'], true)) throw new Exception('Stripe payment not completed yet.');
$subscription = is_array($session['subscription'] ?? null) ? $session['subscription'] : stripe_get_subscription($subscriptionId);
$userId = gc_sync_stripe_subscription_from_order($pdo, $orderId, $subscription);
if (session_status() === PHP_SESSION_NONE) session_start();
$_SESSION['store_user'] = $userId;
header('Location: success.php?order=' . $orderId);
exit;
} catch (Throwable $e) {
die('Stripe verification error: ' . $e->getMessage());
}