From 828f93d1602a7765a1313c0e34557beb6f9e74fd Mon Sep 17 00:00:00 2001 From: Tria Date: Sat, 6 Jun 2026 08:13:38 +0300 Subject: [PATCH] minor update --- .../dto/initiate-booking-payment.dto.ts | 6 ++++ .../src/modules/payment/payment.controller.ts | 33 +++---------------- .../src/modules/payment/payment.service.ts | 15 ++++++--- .../strategies/payment.telebirr.strategy.ts | 11 +++---- .../payment/strategies/payments.types.ts | 2 +- 5 files changed, 28 insertions(+), 39 deletions(-) create mode 100644 apps/edr-freight-api/src/modules/payment/dto/initiate-booking-payment.dto.ts diff --git a/apps/edr-freight-api/src/modules/payment/dto/initiate-booking-payment.dto.ts b/apps/edr-freight-api/src/modules/payment/dto/initiate-booking-payment.dto.ts new file mode 100644 index 000000000..3ff5f1798 --- /dev/null +++ b/apps/edr-freight-api/src/modules/payment/dto/initiate-booking-payment.dto.ts @@ -0,0 +1,6 @@ +import { IsString } from "class-validator"; + +export class InitiateBookingPayment { + @IsString() + bookingId!: string; +} \ No newline at end of file diff --git a/apps/edr-freight-api/src/modules/payment/payment.controller.ts b/apps/edr-freight-api/src/modules/payment/payment.controller.ts index 05b127968..140800c89 100644 --- a/apps/edr-freight-api/src/modules/payment/payment.controller.ts +++ b/apps/edr-freight-api/src/modules/payment/payment.controller.ts @@ -4,9 +4,9 @@ import { Public } from "@edr/api-common"; import { randomUUID } from "crypto"; import { Response } from "express" -import * as fs from 'fs'; -import * as path from 'path'; -import Handlebars from 'handlebars'; +// import * as fs from 'fs'; +// import * as path from 'path'; +// import Handlebars from 'handlebars'; @Public() @@ -21,13 +21,12 @@ export class PaymentController { return res.send(filled) } - @Post("/initiate") + @Post("/initiate/booking") async initiatePayment() { //Only for testing.. - const redirectBaseURL = "http://localhost:3004/payment" const description = "booking" - const data = await this.paymentService.pay(redirectBaseURL, 20, "ETB", "telebirr", description, (_) => { + const data = await this.paymentService.pay(20, "ETB", "telebirr", description, "booking", (_) => { return new Promise((resp, _) => { resp({ id: randomUUID(), @@ -64,26 +63,4 @@ export class PaymentController { `); } - - // @Get("test") - // handleTest(@Res() res: Response) { - - - // const filePath = path.join(__dirname, "templates", "payment.hbs"); - // console.log(filePath) - // console.log(__dirname) - // if (fs.existsSync(filePath)) { - // const source = fs.readFileSync(filePath, "utf8"); - // const template = Handlebars.compile(source); - - // const html = template({ - // url: "https://example.com" - // }); - - // res.send(html) - // } - // } - - - } \ No newline at end of file diff --git a/apps/edr-freight-api/src/modules/payment/payment.service.ts b/apps/edr-freight-api/src/modules/payment/payment.service.ts index b714d5175..e89fd5157 100644 --- a/apps/edr-freight-api/src/modules/payment/payment.service.ts +++ b/apps/edr-freight-api/src/modules/payment/payment.service.ts @@ -28,7 +28,7 @@ export class PaymentService { ]) } - async pay(redirectBaseURL: string, amount: number, currency: CurrencyType, method: PaymentMethod, reason: string, cb: (qr: QueryRunner) => Promise<{ id: string, type: PaymentEntity["type"] }>, payform: PaymentPlatform = "web"): Promise<{ + async pay(amount: number, currency: CurrencyType, method: PaymentMethod, reason: string, type: PaymentEntity["type"], cb: (qr: QueryRunner) => Promise<{ id: string, type: PaymentEntity["type"] }>, payform: PaymentPlatform = "web"): Promise<{ refId: string, clientAction: ClientAction, status: PaymentEntity["status"], @@ -43,9 +43,16 @@ export class PaymentService { throw new NotFoundException("strategy not found") } - const orderId = `freigh${Date.now()}${crypto.randomBytes(4).toString('hex')}` //todo: make it dynamic + const orderId = `${Date.now()}${crypto.randomBytes(4).toString('hex')}` //todo: make it dynamic + let redirectUrl: string; + switch (type) { + case "booking": + redirectUrl = `http://localhost:3001/api/payments/receipts/${orderId}/html` + break; + } + const paymentResp = await strategy.pay({ - redirectBaseURL, + redirectUrl, amountMinor: amount, currency: currency, merchantOrderId: orderId, @@ -104,7 +111,7 @@ export class PaymentService { throw new BadRequestException() } - const filePath = path.join(__dirname, "templates", "ceiepts.hbs"); + const filePath = path.join(__dirname, "templates", "receipt.hbs"); if (!fs.existsSync(filePath)) { throw new InternalServerErrorException() } diff --git a/apps/edr-freight-api/src/modules/payment/strategies/payment.telebirr.strategy.ts b/apps/edr-freight-api/src/modules/payment/strategies/payment.telebirr.strategy.ts index 17a255dea..a292863d8 100644 --- a/apps/edr-freight-api/src/modules/payment/strategies/payment.telebirr.strategy.ts +++ b/apps/edr-freight-api/src/modules/payment/strategies/payment.telebirr.strategy.ts @@ -22,8 +22,7 @@ export class PaymentTelebirrStrategy implements PaymentStrategy { async pay(data: ProviderInitiationInput): Promise { // const refId = randomUUID() // const orderId = createMerchantOrderId() - const redirectURL = `${data.redirectBaseURL}/${data.merchantOrderId}` - const resp = await this.initiate(redirectURL, data) + const resp = await this.initiate(data) return resp; } @@ -45,9 +44,9 @@ export class PaymentTelebirrStrategy implements PaymentStrategy { }); } - async initiate(redirectURL: string, input: ProviderInitiationInput): Promise { + async initiate(input: ProviderInitiationInput): Promise { const fabricToken = await this.applyFabricToken(); - const requestBody = this.buildCreateOrderRequest(redirectURL, input); + const requestBody = this.buildCreateOrderRequest(input); const response = await this.requestCreateOrder(fabricToken, requestBody); const prepayId = response.biz_content?.prepay_id; @@ -177,7 +176,7 @@ export class PaymentTelebirrStrategy implements PaymentStrategy { ); } - private buildCreateOrderRequest(redirectURL: string, input: ProviderInitiationInput): CreateOrderRequest { + private buildCreateOrderRequest(input: ProviderInitiationInput): CreateOrderRequest { // const totalAmount = String(input.amountMinor / 100); const totalAmount = String(input.amountMinor) const req = { @@ -188,7 +187,7 @@ export class PaymentTelebirrStrategy implements PaymentStrategy { biz_content: { notify_url: this.notifyUrl, appid: this.merchantAppId, - redirect_url: redirectURL, + redirect_url: input.redirectUrl, merch_code: this.merchantCode, merch_order_id: input.merchantOrderId, trade_type: 'Checkout' as const, diff --git a/apps/edr-freight-api/src/modules/payment/strategies/payments.types.ts b/apps/edr-freight-api/src/modules/payment/strategies/payments.types.ts index dd23f8287..75a1c8bdc 100644 --- a/apps/edr-freight-api/src/modules/payment/strategies/payments.types.ts +++ b/apps/edr-freight-api/src/modules/payment/strategies/payments.types.ts @@ -10,7 +10,7 @@ export type ClientAction = | { type: 'LAUNCH_APP'; prepayId: string; receiveCode?: string; shortCode: string }; export interface ProviderInitiationInput { - redirectBaseURL: string; + redirectUrl: string; merchantOrderId: string; // bookingRef: string; amountMinor: number;