From 492e538eb86eba54c57cf5addfb77325c0094eb7 Mon Sep 17 00:00:00 2001 From: Abdalhamid Alhamad Date: Sun, 6 Jul 2025 16:44:23 +0300 Subject: [PATCH] feat: send request via gateway --- .../neoleap-webhooks.controller.ts | 7 ++-- .../neoleap/controllers/neotest.controller.ts | 2 +- .../neoleap/services/neoleap.service.ts | 39 +++++++------------ 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/common/modules/neoleap/controllers/neoleap-webhooks.controller.ts b/src/common/modules/neoleap/controllers/neoleap-webhooks.controller.ts index 0089a66..ae0aadb 100644 --- a/src/common/modules/neoleap/controllers/neoleap-webhooks.controller.ts +++ b/src/common/modules/neoleap/controllers/neoleap-webhooks.controller.ts @@ -7,8 +7,9 @@ import { CardTransactionWebhookRequest } from '../dtos/requests'; @ApiTags('Neoleap Webhooks') export class NeoLeapWebhooksController { constructor(private readonly transactionService: TransactionService) {} - @Post('account-transaction') - async handleAccountTransactionWebhook(@Body() body: CardTransactionWebhookRequest) { - await this.transactionService.createCardTransaction(body); + + @Post('card-transaction') + async handleCardTransactionWebhook(@Body() body: CardTransactionWebhookRequest) { + return this.transactionService.createCardTransaction(body); } } diff --git a/src/common/modules/neoleap/controllers/neotest.controller.ts b/src/common/modules/neoleap/controllers/neotest.controller.ts index 5d8ff8a..f7ba156 100644 --- a/src/common/modules/neoleap/controllers/neotest.controller.ts +++ b/src/common/modules/neoleap/controllers/neotest.controller.ts @@ -30,7 +30,7 @@ export class NeoTestController { @ApiDataResponse(CreateApplicationResponse) async createApplication() { const customer = await this.customerService.findCustomerById( - this.configService.get('MOCK_CUSTOMER_ID', '0778c431-f604-4b91-af53-49c33849b5ff'), + this.configService.get('MOCK_CUSTOMER_ID', 'ff462af1-1e0c-4216-8865-738e5b525ac1'), ); const data = await this.neoleapService.createApplication(customer as Customer); await this.cardService.createCard(customer.id, data); diff --git a/src/common/modules/neoleap/services/neoleap.service.ts b/src/common/modules/neoleap/services/neoleap.service.ts index 3c7802b..05ff545 100644 --- a/src/common/modules/neoleap/services/neoleap.service.ts +++ b/src/common/modules/neoleap/services/neoleap.service.ts @@ -2,10 +2,7 @@ import { HttpService } from '@nestjs/axios'; import { BadRequestException, Injectable, InternalServerErrorException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { ClassConstructor, plainToInstance } from 'class-transformer'; -import { readFileSync } from 'fs'; -import { Agent } from 'https'; import moment from 'moment'; -import path from 'path'; import { v4 as uuid } from 'uuid'; import { CountriesNumericISO } from '~/common/constants'; import { Customer } from '~/customer/entities'; @@ -17,22 +14,22 @@ import { ICreateApplicationRequest, IInquireApplicationRequest, INeoleapHeaderRe export class NeoLeapService { private readonly baseUrl: string; private readonly apiKey: string; - private readonly useMock: boolean; + private readonly useGateway: boolean; private readonly institutionCode = '1100'; useLocalCert: boolean; constructor(private readonly httpService: HttpService, private readonly configService: ConfigService) { - this.baseUrl = this.configService.getOrThrow('NEOLEAP_BASE_URL'); - this.apiKey = this.configService.getOrThrow('NEOLEAP_API_KEY'); - this.useMock = [true, 'true'].includes(this.configService.get('USE_MOCK', false)); + this.baseUrl = this.configService.getOrThrow('GATEWAY_URL'); + this.apiKey = this.configService.getOrThrow('GATEWAY_API_KEY'); + this.useGateway = [true, 'true'].includes(this.configService.get('USE_GATEWAY', false)); this.useLocalCert = this.configService.get('USE_LOCAL_CERT', false); } async createApplication(customer: Customer) { const responseKey = 'CreateNewApplicationResponseDetails'; - if (customer.cards.length > 0) { - throw new BadRequestException('CUSTOMER.ALREADY_HAS_CARD'); - } - if (this.useMock) { + // if (customer.cards.length > 0) { + // throw new BadRequestException('CUSTOMER.ALREADY_HAS_CARD'); + // } + if (!this.useGateway) { return plainToInstance(CreateApplicationResponse, CREATE_APPLICATION_MOCK[responseKey], { excludeExtraneousValues: true, }); @@ -42,7 +39,7 @@ export class NeoLeapService { CreateNewApplicationRequestDetails: { ApplicationRequestDetails: { InstitutionCode: this.institutionCode, - ExternalApplicationNumber: (customer.waitingNumber * 64).toString(), + ExternalApplicationNumber: customer.waitingNumber.toString(), ApplicationType: '01', Product: '1101', ApplicationDate: moment().format('YYYY-MM-DD'), @@ -105,7 +102,7 @@ export class NeoLeapService { async inquireApplication(externalApplicationNumber: string) { const responseKey = 'InquireApplicationResponseDetails'; - if (this.useMock) { + if (!this.useGateway) { return plainToInstance(InquireApplicationResponse, INQUIRE_APPLICATION_MOCK[responseKey], { excludeExtraneousValues: true, }); @@ -152,29 +149,21 @@ export class NeoLeapService { responseClass: ClassConstructor, ): Promise { try { - const response = await this.httpService.axiosRef.post(`${this.baseUrl}/${endpoint}`, payload, { + const { data } = await this.httpService.axiosRef.post(`${this.baseUrl}/${endpoint}`, payload, { headers: { 'Content-Type': 'application/json', Authorization: `${this.apiKey}`, Host: 'apigw-uat.neoleap.com.sa', }, - httpsAgent: new Agent({ - rejectUnauthorized: false, // Disable SSL verification for development purposes - ca: this.useLocalCert ? readFileSync(path.join(__dirname, '../zod-certs/My_CA_Bundle.ca-bundle')) : undefined, - cert: this.useLocalCert - ? readFileSync(path.join(__dirname, '../zod-certs/gw-dev_zodwallet_com.crt')) - : undefined, - key: this.useLocalCert ? readFileSync(path.join(__dirname, '../zod-certs/server.key')) : undefined, - }), }); - if (response.data?.ResponseHeader.ResponseCode !== '000' || !response.data[responseKey]) { + if (data.data?.ResponseHeader?.ResponseCode !== '000' || !data.data[responseKey]) { throw new BadRequestException( - response.data.ResponseHeader.ResponseDescription || 'Unexpected Error from Service Provider', + data.data.ResponseHeader.ResponseDescription || 'Unexpected Error from Service Provider', ); } - return plainToInstance(responseClass, response.data[responseKey], { + return plainToInstance(responseClass, data.data[responseKey], { excludeExtraneousValues: true, }); } catch (error: any) {