Skip to content

Commit

Permalink
Intergrated payment for appointments
Browse files Browse the repository at this point in the history
  • Loading branch information
3laaHisham committed Nov 14, 2023
1 parent 531b4f3 commit d1ee8c1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 49 deletions.
18 changes: 16 additions & 2 deletions client/src/sections/doctors/doctor-slot.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
import { useNavigate } from 'react-router-dom';

import Stack from '@mui/material/Stack';
import Box from '@mui/material/Box';
import Typography from '@mui/material/Typography';
import Divider from '@mui/material/Divider';
import Link from '@mui/material/Link';
import Button from '@mui/material/Button';

import { useRouter } from 'src/routes/hooks';
import { axiosInstance } from '../../utils/axiosInstance';

export default function DoctorDaySlots({ day, slots, doctorID }) {
const navigate = useNavigate();

const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const today = daysOfWeek[new Date().getDay()];

if (today == day) day = 'Today';

const reserveSlot = (slot) => {
axiosInstance
.post(`/payment/session/oneTimePayment`, {
products: [{ name: `${slot.startDate} - ${slot.endDate}`, price: slot.sessionPrice, quantity: 1 }]
})
.then((res) => {
window.location.replace(res.data.url);
})
.catch((err) => console.log(err));

axiosInstance
.post(`/doctors/${doctorID}/appointments`, {
startDate: slot.startDate,
Expand All @@ -38,8 +52,8 @@ export default function DoctorDaySlots({ day, slots, doctorID }) {
const startDate = new Date(slot.startDate);
const endDate = new Date(slot.endDate);

const from = `${startDate.getHours()}: ${startDate.getMinutes()}`;
const to = `${endDate.getHours()}: ${endDate.getMinutes()}`;
const from = `${startDate.getHours()}:${startDate.getMinutes()}`;
const to = `${endDate.getHours()}:${endDate.getMinutes()}`;

return (
<Stack>
Expand Down
92 changes: 45 additions & 47 deletions server/src/routes/payment.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import express, { Request, Response } from 'express';
import controller from '../controllers';
import { isAuthenticated } from '../middlewares';
import {} from '../services';
import {stripe} from '../utils/stripe'
import { stripe } from '../utils/stripe';

const router = express.Router();

Expand All @@ -22,47 +22,46 @@ interface Product {
quantity: number;
}

router.post('/session/subscription/:priceAmount/:productName',async(req,res)=>{
try{
const product = await stripe.products.create({
name: req.params.productName,
type: 'service',
});
const priceAmount = parseInt(req.params.priceAmount, 10);
const price = await stripe.prices.create({
unit_amount: priceAmount * 100,
currency: 'usd',
recurring: {
interval: 'month',
},
product: product.id,
});
const session = await stripe.checkout.sessions.create({
mode : "subscription",
payment_method_types:["card"],
line_items: [
router.post('/session/subscription/:priceAmount/:productName', async (req, res) => {
try {
const product = await stripe.products.create({
name: req.params.productName,
type: 'service'
});
const priceAmount = parseInt(req.params.priceAmount, 10);
const price = await stripe.prices.create({
unit_amount: priceAmount * 100,
currency: 'usd',
recurring: {
interval: 'month'
},
product: product.id
});
const session = await stripe.checkout.sessions.create(
{
mode: 'subscription',
payment_method_types: ['card'],
line_items: [
{
price: price.id,
quantity: 1
}
],

success_url: 'http://localhost:3030',
cancel_url: 'http://localhost:3030',
customer: 'cus_P02l6C1LLVGNmW' //fixed customerID in stripe
},
{
price: price.id,
quantity: 1
apiKey: process.env.STRIPE_SECRET_KEY
}
],

success_url: "http://localhost:3030",
cancel_url: "http://localhost:3030",
customer : "cus_P02l6C1LLVGNmW", //fixed customerID in stripe


},{
apiKey: process.env.STRIPE_SECRET_KEY,
})
);

return res.json(session);
}
catch(error){
console.error('Error creating checkout session,error');
return res.status(500).json({ error: 'Failed to create checkout session' });
}

return res.json(session);
} catch (error) {
console.error('Error creating checkout session,error');
return res.status(500).json({ error: 'Failed to create checkout session' });
}
});

router.post('/session/oneTimePayment', async (req, res) => {
Expand All @@ -72,38 +71,38 @@ router.post('/session/oneTimePayment', async (req, res) => {

// Create products in Stripe
const stripeProducts = await Promise.all(
products.map(async (product:Product) => {
products.map(async (product: Product) => {
return await stripe.products.create({
name: product.name,
type: 'service',
type: 'service'
});
})
);

const stripePrices = await Promise.all(
products.map(async (product:Product, index:number) => {
products.map(async (product: Product, index: number) => {
const productObj = stripeProducts[index];

return await stripe.prices.create({
unit_amount: product.price * 100, // assuming the price is in cents
currency: 'usd',
product: productObj.id,
product: productObj.id
});
})
);

// Create line items for the Checkout Session
const lineItems = stripePrices.map((price, index) => ({
price: price.id,
quantity: products[index].quantity,
quantity: products[index].quantity
}));

const session = await stripe.checkout.sessions.create({
mode: 'payment',
payment_method_types: ['card','alipay'],
payment_method_types: ['card'],
line_items: lineItems,
success_url: 'http://localhost:3030',
cancel_url: 'http://localhost:3030',
cancel_url: 'http://localhost:3030'
});

return res.json(session);
Expand All @@ -113,7 +112,6 @@ router.post('/session/oneTimePayment', async (req, res) => {
}
});


router.post('/payment/checkout', (req: Request, res: Response) => {
// Handle payment checkout logic here
// controller(res)()();
Expand Down

0 comments on commit d1ee8c1

Please sign in to comment.