Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions vehicles/src/common/helper/permit-fee.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { differenceBetween } from './date-time.helper';
import * as dayjs from 'dayjs';
import { Nullable } from '../types/common';
import { PaymentMethodType } from '../enum/payment-method-type.enum';

/**
* Calculates the permit fee based on the application and old amount.
Expand Down Expand Up @@ -155,6 +156,25 @@ export const currentPermitFee = (
: pricePerTerm * permitTerms + oldAmount;
};

/**
* Determines whether or not transaction is valid based on payment method and if it's approved.
* @param paymentMethod Payment method used
* @param transactionApproved Approval status of the transaction
* @returns Whether or not the transaction is valid
*/
export const isValidTransaction = (
paymentMethod: PaymentMethodType,
transactionApproved?: Nullable<number>,
) => {
// For payment methods using payment gateways (ie. PayBC), a payment is considered to have succeeded only if its
// pgApproved flag is 1, and transactions using all other payment methods (ie. IcePay) is automatically considered
// to have succeeded
return (
paymentMethod !== PaymentMethodType.WEB ||
(Boolean(transactionApproved) && transactionApproved > 0)
);
};

export const calculatePermitAmount = (
permitPaymentHistory: PermitHistoryDto[],
): number => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { PaymentMethodType } from './entities/payment-method-type.entity';
import { LogAsyncMethodExecution } from '../../../common/decorator/log-async-method-execution.decorator';
import {
calculatePermitAmount,
isValidTransaction,
permitFee,
validAmount,
} from 'src/common/helper/permit-fee.helper';
Expand Down Expand Up @@ -856,11 +857,20 @@ export class PaymentService {
queryRunner,
companyId,
);

const validPermitPaymentHistory = permitPaymentHistory.filter(
historyItem => isValidTransaction(
historyItem.paymentMethodTypeCode,
historyItem.pgApproved,
),
);

const isNoFee = await this.specialAuthService.findNoFee(companyId);
const oldAmount =
permitPaymentHistory.length > 0
? calculatePermitAmount(permitPaymentHistory)
validPermitPaymentHistory.length > 0
? calculatePermitAmount(validPermitPaymentHistory)
: undefined;

if (application.permitStatus === ApplicationStatus.VOIDED)
return -oldAmount;
const fee = permitFee(application, isNoFee, oldAmount);
Expand Down