Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve conflicts #115

Closed
wants to merge 2 commits into from
Closed
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
108 changes: 89 additions & 19 deletions processor/src/service/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
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 @@ -422,10 +423,6 @@

const paymentParams = createMollieCreatePaymentParams(ctPayment, extensionUrl, surchargeAmountInCent, cart);

if (method && method.toString() === 'googlepay') {
paymentParams.method = PaymentMethod.creditcard;
}

let molliePayment;
if (PaymentMethod[paymentParams.method as PaymentMethod]) {
logger.debug('SCTM - handleCreatePayment - Attempt creating a payment with method defined in Mollie NodeJS Client');
Expand Down Expand Up @@ -530,27 +527,59 @@
};

export const handleCreateRefund = async (ctPayment: Payment): Promise<ControllerResponseType> => {
const successChargeTransaction = ctPayment.transactions.find(
(transaction) => transaction.type === CTTransactionType.Charge && transaction.state === CTTransactionState.Success,
);
let successChargeTransaction;
const updateActions = [] as UpdateAction[];

const initialRefundTransaction = ctPayment.transactions.find(
(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),
};

const refund = await createPaymentRefund(paymentCreateRefundParams);

updateActions.push(
changeTransactionInteractionId(initialRefundTransaction?.id as string, refund.id),
changeTransactionState(initialRefundTransaction?.id as string, CTTransactionState.Pending),
);

return {
statusCode: 201,
actions: [
changeTransactionInteractionId(initialRefundTransaction?.id as string, refund.id),
changeTransactionState(initialRefundTransaction?.id as string, CTTransactionState.Pending),
],
actions: updateActions,
};
};

Expand All @@ -562,19 +591,60 @@
* @throws {CustomError} If there is an error in the process.
*/
export const handlePaymentCancelRefund = async (ctPayment: Payment): Promise<ControllerResponseType> => {
const successChargeTransaction = ctPayment.transactions.find(
(transaction) => transaction.type === CTTransactionType.Charge && transaction.state === CTTransactionState.Success,
);

const pendingRefundTransaction = ctPayment.transactions.find(
(transaction) => transaction.type === CTTransactionType.Refund && transaction.state === CTTransactionState.Pending,
);
let pendingRefundTransaction: any;
let successChargeTransaction: any;

const initialCancelAuthorization = ctPayment.transactions.find(
(transaction) =>
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 Expand Up @@ -763,4 +833,4 @@
statusCode: 200,
actions: ctActions,
};
};
};

Check failure on line 836 in processor/src/service/payment.service.ts

View workflow job for this annotation

GitHub Actions / build-processor

Insert `⏎`

Check failure on line 836 in processor/src/service/payment.service.ts

View workflow job for this annotation

GitHub Actions / build-processor

Insert `⏎`
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;
});
};
3 changes: 2 additions & 1 deletion processor/src/utils/constant.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
},
transactionSurchargeCost: 'sctm_transaction_surcharge_cost',
transactionRefundForMolliePayment: 'sctm_transaction_refund_for_mollie_payment',
};

export enum ConnectorActions {
Expand Down Expand Up @@ -69,4 +70,4 @@

export const MOLLIE_SURCHARGE_LINE_DESCRIPTION = 'Total surcharge amount';

export const MOLLIE_SHIPPING_LINE_DESCRIPTION = 'Shipping amount';
export const MOLLIE_SHIPPING_LINE_DESCRIPTION = 'Shipping amount';

Check failure on line 73 in processor/src/utils/constant.utils.ts

View workflow job for this annotation

GitHub Actions / build-processor

Insert `⏎`

Check failure on line 73 in processor/src/utils/constant.utils.ts

View workflow job for this annotation

GitHub Actions / build-processor

Insert `⏎`
Loading
Loading