-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaypal_start.php
More file actions
39 lines (34 loc) · 1.32 KB
/
paypal_start.php
File metadata and controls
39 lines (34 loc) · 1.32 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
37
38
39
<?php
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/paypal_lib.php';
$pdo=db();
$orderId=(int)($_GET['order'] ?? 0);
$st=$pdo->prepare("SELECT * FROM orders WHERE id=?");
$st->execute([$orderId]);
$order=$st->fetch();
if(!$order) die("Order not found");
if (!gc_payment_provider_is_available($pdo, 'paypal')) {
flash_set('PayPal is not enabled or not configured yet.', 'error');
header('Location: checkout.php?plan=' . (int)($order['plan_id'] ?? 0));
exit;
}
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
$baseUrl = $scheme."://".$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']);
try{
$ppOrder = paypal_create_order($order['amount'],$order['currency'],
$baseUrl."/paypal_return.php?order=".$orderId,
$baseUrl."/paypal_cancel.php?order=".$orderId
);
$ppId=$ppOrder['id'] ?? null;
if(!$ppId) throw new Exception("PayPal order failed");
$pdo->prepare("UPDATE orders SET provider_txn=? WHERE id=?")->execute([$ppId,$orderId]);
$approve=null;
foreach(($ppOrder['links'] ?? []) as $ln){
if(($ln['rel'] ?? '')==='approve'){ $approve=$ln['href']; break; }
}
if(!$approve) throw new Exception("No approve link");
header("Location: ".$approve); exit;
}catch(Exception $e){
die("PayPal error: ".$e->getMessage());
}