Skip to content

Commit

Permalink
api for stripe payment method
Browse files Browse the repository at this point in the history
  • Loading branch information
mohiuddin2721 committed May 28, 2022
1 parent fadabfe commit 1a47f23
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
35 changes: 33 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jwt = require('jsonwebtoken');
require('dotenv').config();
const { MongoClient, ServerApiVersion, ObjectId } = require('mongodb');
const app = express();
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const port = process.env.PORT || 5000;

app.use(cors());
Expand Down Expand Up @@ -38,6 +39,7 @@ async function run() {
const reviewsCollection = client.db("car_parts").collection("reviews");
const profileCollection = client.db("car_parts").collection("profile");
const usersCollection = client.db("car_parts").collection("users");
const paymentsCollection = client.db("car_parts").collection("payments");

// verify admin middle
const verifyAdmin = async (req, res, next) => {
Expand Down Expand Up @@ -81,6 +83,19 @@ async function run() {
res.send(result);
});

// post for payment stripe
app.post('/create-payment-intent', verifyJWT, async (req, res) => {
const order = req.body;
const price = order.price;
const amount = price * 100;
const paymentIntent = await stripe.paymentIntents.create({
amount: amount,
currency: 'usd',
payment_method_types: ['card']
});
res.send({clientSecret: paymentIntent.client_secret})
})

// post parts for addProduct
app.post('/parts', verifyJWT, verifyAdmin, async (req, res) => {
const product = req.body;
Expand Down Expand Up @@ -110,13 +125,29 @@ async function run() {
});

// get order by id for myOrder page
app.get('/orders/:id', verifyJWT, async(req, res) => {
app.get('/orders/:id', verifyJWT, async (req, res) => {
const id = req.params.id;
const query = {_id: ObjectId(id)}
const query = { _id: ObjectId(id) }
const order = await ordersCollection.findOne(query);
res.send(order);
})

// patch payment paid
app.patch('/orders/:id', verifyJWT, async(req, res) => {
const id = req.params.id;
const payment = req.body;
const filter = {_id: ObjectId(id)};
const updatedDoc = {
$set: {
paid: true,
transactionId: payment.transactionId
}
}
const updatePayment = await paymentsCollection.insertOne(payment);
const updatedOrder = await ordersCollection.updateOne(filter, updatedDoc);
res.send(updatedOrder);
})

// get reviews
app.get('/reviews', async (req, res) => {
const query = {}
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dotenv": "^16.0.1",
"express": "^4.18.1",
"jsonwebtoken": "^8.5.1",
"mongodb": "^4.6.0"
"mongodb": "^4.6.0",
"stripe": "^9.5.0"
}
}

0 comments on commit 1a47f23

Please sign in to comment.