diff --git a/processor/src/service/payment.service.ts b/processor/src/service/payment.service.ts index 8dba22d..ec605b8 100644 --- a/processor/src/service/payment.service.ts +++ b/processor/src/service/payment.service.ts @@ -138,7 +138,8 @@ export const handlePaymentWebhook = async (paymentId: string): Promise 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}`, @@ -424,24 +425,36 @@ export const handlePaymentCancelRefund = async (ctPayment: Payment): Promise { - 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; }; /** diff --git a/processor/tests/service/payment.service.spec.ts b/processor/tests/service/payment.service.spec.ts index 980ceb9..371c94b 100644 --- a/processor/tests/service/payment.service.spec.ts +++ b/processor/tests/service/payment.service.spec.ts @@ -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); @@ -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);