Skip to content

Commit 25ddb9e

Browse files
Merge pull request #57 from mollie/feature/MOL-414
MOL-414: update cancel webhook
2 parents 31c3299 + 96c2b4f commit 25ddb9e

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

processor/src/service/payment.service.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ export const handlePaymentWebhook = async (paymentId: string): Promise<boolean>
138138

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

429430
const newTransactionCustomFieldValue = {
430-
reasonText: triggerTransaction.custom?.fields?.reasonText,
431+
reasonText: triggerTransaction?.custom?.fields?.reasonText,
431432
statusText: CancelStatusText,
432433
};
433434

434-
return [
435-
// Update transaction state to failure
436-
// For cancelling payment, it will be the pendingChargeTransaction
437-
// For cancelling refund, it will be the pendingRefundTransaction
438-
changeTransactionState(targetTransaction.id, CTTransactionState.Failure),
439-
// Update transaction state to success
440-
// For both cancelling payment and cancelling refund, it will be the InitialCancelAuthorization
441-
changeTransactionState(triggerTransaction.id, CTTransactionState.Success),
442-
// Set transaction custom field value
443-
setTransactionCustomType(targetTransaction.id, transactionCustomFieldName, newTransactionCustomFieldValue),
444-
];
435+
// Update transaction state to failure
436+
// For cancelling payment, it will be the pendingChargeTransaction
437+
// For cancelling refund, it will be the pendingRefundTransaction
438+
const actions: UpdateAction[] = [];
439+
440+
if (targetTransaction?.id) {
441+
actions.push(changeTransactionState(targetTransaction?.id, CTTransactionState.Failure));
442+
}
443+
444+
// Update transaction state to success
445+
// For both cancelling payment and cancelling refund, it will be the InitialCancelAuthorization
446+
if (triggerTransaction?.id) {
447+
actions.push(changeTransactionState(triggerTransaction?.id, CTTransactionState.Success));
448+
}
449+
450+
// Set transaction custom field value
451+
if (transactionCustomFieldName) {
452+
actions.push(
453+
setTransactionCustomType(targetTransaction?.id, transactionCustomFieldName, newTransactionCustomFieldValue),
454+
);
455+
}
456+
457+
return actions;
445458
};
446459

447460
/**

processor/tests/service/payment.service.spec.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,13 +1181,7 @@ describe('Test handlePaymentWebhook', () => {
11811181
});
11821182
const ctPayment = {
11831183
id: 'payment-id',
1184-
transactions: [
1185-
{
1186-
id: '12345',
1187-
type: 'CancelAuthorization',
1188-
state: 'Initial',
1189-
},
1190-
],
1184+
transactions: [],
11911185
};
11921186
(getPaymentByMolliePaymentId as jest.Mock).mockReturnValue(ctPayment);
11931187

@@ -1224,13 +1218,7 @@ describe('Test handlePaymentWebhook', () => {
12241218
});
12251219
const ctPayment = {
12261220
id: 'payment-id',
1227-
transactions: [
1228-
{
1229-
id: '12345',
1230-
type: 'Charge',
1231-
state: 'Pending',
1232-
},
1233-
],
1221+
transactions: [],
12341222
};
12351223
(getPaymentByMolliePaymentId as jest.Mock).mockReturnValue(ctPayment);
12361224
const result = await handlePaymentWebhook(fakePaymentId);

0 commit comments

Comments
 (0)