· 5 years ago · Oct 02, 2020, 07:46 PM
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
7 <!-- CSS only -->
8 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
9
10 <title>Document</title>
11</head>
12<body>
13
14 <div class="container">
15 <div class="jumbotron">
16 <h1>Payment with Stripe</h1>
17 </div>
18
19 <button type="button" class="btn btn-primary" id="checkout-button">Pay</button>
20 </div>
21
22 <script src="https://js.stripe.com/v3/"></script>
23 <script type="text/javascript">
24 // Create an instance of the Stripe object with your publishable API key
25 var stripe = Stripe('pk_live_oIni5L8zHfl4oklmtMXSgVbg00yRsU0u1C');
26 var checkoutButton = document.getElementById('checkout-button');
27
28 checkoutButton.addEventListener('click', function() {
29 // Create a new Checkout Session using the server-side endpoint you
30 // created in step 3.
31 fetch('/create-checkout-session', {
32 method: 'POST',
33 })
34 .then(function(response) {
35 return response.json();
36 })
37 .then(function(session) {
38 return stripe.redirectToCheckout({ sessionId: session.id });
39 })
40 .then(function(result) {
41 // If `redirectToCheckout` fails due to a browser or network
42 // error, you should display the localized error message to your
43 // customer using `error.message`.
44 if (result.error) {
45 //alert(result.error.message);
46 }
47 })
48 .catch(function(error) {
49 //console.error('Error:', error);
50 });
51 });
52 </script>
53</body>
54</html>