Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe API #395

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions New_APIs/Stripe API/index.html
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>
51 changes: 51 additions & 0 deletions New_APIs/Stripe API/index.js
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!');
}
}
});
37 changes: 37 additions & 0 deletions New_APIs/Stripe API/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions New_APIs/Stripe API/package.json
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"
}
}

27 changes: 27 additions & 0 deletions New_APIs/Stripe API/server.js
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');
});
55 changes: 55 additions & 0 deletions New_APIs/Stripe API/style.css
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;
}
Loading