Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Salmandabbakuti committed Dec 19, 2024
0 parents commit 7ad68c3
Show file tree
Hide file tree
Showing 10 changed files with 1,700 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RAZORPAY_KEY_ID=your_key_id
RAZORPAY_KEY_SECRET=your_key_secret
21 changes: 21 additions & 0 deletions .gitignore
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.
21 changes: 21 additions & 0 deletions LICENSE
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.
9 changes: 9 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require('dotenv').config();

const RAZORPAY_KEY_ID = process.env.RAZORPAY_KEY_ID;
const RAZORPAY_KEY_SECRET = process.env.RAZORPAY_KEY_SECRET;

module.exports = {
RAZORPAY_KEY_ID,
RAZORPAY_KEY_SECRET,
};
79 changes: 79 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const express = require('express');
const Razorpay = require('razorpay');
const { validateWebhookSignature } = require('razorpay/dist/utils/razorpay-utils');
const app = express();
const { RAZORPAY_KEY_ID, RAZORPAY_KEY_SECRET } = require('./config');

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// serve static files
app.use(express.static(__dirname + '/public'));

const razorpay = new Razorpay({
key_id: RAZORPAY_KEY_ID,
key_secret: RAZORPAY_KEY_SECRET,
});

app.post("/create-order", async (req, res) => {
console.log("creating order", req.body);
const { amount, currency } = req.body;
const options = {
amount: amount * 100, // amount in the smallest currency unit
currency,
receipt: "order_rcptid_11",
// custom metadata object from request auth token or database
notes: {
userId: "123",
plan: "basic",
email: "[email protected]"
}
};
try {
const response = await razorpay.orders.create(options);
res.status(200).send(response);
} catch (error) {
console.log(error);
res.status(500).send(error);
}
});

app.post("/verify-payment", async (req, res) => {
console.log("verifying payment webhook", req.body);
const { razorpay_order_id, razorpay_payment_id, razorpay_signature } = req.body;
const body = razorpay_order_id + '|' + razorpay_payment_id;
try {
const isValidSignature = validateWebhookSignature(body, razorpay_signature, RAZORPAY_KEY_SECRET);
if (isValidSignature) {
// Update the order with payment details in your database
console.log("Payment verification successful");
res.status(200).json({ status: 'ok' });
} else {
console.log("Payment verification failed");
res.status(400).json({ status: 'verification_failed' });
}
} catch (error) {
console.error("Error verifying payment", error);
res.status(500).json({ status: 'error', message: "Failed to verify payment." });
}
});

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');
});

app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Loading

0 comments on commit 7ad68c3

Please sign in to comment.