diff --git a/processor/src/clients/mockPaymentConnector.ts b/processor/src/clients/MockPaymentConnector.ts similarity index 69% rename from processor/src/clients/mockPaymentConnector.ts rename to processor/src/clients/MockPaymentConnector.ts index 6ab38f7..14fd8fc 100644 --- a/processor/src/clients/mockPaymentConnector.ts +++ b/processor/src/clients/MockPaymentConnector.ts @@ -26,7 +26,17 @@ export class MockPaymentConnector implements PaymentConnector { }; } - async modifyPaymentByPspReference(pspReference: string, payment: Payment): Promise { + async capturePayment(pspReference: string, payment: Payment): Promise { + + return { outcome: PaymentModificationStatus.APPROVED, pspReference: pspReference } + } + + async cancelPayment(pspReference: string, payment: Payment): Promise { + + return { outcome: PaymentModificationStatus.APPROVED, pspReference: pspReference } + } + + async refundPayment(pspReference: string, payment: Payment): Promise { return { outcome: PaymentModificationStatus.APPROVED, pspReference: pspReference } } diff --git a/processor/src/clients/PaymentConnector.ts b/processor/src/clients/PaymentConnector.ts index cea3e35..572a357 100644 --- a/processor/src/clients/PaymentConnector.ts +++ b/processor/src/clients/PaymentConnector.ts @@ -8,5 +8,7 @@ import { Payment } from '@commercetools/platform-sdk'; export interface PaymentConnector { processPayment: (request: CreatePaymentRequest) => Promise - modifyPaymentByPspReference: (pspReference: string, payment: Payment) => Promise + capturePayment: (pspReference: string, payment: Payment) => Promise + cancelPayment: (pspReference: string, payment: Payment) => Promise + refundPayment: (pspReference: string, payment: Payment) => Promise } diff --git a/processor/src/server.ts b/processor/src/server.ts index 97846c9..725559e 100644 --- a/processor/src/server.ts +++ b/processor/src/server.ts @@ -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 diff --git a/processor/src/services/payment.service.ts b/processor/src/services/payment.service.ts index b4fa0f1..3bee973 100644 --- a/processor/src/services/payment.service.ts +++ b/processor/src/services/payment.service.ts @@ -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; @@ -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, @@ -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, + ); + } + } + } + } diff --git a/processor/src/services/types/payment.type.ts b/processor/src/services/types/payment.type.ts index 684b052..d94d1f7 100644 --- a/processor/src/services/types/payment.type.ts +++ b/processor/src/services/types/payment.type.ts @@ -8,7 +8,6 @@ import { PaymentRequestSchemaDTO, PaymentResponseSchemaDTO, } from '../../dtos/payment.dto'; -import {MockPaymentConnector} from "../../clients/mockPaymentConnector"; export type CreatePayment = { data: PaymentRequestSchemaDTO;