-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #395 from revanth1718/revanth
Stripe API
- Loading branch information
Showing
6 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="stylesheet" href="style.css"> | ||
<title>Stripe Payment Integration</title> | ||
</head> | ||
<body> | ||
<header> | ||
<h1>Stripe Payment Integration</h1> | ||
</header> | ||
<main> | ||
<section> | ||
<form id="payment-form"> | ||
<div id="card-element"></div> | ||
<button id="submit">Pay Now</button> | ||
<div id="card-errors" role="alert"></div> | ||
</form> | ||
</section> | ||
</main> | ||
<script src="https://js.stripe.com/v3/"></script> | ||
<script src="index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Initialize Stripe | ||
const stripe = Stripe('YOUR_PUBLIC_STRIPE_KEY'); | ||
|
||
// Create an instance of Elements | ||
const elements = stripe.elements(); | ||
|
||
// Create an instance of the card Element | ||
const card = elements.create('card'); | ||
|
||
// Add an instance of the card Element into the `card-element` div | ||
card.mount('#card-element'); | ||
|
||
// Handle real-time validation errors from the card Element | ||
card.on('change', function(event) { | ||
const displayError = document.getElementById('card-errors'); | ||
if (event.error) { | ||
displayError.textContent = event.error.message; | ||
} else { | ||
displayError.textContent = ''; | ||
} | ||
}); | ||
|
||
// Handle form submission | ||
const form = document.getElementById('payment-form'); | ||
form.addEventListener('submit', async (event) => { | ||
event.preventDefault(); | ||
|
||
const {token, error} = await stripe.createToken(card); | ||
|
||
if (error) { | ||
// Inform the user if there was an error | ||
const errorElement = document.getElementById('card-errors'); | ||
errorElement.textContent = error.message; | ||
} else { | ||
// Send the token to your server | ||
const response = await fetch('/charge', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ token: token.id }) | ||
}); | ||
|
||
const data = await response.json(); | ||
if (data.success) { | ||
alert('Payment Successful!'); | ||
} else { | ||
alert('Payment Failed!'); | ||
} | ||
} | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "stripe-payment-integration", | ||
"version": "1.0.0", | ||
"description": "A simple payment integration using Stripe API", | ||
"main": "server.js", | ||
"scripts": { | ||
"start": "node server.js" | ||
}, | ||
"author": "Your Name", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.17.1", | ||
"stripe": "^8.0.0", | ||
"body-parser": "^1.19.0" | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const express = require('express'); | ||
const stripe = require('stripe')('YOUR_SECRET_STRIPE_KEY'); | ||
const bodyParser = require('body-parser'); | ||
|
||
const app = express(); | ||
app.use(express.static('public')); | ||
app.use(bodyParser.json()); | ||
|
||
app.post('/charge', async (req, res) => { | ||
try { | ||
const { token } = req.body; | ||
const charge = await stripe.charges.create({ | ||
amount: 5000, // Amount in cents (e.g., $50.00) | ||
currency: 'usd', | ||
source: token, | ||
description: 'Example charge' | ||
}); | ||
res.json({ success: true }); | ||
} catch (error) { | ||
console.error('Error creating charge:', error); | ||
res.status(500).json({ success: false }); | ||
} | ||
}); | ||
|
||
app.listen(3000, () => { | ||
console.log('Server is running on port 3000'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
body { | ||
font-family: 'Arial', sans-serif; | ||
background: linear-gradient(135deg, #6772e5, #ff5e57); | ||
color: #333; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
min-height: 100vh; | ||
margin: 0; | ||
} | ||
|
||
header { | ||
text-align: center; | ||
margin-bottom: 20px; | ||
} | ||
|
||
header h1 { | ||
font-size: 2.5em; | ||
color: #fff; | ||
} | ||
|
||
main { | ||
background: #fff; | ||
padding: 20px; | ||
border-radius: 10px; | ||
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); | ||
width: 300px; | ||
} | ||
|
||
form { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 10px; | ||
} | ||
|
||
button { | ||
padding: 10px; | ||
font-size: 1em; | ||
color: #fff; | ||
background-color: #6772e5; | ||
border: none; | ||
border-radius: 5px; | ||
cursor: pointer; | ||
transition: background-color 0.3s; | ||
} | ||
|
||
button:hover { | ||
background-color: #5469d4; | ||
} | ||
|
||
#card-errors { | ||
color: red; | ||
margin-top: 10px; | ||
} |