Skip to content

Commit

Permalink
Create prototype for supported payment components
Browse files Browse the repository at this point in the history
  • Loading branch information
leungkinghin-ct committed Feb 7, 2024
1 parent 9fbddcb commit 15c1f8a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
7 changes: 7 additions & 0 deletions processor/src/dtos/payment-methods.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Static, Type } from '@sinclair/typebox';

export const SupportedPaymentMethodData = Type.String();

export const SupportedPaymentMethodsSchema = Type.Array(SupportedPaymentMethodData);

export type SupportedPaymentMethodsDTO = Static<typeof SupportedPaymentMethodsSchema>;
17 changes: 17 additions & 0 deletions processor/src/routes/payment-components.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { FastifyInstance, FastifyPluginOptions } from 'fastify';

import { PaymentService } from '../services/types/payment.type';

type PaymentComponentsRoutesOptions = {
paymentService: PaymentService;
};

export const paymentComponentsRoute = async (
fastify: FastifyInstance,
options: FastifyPluginOptions & PaymentComponentsRoutesOptions,
) => {
fastify.get('/payment-components', async (request, reply) => {
const result = await options.paymentService.getSupponpmrtedPaymentComponents();
reply.code(200).send(result);
});
};
2 changes: 1 addition & 1 deletion processor/src/routes/payment.route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FastifyInstance, FastifyPluginOptions, onRequestHookHandler } from 'fastify';
import { FastifyInstance, FastifyPluginOptions } from 'fastify';
import { PaymentService } from '../services/types/payment.type';
import {
PaymentRequestSchema,
Expand Down
7 changes: 6 additions & 1 deletion processor/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import cors from '@fastify/cors';
import fastifyFormBody from '@fastify/formbody';
import Fastify, { FastifyRequest } from 'fastify';
import Fastify from 'fastify';
import { randomUUID } from 'node:crypto';
import { config } from './config/config';
import { requestContextPlugin } from './libs/fastify/context/context';
Expand All @@ -9,6 +9,7 @@ import { paymentSDK } from './payment-sdk';
import { configRoutes } from './routes/config.route';
import { paymentRoutes } from './routes/payment.route';
import { statusRoutes } from './routes/status.route';
import { paymentComponentsRoute } from './routes/payment-components.route';
import { DefaultPaymentService } from './services/payment.service';

/**
Expand Down Expand Up @@ -55,5 +56,9 @@ export const setupFastify = async () => {
sessionAuthHook: paymentSDK.sessionAuthHookFn,
});

await server.register(paymentComponentsRoute, {
paymentService,
});

return server;
};
9 changes: 8 additions & 1 deletion processor/src/services/payment.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommercetoolsCartService, CommercetoolsPaymentService } from '@commercetools/connect-payments-sdk';
import { paymentProviderApi } from '../clients/mockPaymentAPI';
import { CreatePayment, PaymentService, PaymentServiceOptions } from './types/payment.type';
import { CreatePayment, PaymentService, PaymentServiceOptions, SupportedPaymentComponents } from './types/payment.type';
import { PaymentOutcome, PaymentResponseSchemaDTO } from '../dtos/payment.dto';
import { getCartIdFromContext } from '../libs/fastify/context/context';

Expand All @@ -13,6 +13,13 @@ export class DefaultPaymentService implements PaymentService {
this.ctPaymentService = opts.ctPaymentService;
}

public async getSupportedPaymentComponents(): Promise<SupportedPaymentComponents> {
// TODO : Implement actual API call to payment service provider
return {
supportedPaymentComponents: ['dropin', 'card', 'applepay'],
};
}

public async createPayment(opts: CreatePayment): Promise<PaymentResponseSchemaDTO> {
let ctCart;
ctCart = await this.ctCartService.getCart({
Expand Down
6 changes: 6 additions & 0 deletions processor/src/services/types/payment.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Cart, Payment } from '@commercetools/platform-sdk';
import { CommercetoolsCartService, CommercetoolsPaymentService } from '@commercetools/connect-payments-sdk';
import { PaymentOutcome, PaymentRequestSchemaDTO, PaymentResponseSchemaDTO } from '../../dtos/payment.dto';
import { SupportedPaymentMethodsDTO } from '../../dtos/payment-methods.dto';

export type CreatePayment = {
data: PaymentRequestSchemaDTO;
Expand All @@ -18,8 +19,13 @@ export type MockPaymentProviderResponse = {
paymentMethodType: string;
};

export type SupportedPaymentComponents = {
supportedPaymentComponents: SupportedPaymentMethodsDTO;
};

export interface PaymentService {
createPayment(opts: CreatePayment): Promise<PaymentResponseSchemaDTO>;
getSupportedPaymentComponents(): Promise<SupportedPaymentComponents>;
}

export type PaymentServiceOptions = {
Expand Down

0 comments on commit 15c1f8a

Please sign in to comment.