Skip to content

Commit

Permalink
Fix based on reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
NghiaDTr committed Dec 11, 2024
1 parent 791b4fe commit df4208b
Show file tree
Hide file tree
Showing 4 changed files with 672 additions and 2 deletions.
78 changes: 78 additions & 0 deletions processor/src/service/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import {
convertCentToEUR,
parseStringToJsonObject,
roundSurchargeAmountToCent,
sortTransactionsByLatestCreationTime,
} from '../utils/app.utils';
import ApplePaySession from '@mollie/api-client/dist/types/src/data/applePaySession/ApplePaySession';
import { getMethodConfigObjects, getSingleMethodConfigObject } from '../commercetools/customObjects.commercetools';
Expand Down Expand Up @@ -538,6 +539,37 @@ export const handleCreateRefund = async (ctPayment: Payment): Promise<Controller
(transaction) => transaction.type === CTTransactionType.Refund && transaction.state === CTTransactionState.Initial,
);

if (initialRefundTransaction?.custom?.fields[CustomFields.transactionRefundForMolliePayment]) {
logger.debug('SCTM - handleCreateRefund - creating a refund with specific payment id');

successChargeTransaction = ctPayment.transactions.find(
(transaction) =>
transaction.type === CTTransactionType.Charge &&
transaction.state === CTTransactionState.Success &&
transaction.interactionId ===
initialRefundTransaction?.custom?.fields[CustomFields.transactionRefundForMolliePayment],
);
} else {
logger.debug('SCTM - handleCreateRefund - creating a refund for the latest success charge transaction');

const latestTransactions = sortTransactionsByLatestCreationTime(ctPayment.transactions);

successChargeTransaction = latestTransactions.find(
(transaction) =>
transaction.type === CTTransactionType.Charge && transaction.state === CTTransactionState.Success,
);

updateActions.push(
setTransactionCustomType(initialRefundTransaction?.id as string, CustomFields.transactionRefundForMolliePayment, {
[CustomFields.transactionRefundForMolliePayment]: successChargeTransaction?.interactionId,
}),
);
}

if (!successChargeTransaction) {
throw new CustomError(400, 'SCTM - handleCreateRefund - Cannot find valid success charge transaction');
}

const paymentCreateRefundParams: CreateParameters = {
paymentId: successChargeTransaction?.interactionId as string,
amount: makeMollieAmount(initialRefundTransaction?.amount as CentPrecisionMoney),
Expand Down Expand Up @@ -575,6 +607,52 @@ export const handlePaymentCancelRefund = async (ctPayment: Payment): Promise<Con
transaction.type === CTTransactionType.CancelAuthorization && transaction.state === CTTransactionState.Initial,
);

if (initialCancelAuthorization?.interactionId) {
pendingRefundTransaction = ctPayment.transactions.find(
(transaction) =>
transaction.type === CTTransactionType.Refund &&
transaction.state === CTTransactionState.Pending &&
transaction?.interactionId === initialCancelAuthorization.interactionId,
) as Transaction;

if (pendingRefundTransaction) {
successChargeTransaction = ctPayment.transactions.find(
(transaction) =>
transaction.type === CTTransactionType.Charge &&
transaction.state === CTTransactionState.Success &&
transaction.interactionId ===
pendingRefundTransaction?.custom?.fields[CustomFields.transactionRefundForMolliePayment],
) as Transaction;
}

if (!successChargeTransaction) {
throw new CustomError(
400,
'SCTM - handlePaymentCancelRefund - Cannot find the valid Success Charge transaction.',
);
}
}

/**
* @deprecated v1.2 - Will be remove in the next version
*/
if (!pendingRefundTransaction || !successChargeTransaction) {
const latestTransactions = sortTransactionsByLatestCreationTime(ctPayment.transactions);

pendingRefundTransaction = latestTransactions.find(
(transaction) =>
transaction.type === CTTransactionType.Refund && transaction.state === CTTransactionState.Pending,
);

successChargeTransaction = latestTransactions.find(
(transaction) =>
transaction.type === CTTransactionType.Charge && transaction.state === CTTransactionState.Success,
);
}
/**
* end deprecated
*/

const paymentGetRefundParams: CancelParameters = {
paymentId: successChargeTransaction?.interactionId as string,
};
Expand Down
21 changes: 20 additions & 1 deletion processor/src/utils/app.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SurchargeCost } from './../types/commercetools.types';
import { Payment } from '@commercetools/platform-sdk';
import { Payment, Transaction } from '@commercetools/platform-sdk';
import CustomError from '../errors/custom.error';
import { logger } from './logger.utils';
/**
Expand Down Expand Up @@ -101,3 +101,22 @@ export const calculateTotalSurchargeAmount = (ctPayment: Payment, surcharges?: S
export const roundSurchargeAmountToCent = (surchargeAmountInEur: number, fractionDigits: number): number => {
return Math.round(surchargeAmountInEur * Math.pow(10, fractionDigits));
};

export const sortTransactionsByLatestCreationTime = (transactions: Transaction[]): Transaction[] => {
const clonedTransactions = Object.assign([], transactions);

return clonedTransactions.sort((a: Transaction, b: Transaction) => {
const timeA = a.timestamp as string;
const timeB = b.timestamp as string;

if (timeA < timeB) {
return 1;
}

if (timeA > timeB) {
return -1;
}

return 0;
});
};
Loading

0 comments on commit df4208b

Please sign in to comment.