-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpaddle-checkout.php
More file actions
96 lines (84 loc) · 2.49 KB
/
Copy pathpaddle-checkout.php
File metadata and controls
96 lines (84 loc) · 2.49 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
chdir($_SERVER['DOCUMENT_ROOT']);
require_once("./inc/top.php");
require_once("./pages/download/order.php");
require_once("./inc/service/validation.php");
require_once("./inc/site/purchases.php");
?>
<!DOCTYPE html>
<?php require_once("./inc/header.php"); ?>
<?php
function parse_request() {
global $ACCOUNTING;
global $SITE_URL;
$paddle = $ACCOUNTING['methods']['paddle'];
$paddle_test = $paddle['test'];
$paddle_user_key = ($paddle_test) ? $paddle['test_user_key'] : $paddle['live_user_key'];
if (!isset($paddle_user_key)) {
return ['Paddle payments not configured', null];
}
$paddle_transaction_id = isset($_REQUEST['_ptxn']) ? $_REQUEST['_ptxn'] : null;
if (!verify_paddle_transaction_id($paddle_transaction_id)) {
error_log("Failed to verity transaction id={$paddle_transaction_id}");
return ['Paddle transaction invalid or not specified', null];
}
[$error, $order] = find_order(['remote_id' => $paddle_transaction_id, 'method' => 'paddle']);
if (isset($error)) {
return [$error, null];
}
return [
null,
[
'order' => $order,
'paddle_sandbox' => ($paddle_test) ? "Paddle.Environment.set(\"sandbox\");" : "",
'paddle_key' => $paddle_user_key,
'paddle_txn' => $paddle_transaction_id,
'success_url' => "{$SITE_URL}/actions/finish_order?order_id={$order['order_id']}",
'cancel_url' => "{$SITE_URL}/?page=download"
]
];
}
[$error, $result] = parse_request();
if (isset($error)) {
echo $error;
}
else {
$order = $result['order'];
error_log("Order: " . var_export($order, true));
show_order($order);
if ($order['status'] == 'created') {
?>
<script type="text/javascript" src="https://cdn.paddle.com/paddle/v2/paddle.js"></script>
<script type="text/javascript">
<?= $result['paddle_sandbox'] ?>
Paddle.Initialize({
token: '<?= $result['paddle_key'] ?>',
eventCallback: function(data) {
console.log("Paddle event CALLBACK:", data);
if (data.name === "checkout.completed") {
setTimeout(() => {
window.location.href = "<?= $result['success_url'] ?>";
}, 2000);
}
else if (data.name === "checkout.closed") {
setTimeout(() => {
window.location.href = "<?= $result['cancel_url'] ?>";
}, 1000);
}
}
});
Paddle.Checkout.Open({
transactionId: "<?= $result['paddle_txn'] ?>",
settings: {
displayMode: "overlay",
theme: "dark",
variant: "one-page",
successUrl: "<?= $result['success_url'] ?>"
}
});
</script>
<?php
}
}
?>
<?php require_once("./inc/footer.php"); ?>