Skip to content

Commit

Permalink
Merge pull request #93 from commercetools/dasanorct/manage_unsupporte…
Browse files Browse the repository at this point in the history
…d_notifications

feat(notifications): managed non supported notifications by discard them
  • Loading branch information
dasanorct authored Apr 11, 2024
2 parents 6ee5a61 + 9599920 commit 5deee41
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 58 deletions.
4 changes: 2 additions & 2 deletions enabler/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion enabler/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "enabler",
"private": true,
"version": "3.0.2",
"version": "3.0.3",
"type": "module",
"scripts": {
"dev": "vite --port 3000",
Expand Down
98 changes: 49 additions & 49 deletions processor/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions processor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "payment-integration-adyen",
"version": "3.0.2",
"version": "3.0.3",
"description": "Payment integration with Adyen",
"main": "dist/server.js",
"scripts": {
Expand All @@ -21,7 +21,7 @@
"dependencies": {
"@adyen/api-library": "16.2.0",
"@commercetools-backend/loggers": "22.23.2",
"@commercetools/connect-payments-sdk": "0.4.2",
"@commercetools/connect-payments-sdk": "0.4.3",
"@fastify/autoload": "5.8.0",
"@fastify/cors": "9.0.1",
"@fastify/formbody": "7.4.0",
Expand Down
16 changes: 16 additions & 0 deletions processor/src/errors/adyen-api.error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,19 @@ export class AdyenApiError extends Errorx {
});
}
}

export type UnsupportedNotificationErrorData = {
notificationEvent: string;
};

export class UnsupportedNotificationError extends Errorx {
constructor(errorData: UnsupportedNotificationErrorData, additionalOpts?: ErrorxAdditionalOpts) {
super({
code: 'UnsupportedNotification',
httpErrorStatus: 400,
message: `Unsupported notification event: ${errorData.notificationEvent}`,
skipLog: true,
...additionalOpts,
});
}
}
16 changes: 15 additions & 1 deletion processor/src/services/adyen-payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
statusHandler,
Cart,
Payment,
UpdatePayment,
} from '@commercetools/connect-payments-sdk';
import {
ConfirmPaymentRequestDTO,
Expand Down Expand Up @@ -49,6 +50,7 @@ import { PaymentDetailsResponse } from '@adyen/api-library/lib/src/typings/check
import { CancelPaymentConverter } from './converters/cancel-payment.converter';
import { RefundPaymentConverter } from './converters/refund-payment.converter';
import { log } from '../libs/logger';
import { UnsupportedNotificationError } from '../errors/adyen-api.error';
const packageJSON = require('../../package.json');

export type AdyenPaymentServiceOptions = {
Expand Down Expand Up @@ -336,7 +338,19 @@ export class AdyenPaymentService extends AbstractPaymentService {

public async processNotification(opts: { data: NotificationRequestDTO }): Promise<void> {
log.info('Processing notification', { notification: JSON.stringify(opts.data) });
const updateData = await this.notificationConverter.convert(opts);
let updateData!: UpdatePayment;

try {
updateData = await this.notificationConverter.convert(opts);
} catch (e) {
if (e instanceof UnsupportedNotificationError) {
log.info('Unsupported notification received', { notification: JSON.stringify(opts.data) });
return;
}
log.error('Error processing notification', { error: e });
throw e;
}

const updatedPayment = await this.ctPaymentService.updatePayment(updateData);
log.info('Payment updated after processing the notification', {
paymentId: updatedPayment.id,
Expand Down
6 changes: 3 additions & 3 deletions processor/src/services/converters/notification.converter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NotificationRequestItem } from '@adyen/api-library/lib/src/typings/notification/notificationRequestItem';
import { NotificationRequestDTO } from '../../dtos/adyen-payment.dto';
import { TransactionData, UpdatePayment, Money } from '@commercetools/connect-payments-sdk';
import { UnsupportedNotificationError } from '../../errors/adyen-api.error';

export class NotificationConverter {
constructor() {}
Expand All @@ -10,7 +11,7 @@ export class NotificationConverter {

return {
id: item.merchantReference,
pspReference: item.pspReference,
pspReference: item.originalReference || item.pspReference,
transaction: this.populateTransaction(item),
};
}
Expand Down Expand Up @@ -67,8 +68,7 @@ export class NotificationConverter {
interactionId: item.pspReference,
};
default:
//TODO: throw unsupported notification error
throw new Error('Unsupported notification');
throw new UnsupportedNotificationError({ notificationEvent: item.eventCode.toString() });
}
}

Expand Down

0 comments on commit 5deee41

Please sign in to comment.