Skip to content

Commit

Permalink
verification is added in payment
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkunal2k committed Nov 5, 2022
1 parent 6149b89 commit 3c90a16
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ MONGO_URI=mongodb://localhost:27017/sem5backend
FIREBASE_PRIVATE_KEY=
JWT_SECRET=sdlhfldsfskdfjldjflsdf
JWT_EXPIRES_IN=90d
SESSION_SECRET=skdjfhsdlkfhskdfhnewr
SESSION_SECRET=skdjfhsdlkfhskdfhnewr
KEY_ID=
KEY_SECRET=
7 changes: 7 additions & 0 deletions model/payment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const mongoose = require('mongoose')

const paymentDetailsSchema = new mongoose.Schema({
orderId: {
type: String,
required: true
},
amount: {
type: Number
},
receiptId: {
type: String
},
Expand Down
61 changes: 51 additions & 10 deletions routes/razorpay.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
const express = require('express');
const app = express.Router();
const Razorpay = require('razorpay');
const PaymentDetail = require('../model/payment')
const PaymentDetail = require('../model/payment');
const crypto = require('crypto');

app.post('/', async (req, res) => {
try{
let {amount} = req.body;
console.log('payment');
var instance = new Razorpay({
key_id: "rzp_test_p4B7WH2gc3wU12",
key_secret: "bvCQXenWJVnTzKwKCkJLyjBt"
key_id: process.env.KEY_ID,
key_secret: process.env.KEY_SECRET,
});

let order = await instance.orders.create({
await instance.orders.create({
amount: amount * 100,
currency: "INR",
receipt: "receipt#1",
});

res.status(201).json({
success: true,
order,
amount,
}).then(async (response) => {
// Save orderId and other payment details
const paymentDetail = new PaymentDetail({
orderId: response.id,
receiptId: response.receipt,
amount: response.amount,
currency: response.currency,
createdAt: response.created_at,
status: response.status
})

try {
// Render Order Confirmation page if saved succesfully
await paymentDetail.save()
res.status(201).json({
success: true,
order:response,
amount,
});

} catch (err) {
// Throw err if failed to save
if (err) throw err;
}
});
}
catch (err) {
Expand All @@ -30,4 +49,26 @@ app.post('/', async (req, res) => {
}
});

app.post('/verify', (req, res) => {
try {
const { razorpay_order_id, razorpay_payment_id, razorpay_signature } =
req.body;
const sign = razorpay_order_id + "|" + razorpay_payment_id;
const expectedSign = crypto
.createHmac("sha256", process.env.KEY_SECRET)
.update(sign.toString())
.digest("hex");

if (razorpay_signature === expectedSign) {
return res.status(200).json({ message: "Payment verified successfully" });
} else {
return res.status(400).json({ message: "Invalid signature sent!" });
}
} catch (err) {
console.log(err);
res.status(500).json({ message: "Internal Server Error!" });

}
})

module.exports = app;
10 changes: 7 additions & 3 deletions views/checkout.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@

<script src="https://checkout.razorpay.com/v1/checkout.js"></script>

<script>


</body>
<script>
window.addEventListener('load', function () {
document.getElementById('rzp-button1').onclick = async function (e) {
e.preventDefault();
Expand Down Expand Up @@ -254,8 +258,8 @@
rzp1.open();
};
</script>
})
</body>
</script>

</html>

0 comments on commit 3c90a16

Please sign in to comment.