Skip to content

Commit

Permalink
Update modification route and related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenkumarct committed Feb 8, 2024
1 parent 9a48203 commit 059b0c8
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 218 deletions.
12 changes: 12 additions & 0 deletions processor/src/clients/PaymentConnector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {
CreatePaymentRequest,
MockPaymentProviderModificationResponse,
MockPaymentProviderResponse
} from '../services/types/payment.type';
import { Payment } from '@commercetools/platform-sdk';

export interface PaymentConnector {

processPayment: (request: CreatePaymentRequest) => Promise<MockPaymentProviderResponse>
modifyPaymentByPspReference: (pspReference: string, payment: Payment) => Promise<MockPaymentProviderModificationResponse>
}
53 changes: 0 additions & 53 deletions processor/src/clients/mockPaymentAPI.ts

This file was deleted.

37 changes: 37 additions & 0 deletions processor/src/clients/mockPaymentConnector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { v4 as uuid } from 'uuid';
import {
CreatePaymentRequest, MockPaymentProviderModificationResponse,
MockPaymentProviderResponse
} from '../services/types/payment.type';
import { PaymentModificationStatus, PaymentOutcome } from '../dtos/payment.dto';
import { Payment } from '@commercetools/platform-sdk';
import {PaymentConnector} from "./PaymentConnector";

export class MockPaymentConnector implements PaymentConnector {

private allowedCreditCards = ['4111111111111111', '5555555555554444', '341925950237632'];

/**
* @param request
* @returns
*/
async processPayment(request: CreatePaymentRequest): Promise<MockPaymentProviderResponse> {
const paymentMethod = request.data.paymentMethod;
const isAuthorized = this.isCreditCardAllowed(paymentMethod.cardNumber);

return {
resultCode: isAuthorized ? PaymentOutcome.AUTHORIZED : PaymentOutcome.REJECTED,
pspReference: uuid(),
paymentMethodType: paymentMethod.type,
};
}

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

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

isCreditCardAllowed(cardNumber: string) {
return this.allowedCreditCards.includes(cardNumber);
}
}
31 changes: 13 additions & 18 deletions processor/src/dtos/payment.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export const PaymentRequestSchema = Type.Object({
paymentMethod: Type.Composite([CardPaymentMethod]),
});

export const PaymentModificationRequestSchema = Type.Object({
action: Type.String(),
amount: Type.Object({
centAmount: Type.Number(),
currencyCode: Type.String(),
})
})

export enum PaymentOutcome {
AUTHORIZED = 'Authorized',
REJECTED = 'Rejected',
Expand All @@ -27,31 +35,18 @@ export const PaymentResponseSchema = Type.Object({
paymentReference: Type.String(),
});

export const CapturePaymentRequestSchema = Type.Object({
amount: Type.Object({
centAmount: Type.Number(),
currencyCode: Type.String(),
}),
});

export const RefundPaymentRequestSchema = Type.Object({
amount: Type.Object({
centAmount: Type.Number(),
currencyCode: Type.String(),
}),
});

export enum PaymentModificationStatus {
RECEIVED = 'received',
SUCCESS = 'success',
APPROVED = 'approved',
REJECTED = 'rejected',
}
const PaymentModificationSchema = Type.Enum(PaymentModificationStatus);

export const PaymentModificationResponseSchema = Type.Object({
status: PaymentModificationSchema,
outcome: PaymentModificationSchema,
});

export type PaymentRequestSchemaDTO = Static<typeof PaymentRequestSchema>;
export type PaymentModificationRequestSchemaDTO = Static<typeof PaymentModificationRequestSchema>;
export type PaymentResponseSchemaDTO = Static<typeof PaymentResponseSchema>;
export type CapturePaymentRequestDTO = Static<typeof CapturePaymentRequestSchema>;
export type RefundPaymentRequestDTO = Static<typeof RefundPaymentRequestSchema>;
export type PaymentModificationResponseDTO = Static<typeof PaymentModificationResponseSchema>;
67 changes: 27 additions & 40 deletions processor/src/routes/payment-modification.route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { PaymentService } from '../services/types/payment.type';
import { CapturePaymentRequestDTO, PaymentModificationResponseDTO, RefundPaymentRequestDTO } from '../dtos/payment.dto';
import {
PaymentModificationRequestSchema,
PaymentModificationRequestSchemaDTO,
PaymentModificationResponseDTO,
PaymentModificationResponseSchema,
} from '../dtos/payment.dto';

type PaymentRoutesOptions = {
paymentService: PaymentService;
Expand All @@ -10,44 +15,26 @@ export const paymentModificationRoutes = async (
fastify: FastifyInstance,
opts: FastifyPluginOptions & PaymentRoutesOptions,
) => {
fastify.post<{ Body: any; Reply: PaymentModificationResponseDTO }>(
'/payments/:id/cancels',
{},
async (request, reply) => {
const params = request.params as any;
const resp = await opts.paymentService.cancelPayment({
paymentId: params.id,
});
fastify.post<{ Body: PaymentModificationRequestSchemaDTO; Reply: PaymentModificationResponseDTO }>(
'/payment-intents/{id}',
{
// TODO: use the oauth hook
onRequest: [opts.sessionAuthHook.authenticate()],
schema: {
body: PaymentModificationRequestSchema,
response: {
200: PaymentModificationResponseSchema,
},
},
},
async (request, reply) => {
const params = request.params as any;
const resp = await opts.paymentService.modifyPayment({
paymentId: params.id,
data: request.body,
});

return reply.status(200).send(resp);
},
);

fastify.post<{ Body: CapturePaymentRequestDTO; Reply: PaymentModificationResponseDTO }>(
'/payments/:id/captures',
{},
async (request, reply) => {
const params = request.params as any;
const resp = await opts.paymentService.capturePayment({
paymentId: params.id,
data: request.body,
});

return reply.status(200).send(resp);
},
);

fastify.post<{ Body: RefundPaymentRequestDTO; Reply: PaymentModificationResponseDTO }>(
'/payments/:id/refunds',
{},
async (request, reply) => {
const params = request.params as any;
const resp = await opts.paymentService.refundPayment({
paymentId: params.id,
data: request.body,
});

return reply.status(200).send(resp);
},
);
return reply.status(200).send(resp);
},
);
};
1 change: 1 addition & 0 deletions processor/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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
Loading

0 comments on commit 059b0c8

Please sign in to comment.