-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0300a37
Showing
12 changed files
with
1,853 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,2 @@ | ||
STRIPE_SECRET_KEY=your_secret_key | ||
STRIPE_ENDPOINT_SECRET=your_endpoint_secret |
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,21 @@ | ||
# Build and Release Folders | ||
bin-debug/ | ||
bin-release/ | ||
[Oo]bj/ | ||
[Bb]in/ | ||
|
||
# Other files and folders | ||
.settings/ | ||
node_modules/ | ||
|
||
|
||
# Executables | ||
*.swf | ||
*.air | ||
*.ipa | ||
*.apk | ||
*.env | ||
|
||
# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties` | ||
# should NOT be excluded as they contain compiler settings and other important | ||
# information for Eclipse / Flash Builder. |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Salman Dabbakuti | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 @@ | ||
# Stripe Payments Integration with express | ||
|
||
This is a simple project to demonstrate how to integrate stripe payments and webhook handling with express | ||
|
||
## Getting Started | ||
|
||
> Copy the `.env.example` file to `.env` and fill in the required values. | ||
```bash | ||
|
||
npm install | ||
|
||
npm start | ||
``` | ||
|
||
## Usage | ||
|
||
- Go to `http://localhost:3000/purchase` and click on checkout. | ||
- It will redirect you to the stripe checkout page. Fill in the details and click on pay. | ||
- You will be redirected to the success page if the payment is successful. | ||
- You can see the payment details in the stripe dashboard. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. |
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,9 @@ | ||
require('dotenv').config(); | ||
|
||
const STRIPE_SECRET_KEY = process.env.STRIPE_SECRET_KEY; | ||
const STRIPE_ENDPOINT_SECRET = process.env.STRIPE_ENDPOINT_SECRET; | ||
|
||
module.exports = { | ||
STRIPE_SECRET_KEY, | ||
STRIPE_ENDPOINT_SECRET | ||
}; |
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,106 @@ | ||
const express = require("express"); | ||
const Stripe = require("stripe"); | ||
const { STRIPE_SECRET_KEY, STRIPE_ENDPOINT_SECRET } = require("./config"); | ||
|
||
const stripe = new Stripe(STRIPE_SECRET_KEY); | ||
|
||
const app = express(); | ||
|
||
// Middleware | ||
app.use(express.json({ | ||
verify: (req, res, buf) => { | ||
if (req.originalUrl.startsWith("/payment-webhook")) { | ||
req.rawBody = buf.toString(); | ||
} | ||
} | ||
})); | ||
app.use(express.urlencoded({ extended: true })); | ||
app.use(express.static(__dirname + "/public")); | ||
|
||
|
||
// Routes | ||
app.post("/create-payment-session", async (req, res) => { | ||
try { | ||
const { amount, currency } = req.body; | ||
const session = await stripe.checkout.sessions.create({ | ||
payment_method_types: ["card"], | ||
line_items: [ | ||
{ | ||
price_data: { | ||
currency, | ||
product_data: { | ||
name: "Technow Pro Subscription", | ||
description: "Monthly subscription to Technow Pro", | ||
images: ["https://picsum.photos/200/300"] | ||
}, | ||
unit_amount: amount * 100 // accepts amount in cents | ||
}, | ||
quantity: 1 | ||
} | ||
], | ||
payment_intent_data: { | ||
// metadata to store custom data and map in webhook | ||
// can only store string values, so stringify if needed | ||
metadata: { | ||
order_id: "12345", | ||
user_id: "67890" | ||
} | ||
}, | ||
mode: "payment", | ||
success_url: `${req.headers.origin}/payment-success`, | ||
cancel_url: `${req.headers.origin}/payment-cancel` | ||
}); | ||
res.status(200).json({ id: session.id, url: session.url }); | ||
} catch (error) { | ||
console.error("Error creating order:", error); | ||
res.status(500).json({ | ||
code: "Internal server error", | ||
message: error.message | ||
}); | ||
} | ||
}); | ||
|
||
app.post("/payment-webhook", async (req, res) => { | ||
console.log("Received webhook payload:", req.body); | ||
console.log("Received stripe webhook event"); | ||
const sig = req.headers["stripe-signature"]; | ||
let event; | ||
try { | ||
event = stripe.webhooks.constructEvent( | ||
req.rawBody, // raw request body. if express.raw() middleware is used, use req.body | ||
sig, | ||
STRIPE_ENDPOINT_SECRET | ||
); | ||
} catch (err) { | ||
console.log("Webhook Error: ", err.message); | ||
return res.status(400).send(`Webhook Error: ${err.message}`); | ||
} | ||
|
||
// Handle the event | ||
switch (event.type) { | ||
case "payment_intent.succeeded": | ||
console.log("Received payment_intent.succeeded event"); | ||
const paymentIntent = event.data.object; | ||
console.log("Payment success intent:", paymentIntent); | ||
// verify payment and update order status in database | ||
break; | ||
default: | ||
console.log(`Unhandled event type ${event.type}`); | ||
} | ||
}); | ||
|
||
app.get("/", (req, res) => res.send("Hello World")); | ||
app.get("/purchase", (req, res) => | ||
res.sendFile(__dirname + "/public/purchase.html") | ||
); | ||
app.get("/payment-cancel", (req, res) => | ||
res.sendFile(__dirname + "/public/payment-cancel.html") | ||
); | ||
app.get("/payment-success", (req, res) => | ||
res.sendFile(__dirname + "/public/payment-success.html") | ||
); | ||
|
||
const PORT = process.env.PORT || 3000; | ||
app.listen(PORT, () => | ||
console.log(`Server is running on http://localhost:${PORT}`) | ||
); |
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,194 @@ | ||
[ | ||
{ | ||
"order_id": "order_PZJiFDNompVq8E", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZJiRTrfw0Erhc" | ||
}, | ||
{ | ||
"order_id": "order_PZJk44KVGtbTJS", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZJkqXpqJRCSaF" | ||
}, | ||
{ | ||
"order_id": "order_PZJlSWqlP0HzoT", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZJlzNEt9bBjYk" | ||
}, | ||
{ | ||
"order_id": "order_PZJoLjckXdZTwC", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZJoe70qge5uh2" | ||
}, | ||
{ | ||
"order_id": "order_PZLZqt7g9WQ7DD", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZLZxl53ug33gY" | ||
}, | ||
{ | ||
"order_id": "order_PZMCvJ79m7BQ3j", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123" | ||
}, | ||
{ | ||
"order_id": "order_PZMDrVOJQZnvbQ", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZMDxJgvxQXpCb" | ||
}, | ||
{ | ||
"order_id": "order_PZMEm186wbMYor", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZMEqx81fXKEur" | ||
}, | ||
{ | ||
"order_id": "order_PZMGVDBrg2s2VW", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123" | ||
}, | ||
{ | ||
"order_id": "order_PZMGxtUVo8eiRe", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZMH2MybR2vKc6" | ||
}, | ||
{ | ||
"order_id": "order_PZMINXLgtjZfcz", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZMISQF1dkDlwp" | ||
}, | ||
{ | ||
"order_id": "order_PZMKFCeDmWNJS3", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZMKKZqOcuvBrS" | ||
}, | ||
{ | ||
"order_id": "order_PZMRLqmrTybO88", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZMRRhSm3DcYKh" | ||
}, | ||
{ | ||
"order_id": "order_PZMTO2dWo12PC3", | ||
"amount": 599900, | ||
"currency": "INR", | ||
"receipt": "order_rcptid_11", | ||
"notes": { | ||
"email": "[email protected]", | ||
"plan": "basic", | ||
"userId": "123" | ||
}, | ||
"createdBy": "123", | ||
"status": "paid", | ||
"payment_id": "pay_PZMTTH9G99xCBs" | ||
} | ||
] |
Oops, something went wrong.