Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenkumarct committed Feb 8, 2024
1 parent 059b0c8 commit bc950d7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ export class MockPaymentConnector implements PaymentConnector {
};
}

async modifyPaymentByPspReference(pspReference: string, payment: Payment): Promise<MockPaymentProviderModificationResponse> {
async capturePayment(pspReference: string, payment: Payment): Promise<MockPaymentProviderModificationResponse> {

return { outcome: PaymentModificationStatus.APPROVED, pspReference: pspReference }
}

async cancelPayment(pspReference: string, payment: Payment): Promise<MockPaymentProviderModificationResponse> {

return { outcome: PaymentModificationStatus.APPROVED, pspReference: pspReference }
}

async refundPayment(pspReference: string, payment: Payment): Promise<MockPaymentProviderModificationResponse> {

return { outcome: PaymentModificationStatus.APPROVED, pspReference: pspReference }
}
Expand Down
4 changes: 3 additions & 1 deletion processor/src/clients/PaymentConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ import { Payment } from '@commercetools/platform-sdk';
export interface PaymentConnector {

processPayment: (request: CreatePaymentRequest) => Promise<MockPaymentProviderResponse>
modifyPaymentByPspReference: (pspReference: string, payment: Payment) => Promise<MockPaymentProviderModificationResponse>
capturePayment: (pspReference: string, payment: Payment) => Promise<MockPaymentProviderModificationResponse>
cancelPayment: (pspReference: string, payment: Payment) => Promise<MockPaymentProviderModificationResponse>
refundPayment: (pspReference: string, payment: Payment) => Promise<MockPaymentProviderModificationResponse>
}
1 change: 0 additions & 1 deletion processor/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { paymentRoutes } from './routes/payment.route';
import { statusRoutes } from './routes/status.route';
import { DefaultPaymentService } from './services/payment.service';
import {paymentModificationRoutes} from "./routes/payment-modification.route";
import {MockPaymentConnector} from "./clients/mockPaymentConnector";

/**
* Setup Fastify server instance
Expand Down
32 changes: 27 additions & 5 deletions processor/src/services/payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
} from '../dtos/payment.dto';
import { getCartIdFromContext } from '../libs/fastify/context/context';
import {PaymentConnector} from "../clients/PaymentConnector";
import {MockPaymentConnector} from "../clients/mockPaymentConnector";
import {MockPaymentConnector} from "../clients/MockPaymentConnector";
import {Payment} from "@commercetools/platform-sdk";

export class DefaultPaymentService implements PaymentService {
private ctCartService: CommercetoolsCartService;
Expand Down Expand Up @@ -97,10 +98,7 @@ export class DefaultPaymentService implements PaymentService {
},
});

const res = await this.paymentConnector.modifyPaymentByPspReference(
ctPayment.interfaceId as string,
ctPayment,
);
const res = await this.processPaymentModification(transactionType, ctPayment);

await this.ctPaymentService.updatePayment({
id: ctPayment.id,
Expand Down Expand Up @@ -141,4 +139,28 @@ export class DefaultPaymentService implements PaymentService {
}
}
}

private async processPaymentModification(transactionType: string, ctPayment: Payment) {
switch (transactionType) {
case 'CancelAuthorization': {
return await this.paymentConnector.cancelPayment(
ctPayment.interfaceId as string,
ctPayment,
);
}
case 'Charge': {
return await this.paymentConnector.capturePayment(
ctPayment.interfaceId as string,
ctPayment,
);
}
case 'Refund': {
return await this.paymentConnector.refundPayment(
ctPayment.interfaceId as string,
ctPayment,
);
}
}
}

}
1 change: 0 additions & 1 deletion processor/src/services/types/payment.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
PaymentRequestSchemaDTO,
PaymentResponseSchemaDTO,
} from '../../dtos/payment.dto';
import {MockPaymentConnector} from "../../clients/mockPaymentConnector";

export type CreatePayment = {
data: PaymentRequestSchemaDTO;
Expand Down

0 comments on commit bc950d7

Please sign in to comment.