-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
18 changed files
with
314 additions
and
42 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
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
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
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 |
---|---|---|
|
@@ -8,5 +8,8 @@ | |
}, | ||
"devDependencies": { | ||
"concurrently": "^8.2.2" | ||
}, | ||
"dependencies": { | ||
"razorpay": "^2.9.4" | ||
} | ||
} | ||
} |
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
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,18 @@ | ||
export enum PaymentStatus { | ||
PENDING = "PENDING", | ||
COMPLETED = "COMPLETED", | ||
FAILED = "FAILED", | ||
} | ||
|
||
export default interface IPayment { | ||
_id?: string; | ||
orderId: string; | ||
paymentId?: string; | ||
appointmentId?: string; | ||
amount?: number; | ||
currency?: string; | ||
status?: PaymentStatus; | ||
createdAt?: Date; | ||
updatedAt?: Date; | ||
razorpaySignature?: string; | ||
} |
3 changes: 2 additions & 1 deletion
3
server/src/domain/interface/repositories/IAppointmentRepository.ts
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import IAppointment, { AppointmentStatus } from "../../entities/IAppointment"; | ||
|
||
export default interface IAppointmentRepository { | ||
create(appointment: IAppointment): Promise<void>; | ||
create(appointment: IAppointment): Promise<string>; | ||
update(appointment: IAppointment): Promise<void>; | ||
updateStatusMany(appointmentIds: string[], status: AppointmentStatus): Promise<void>; | ||
updateManyBySlotIds(slotIds: string[], fields: IAppointment): Promise<void>; | ||
findOneBySlotId(slotId: string): Promise<IAppointment | null>; | ||
findByDateAndSlot(appointmentDate: string, slotId:string): Promise<IAppointment | null>; | ||
findManyByDateAndDoctorId(appointmentDate:string,doctorId:string):Promise<IAppointment[] | null>; | ||
updateAppointmentStatusToConfirmed(appointmentId:string):Promise<void> | ||
} |
8 changes: 8 additions & 0 deletions
8
server/src/domain/interface/repositories/IPaymentRepository.ts
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,8 @@ | ||
import IPayment from "../../entities/IPayment"; | ||
|
||
export default interface IPaymentRepository { | ||
create(payment: IPayment): Promise<IPayment>; | ||
findById(id: string): Promise<IPayment | null>; | ||
findByOrderId(orderId: string): Promise<IPayment | null>; | ||
update(payment: IPayment): Promise<void>; | ||
} |
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,12 @@ | ||
export default interface IPaymentService { | ||
createOrder(amount: number, currency: string, receipt: string): Promise<RazorpayOrder>; | ||
verifyPaymentSignature(signature: string, orderId: string, paymentId: string): Promise<void>; | ||
} | ||
|
||
export interface RazorpayOrder { | ||
id: string; | ||
amount: string | number; | ||
currency: string; | ||
receipt?: string; | ||
status: string; | ||
} |
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
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,47 @@ | ||
import { model, Schema } from 'mongoose'; | ||
import IPayment, { PaymentStatus } from '../../domain/entities/IPayment'; | ||
|
||
const paymentSchema = new Schema<IPayment>( | ||
{ | ||
orderId: { | ||
type: String, | ||
required: true, | ||
}, | ||
paymentId: { | ||
type: String, | ||
default: null, | ||
}, | ||
appointmentId: { | ||
type: Schema.Types.ObjectId, | ||
ref: 'Appointment', | ||
required: true, | ||
index: true, | ||
}, | ||
amount: { | ||
type: Number, | ||
required: true, | ||
}, | ||
currency: { | ||
type: String, | ||
required: true, | ||
}, | ||
status: { | ||
type: String, | ||
enum: Object.values(PaymentStatus), | ||
default: PaymentStatus.PENDING, | ||
required: true, | ||
}, | ||
razorpaySignature: { | ||
type: String, | ||
default: null, | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
versionKey: false, | ||
minimize: false, | ||
} | ||
); | ||
const PaymentModel = model<IPayment>('Payment', paymentSchema); | ||
|
||
export default PaymentModel; |
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
26 changes: 26 additions & 0 deletions
26
server/src/infrastructure/repositories/PaymentRepository.ts
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,26 @@ | ||
// src/repositories/PaymentRepository.ts | ||
|
||
import IPayment from "../../domain/entities/IPayment"; | ||
import IPaymentRepository from "../../domain/interface/repositories/IPaymentRepository"; | ||
import PaymentModel from "../database/PaymentModel"; | ||
|
||
export default class PaymentRepository implements IPaymentRepository { | ||
model = PaymentModel; | ||
|
||
async create(payment: IPayment): Promise<IPayment> { | ||
return await this.model.create(payment); | ||
} | ||
|
||
async findById(id: string): Promise<IPayment | null> { | ||
return await this.model.findById(id).exec(); | ||
} | ||
|
||
async findByOrderId(orderId: string): Promise<IPayment | null> { | ||
return await this.model.findOne({ orderId }).exec(); | ||
} | ||
|
||
async update(payment: IPayment): Promise<void> { | ||
const { _id, ...updateData } = payment; | ||
await this.model.findByIdAndUpdate(_id, updateData, { new: true }).exec(); | ||
} | ||
} |
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,52 @@ | ||
import Razorpay from 'razorpay'; | ||
import IPaymentService, { RazorpayOrder } from '../../domain/interface/services/IPaymentService'; | ||
import { StatusCode } from '../../types'; | ||
import CustomError from '../../domain/entities/CustomError' | ||
import * as crypto from 'crypto'; | ||
import logger from '../../utils/logger'; | ||
|
||
|
||
export default class RazorPayService implements IPaymentService { | ||
private razorpay: Razorpay; | ||
|
||
constructor() { | ||
this.razorpay = new Razorpay({ | ||
key_id: process.env.RAZORPAY_KET_ID!, | ||
key_secret: process.env.RAZORPAY_KEY_SECRET!, | ||
}); | ||
} | ||
|
||
async createOrder(amount: number, currency: string, receipt: string): Promise<RazorpayOrder> { | ||
const options = { | ||
amount: amount * 100, | ||
currency, | ||
receipt, | ||
payment_capture: 1, | ||
}; | ||
|
||
try { | ||
const order = await this.razorpay.orders.create(options); | ||
return { | ||
...order, | ||
amount: typeof order.amount === 'string' ? parseInt(order.amount, 10) : order.amount, | ||
}; | ||
} catch (error) { | ||
logger.error(error) | ||
throw new CustomError('Error creating Razorpay order', StatusCode.PaymentError); | ||
} | ||
} | ||
|
||
async verifyPaymentSignature(signature: string, orderId: string, paymentId: string): Promise<void> { | ||
try { | ||
const generatedSignature = crypto.createHmac('sha256', process.env.RAZORPAY_KEY_SECRET!) | ||
.update(orderId + '|' + paymentId) | ||
.digest('hex'); | ||
|
||
if (generatedSignature !== signature) { | ||
throw new CustomError('Invalid payment signature', StatusCode.PaymentError); | ||
} | ||
} catch (error) { | ||
throw new CustomError('Error verifying payment signature', StatusCode.PaymentError); | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.
aabb4b8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
avm-care – ./
avm-care.vercel.app
avm-care-git-main-sinans-projects-8d312afe.vercel.app
avm-care-sinans-projects-8d312afe.vercel.app