Skip to content

Commit

Permalink
ORV2-2481 - PayBC financial transaction data fix (#1459)
Browse files Browse the repository at this point in the history
  • Loading branch information
praju-aot authored Jul 5, 2024
1 parent 7844505 commit 05d5023
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
6 changes: 6 additions & 0 deletions vehicles/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export class AppService {
createCacheMap(permitTypes, 'permitTypeId', 'name'),
);

await addToCache(
this.cacheManager,
CacheKey.PERMIT_TYPE_GL_CODE,
createCacheMap(permitTypes, 'permitTypeId', 'glCode'),
);

const powerUnitTypes = await this.powerUnitTypeService.findAll();
await addToCache(
this.cacheManager,
Expand Down
1 change: 1 addition & 0 deletions vehicles/src/common/enum/cache-key.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum CacheKey {
COUNTRY = 'COUNTRY',
PROVINCE = 'PROVINCE',
PERMIT_TYPE = 'PERMIT_TYPE',
PERMIT_TYPE_GL_CODE = 'PERMIT_TYPE_GL_CODE',
VEHICLE_TYPE = 'VEHICLE_TYPE',
POWER_UNIT_TYPE = 'POWER_UNIT_TYPE',
TRAILER_TYPE = 'TRAILER_TYPE',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
BadRequestException,
Inject,
Injectable,
InternalServerErrorException,
Logger,
Expand Down Expand Up @@ -39,7 +40,6 @@ import { convertToHash } from 'src/common/helper/crypto.helper';
import { UpdatePaymentGatewayTransactionDto } from './dto/request/update-payment-gateway-transaction.dto';
import { PaymentCardType } from './entities/payment-card-type.entity';
import { PaymentMethodType } from './entities/payment-method-type.entity';
import { LogMethodExecution } from '../../../common/decorator/log-method-execution.decorator';
import { LogAsyncMethodExecution } from '../../../common/decorator/log-async-method-execution.decorator';
import { PermitHistoryDto } from '../permit/dto/response/permit-history.dto';
import {
Expand All @@ -51,6 +51,10 @@ import { CfsFileStatus } from 'src/common/enum/cfs-file-status.enum';
import { isAmendmentApplication } from '../../../common/helper/permit-application.helper';
import { isCfsPaymentMethodType } from 'src/common/helper/payment.helper';
import { PgApprovesStatus } from 'src/common/enum/pg-approved-status-type.enum';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { CacheKey } from 'src/common/enum/cache-key.enum';
import { getFromCache } from '../../../common/helper/cache.helper';

@Injectable()
export class PaymentService {
Expand All @@ -68,6 +72,8 @@ export class PaymentService {
@InjectRepository(Permit)
private permitRepository: Repository<Permit>,
@InjectMapper() private readonly classMapper: Mapper,
@Inject(CACHE_MANAGER)
private readonly cacheManager: Cache,
) {}

private generateHashExpiry = (currDate?: Date) => {
Expand All @@ -91,10 +97,33 @@ export class PaymentService {
return `${year}${monthPadded}${dayPadded}${hoursPadded}${minutesPadded}`;
};

private queryHash = (transaction: Transaction) => {
private queryHash = async (transaction: Transaction) => {
const redirectUrl = process.env.PAYBC_REDIRECT;
const date = new Date().toISOString().split('T')[0];

const glCodeDetails = await Promise.all(
transaction.permitTransactions.map(
async ({ permit: { permitType }, transactionAmount }) => ({
glCode: await getFromCache(
this.cacheManager,
CacheKey.PERMIT_TYPE_GL_CODE,
permitType,
),
amount: transactionAmount,
}),
),
);

const groupedGlCodes = glCodeDetails.reduce((acc, { glCode, amount }) => {
acc.set(glCode, (acc.get(glCode) || 0) + amount);
return acc;
}, new Map<string, number>());

// Format the output string as <<index>>:<<gl code>>:<<transaction amount>> where index starts from 1
const revenue = Array.from(groupedGlCodes.entries())
.map(([glCode, amount], index) => `${index + 1}:${glCode}:${amount}`)
.join('|');

// There should be a better way of doing this which is not as rigid - something like
// dynamically removing the hashValue param from the actual query string instead of building
// it up manually below, but this is sufficient for now.
Expand All @@ -108,7 +137,7 @@ export class PaymentService {
`&glDate=${date}` +
`&paymentMethod=${PAYBC_PAYMENT_METHOD}` +
`&currency=${PAYMENT_CURRENCY}` +
`&revenue=1:${process.env.GL_CODE}:${transaction.totalTransactionAmount}` +
`&revenue=${revenue}` +
`&ref2=${transaction.transactionId}`;

// Generate the hash using the query string and the MD5 algorithm
Expand All @@ -122,10 +151,10 @@ export class PaymentService {
return { queryString, payBCHash, hashExpiry };
};

@LogMethodExecution()
generateUrl(transaction: Transaction): string {
@LogAsyncMethodExecution()
async generateUrl(transaction: Transaction): Promise<string> {
// Construct the URL with the transaction details for the payment gateway
const { queryString, payBCHash } = this.queryHash(transaction);
const { queryString, payBCHash } = await this.queryHash(transaction);
const url =
`${process.env.PAYBC_BASE_URL}?` +
`${queryString}` +
Expand Down Expand Up @@ -352,7 +381,7 @@ export class PaymentService {
)
) {
// Only payment using PayBC should generate the url
url = this.generateUrl(createdTransaction);
url = await this.generateUrl(createdTransaction);
}

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ export class PermitType {
@AutoMap()
@Column({ name: 'NAME', nullable: true })
name: string;

@AutoMap()
@Column({ name: 'GL_CODE', nullable: true })
glCode: string;
}

0 comments on commit 05d5023

Please sign in to comment.