mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 18:48:11 +00:00
Merge pull request #162 from Tria-plc/feat/payment-microservice
Update payments.service.ts
This commit is contained in:
@@ -43,6 +43,14 @@ const NON_TERMINAL_STATUSES: PaymentIntentStatus[] = [
|
|||||||
export class PaymentsService {
|
export class PaymentsService {
|
||||||
private readonly logger = new Logger(PaymentsService.name);
|
private readonly logger = new Logger(PaymentsService.name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DEMO ONLY: when true, a WALLET "payment" is treated as instantly successful — the wallet
|
||||||
|
* balance check and debit are skipped and the booking is confirmed + ticket issued as if fully
|
||||||
|
* paid. Lets the happy-path be demoed while a real provider (e.g. Telebirr) is unavailable.
|
||||||
|
* Never enable in production. Toggle with WALLET_DEMO_AUTO_SUCCEED in the env.
|
||||||
|
*/
|
||||||
|
private readonly walletDemoAutoSucceed = true;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private prisma: PrismaService,
|
private prisma: PrismaService,
|
||||||
private seatsService: SeatsService,
|
private seatsService: SeatsService,
|
||||||
@@ -196,6 +204,35 @@ export class PaymentsService {
|
|||||||
private async initiateWalletPayment(
|
private async initiateWalletPayment(
|
||||||
booking: Prisma.BookingGetPayload<{ include: { seats: true } }>,
|
booking: Prisma.BookingGetPayload<{ include: { seats: true } }>,
|
||||||
): Promise<InitiateResponseDto> {
|
): Promise<InitiateResponseDto> {
|
||||||
|
// DEMO ONLY (WALLET_DEMO_AUTO_SUCCEED): pretend the payment succeeded — no balance check,
|
||||||
|
// no debit — and run the exact same finalize path a real successful payment uses
|
||||||
|
// (booking → CONFIRMED, seats confirmed, ticket issued). Remove once a real provider works.
|
||||||
|
if (this.walletDemoAutoSucceed) {
|
||||||
|
this.logger.warn(
|
||||||
|
`WALLET_DEMO_AUTO_SUCCEED enabled — faking a successful WALLET payment for booking ${booking.bookingRef} (${booking.id})`,
|
||||||
|
);
|
||||||
|
const demoIntent = await this.prisma.paymentIntent.upsert({
|
||||||
|
where: { bookingId: booking.id },
|
||||||
|
update: {
|
||||||
|
status: PaymentIntentStatus.PROCESSING,
|
||||||
|
failureCode: null,
|
||||||
|
method: PaymentMethodType.WALLET,
|
||||||
|
},
|
||||||
|
create: {
|
||||||
|
bookingId: booking.id,
|
||||||
|
amountMinor: booking.totalMinor,
|
||||||
|
method: PaymentMethodType.WALLET,
|
||||||
|
status: PaymentIntentStatus.PROCESSING,
|
||||||
|
providerRef: `WALLET-DEMO-${Date.now()}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
await this.finalizePaymentSuccess({ intentId: demoIntent.id });
|
||||||
|
const settled = await this.prisma.paymentIntent.findUniqueOrThrow({
|
||||||
|
where: { id: demoIntent.id },
|
||||||
|
});
|
||||||
|
return this.formatIntentResponse(settled);
|
||||||
|
}
|
||||||
|
|
||||||
const debitResult = await this.prisma.$transaction(async (tx) => {
|
const debitResult = await this.prisma.$transaction(async (tx) => {
|
||||||
const wallet = await tx.walletAccount.findUnique({
|
const wallet = await tx.walletAccount.findUnique({
|
||||||
where: { passengerId: booking.passengerId },
|
where: { passengerId: booking.passengerId },
|
||||||
|
|||||||
Reference in New Issue
Block a user