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

MOL-414: update cancel webhook #57

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
41 changes: 27 additions & 14 deletions processor/src/service/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ export const handlePaymentWebhook = async (paymentId: string): Promise<boolean>

if (
molliePayment.status === PaymentStatus.canceled &&
(!pendingChargeTransaction || !initialCancelAuthorizationTransaction)
!pendingChargeTransaction &&
!initialCancelAuthorizationTransaction
) {
logger.warn(
`SCTM - handlePaymentWebhook - Pending Charge transaction or Initial CancelAuthorization transaction is not found, CommerceTools Payment ID: ${ctPayment.id}`,
Expand Down Expand Up @@ -424,24 +425,36 @@ export const handlePaymentCancelRefund = async (ctPayment: Payment): Promise<Con
* @param triggerTransaction
*/
export const getPaymentCancelActions = (targetTransaction: Transaction, triggerTransaction: Transaction) => {
const transactionCustomFieldName = CustomFields.paymentCancelReason;
const transactionCustomFieldName = CustomFields?.paymentCancelReason;

const newTransactionCustomFieldValue = {
reasonText: triggerTransaction.custom?.fields?.reasonText,
reasonText: triggerTransaction?.custom?.fields?.reasonText,
statusText: CancelStatusText,
};

return [
// Update transaction state to failure
// For cancelling payment, it will be the pendingChargeTransaction
// For cancelling refund, it will be the pendingRefundTransaction
changeTransactionState(targetTransaction.id, CTTransactionState.Failure),
// Update transaction state to success
// For both cancelling payment and cancelling refund, it will be the InitialCancelAuthorization
changeTransactionState(triggerTransaction.id, CTTransactionState.Success),
// Set transaction custom field value
setTransactionCustomType(targetTransaction.id, transactionCustomFieldName, newTransactionCustomFieldValue),
];
// Update transaction state to failure
// For cancelling payment, it will be the pendingChargeTransaction
// For cancelling refund, it will be the pendingRefundTransaction
const actions: UpdateAction[] = [];

if (targetTransaction?.id) {
actions.push(changeTransactionState(targetTransaction?.id, CTTransactionState.Failure));
}

// Update transaction state to success
// For both cancelling payment and cancelling refund, it will be the InitialCancelAuthorization
if (triggerTransaction?.id) {
actions.push(changeTransactionState(triggerTransaction?.id, CTTransactionState.Success));
}

// Set transaction custom field value
if (transactionCustomFieldName) {
actions.push(
setTransactionCustomType(targetTransaction?.id, transactionCustomFieldName, newTransactionCustomFieldValue),
);
}

return actions;
};

/**
Expand Down
16 changes: 2 additions & 14 deletions processor/tests/service/payment.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1181,13 +1181,7 @@ describe('Test handlePaymentWebhook', () => {
});
const ctPayment = {
id: 'payment-id',
transactions: [
{
id: '12345',
type: 'CancelAuthorization',
state: 'Initial',
},
],
transactions: [],
};
(getPaymentByMolliePaymentId as jest.Mock).mockReturnValue(ctPayment);

Expand Down Expand Up @@ -1224,13 +1218,7 @@ describe('Test handlePaymentWebhook', () => {
});
const ctPayment = {
id: 'payment-id',
transactions: [
{
id: '12345',
type: 'Charge',
state: 'Pending',
},
],
transactions: [],
};
(getPaymentByMolliePaymentId as jest.Mock).mockReturnValue(ctPayment);
const result = await handlePaymentWebhook(fakePaymentId);
Expand Down