-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
80 lines (66 loc) · 2.97 KB
/
index.php
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
<?php
require_once __DIR__ . '/includes/functions.php';
require_once __DIR__ . '/config/db.php';
$actions = new Actions;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<title>Stripe Payment Test</title>
</head>
<body>
<?php include __DIR__ . '/includes/header.php'; ?>
<div class="container">
<h3 class="text-center font-weight-bold mt-4">Welcome to my shop</h3>
<hr>
<div class="alert alert-warning" role="alert">
You must login first. Otherwise you cannot buy.
</div>
<div class='row'>
<?php $actions->getProduct(); ?>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
<!-- Stripe Payment Script -->
<script src="https://js.stripe.com/v3/"></script>
<script>
$(function() {
// Create an instance of the Stripe object with your publishable API key
let stripe = Stripe('<?= DB::STRIPE_PUB_KEY ?>');
// When we click the button with the class 'buy_now_btn'
$(document).on('click', '.buy_now_btn', function(e) {
// Get the corresponding product id
// 'this' means that the button what we click
let id = $(this).attr('id');
$(this).text('Please wait...');
$.ajax({
url: "includes/ajax/checkout-action.php",
method: "POST",
dataType: "json",
data: {
id: id,
// We use this one to treat whether the checkotu btn is clicked
stripe_payment_process: 1
},
success: function(session) {
if (session == 1) {
// Similar behaviour to HTTP redirect for replace() means it won't create an entry in your browser's history.
window.location.replace('https://localhost/stripe_payment/login.php');
} else {
// redirect to stripe checkout page
return stripe.redirectToCheckout({
sessionId: session.id
});
}
}
});
});
});
</script>
</body>
</html>