Skip to content

Commit

Permalink
Merge pull request #57 from mollie/feature/MOL-414
Browse files Browse the repository at this point in the history
MOL-414: update cancel webhook
  • Loading branch information
Tung-Huynh-Shopmacher authored Sep 11, 2024
2 parents 31c3299 + 96c2b4f commit 25ddb9e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
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

0 comments on commit 25ddb9e

Please sign in to comment.