-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment.html
More file actions
86 lines (79 loc) · 2.91 KB
/
Copy pathpayment.html
File metadata and controls
86 lines (79 loc) · 2.91 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stripe Payment</title>
<script src="https://js.stripe.com/v3/"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
#payment-form {
max-width: 400px;
margin: auto;
}
#card-element {
border: 1px solid #ccc;
padding: 10px;
margin-bottom: 20px;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#card-errors {
color: red;
}
</style>
</head>
<body>
<h1>Stripe Payment Integration</h1>
<form id="payment-form">
<div id="card-element"></div>
<button type="submit">Pay $10</button>
<div id="card-errors" role="alert"></div>
</form>
<script>
const stripe = Stripe('pk_test_51QZAZQFDu12sFaxLg9ZFXEIJrhJt8qxSx86IeiuJWUBKGUtGUWw7MYuUp5tTt7ml876BT3zg2dRBlZekTkTWeyka00UjetT02u'); // Replace with your Publishable Key
const elements = stripe.elements();
const card = elements.create('card');
card.mount('#card-element');
document.getElementById('payment-form').addEventListener('submit', async (event) => {
event.preventDefault();
try {
// Fetch the client secret from the PHP backend (payment.php)
const response = await fetch('payment.php', { method: 'POST' });
// Read the raw response body as text
const responseBody = await response.text();
console.log('Raw Response Body:', responseBody); // Log the raw response to check what's returned
// Parse the response only if it's valid JSON
const responseData = JSON.parse(responseBody);
const { clientSecret } = responseData; // Extract clientSecret from the response
// Use the clientSecret to confirm the payment
const { error } = await stripe.confirmCardPayment(clientSecret, {
payment_method: { card: card },
});
// Handle errors or success
if (error) {
document.getElementById('card-errors').textContent = error.message;
} else {
alert('Payment Successful!');
}
} catch (err) {
console.error('Error:', err);
document.getElementById('card-errors').textContent = 'An error occurred. Please try again.';
}
});
</script>
</body>
</html>