From 3a64d5a27866397c0760179c7acdd5f6ed0ed684 Mon Sep 17 00:00:00 2001 From: praveenkumarct Date: Mon, 29 Jan 2024 13:31:37 +0100 Subject: [PATCH] Remove create session and adyen specific libraries --- processor/package.json | 2 - processor/src/dtos/payment.dto.ts | 30 +----- processor/src/services/payment.service.ts | 112 ++++++---------------- 3 files changed, 32 insertions(+), 112 deletions(-) diff --git a/processor/package.json b/processor/package.json index 90a6a9e..c83ab44 100644 --- a/processor/package.json +++ b/processor/package.json @@ -18,7 +18,6 @@ "author": "", "license": "ISC", "dependencies": { - "@adyen/api-library": "14.2.0", "@commercetools/connect-payments-sdk": "0.0.1", "@commercetools/platform-sdk": "6.0.0", "@commercetools/sdk-client-v2": "2.2.0", @@ -36,7 +35,6 @@ "pino-std-serializers": "6.2.2" }, "devDependencies": { - "@adyen/adyen-web": "5.51.0", "@jest/globals": "29.7.0", "@types/jest": "29.5.6", "@types/node": "20.6.3", diff --git a/processor/src/dtos/payment.dto.ts b/processor/src/dtos/payment.dto.ts index 294484a..6e043b7 100644 --- a/processor/src/dtos/payment.dto.ts +++ b/processor/src/dtos/payment.dto.ts @@ -1,26 +1,6 @@ import {Static, Type} from '@sinclair/typebox'; -// TODO: Identify the SessionRequest Type for mock payment - -// export type CreateSessionData = Omit< -// CreateCheckoutSessionRequest, -// | 'amount' -// | 'merchantAccount' -// | 'countryCode' -// | 'returnUrl' -// | 'reference' -// | 'storePaymentMethod' -// | 'shopperReference' -// | 'recurringProcessingModel' -// | 'storePaymentMethodMode' -// >; -// -// export type SessionData = { -// sessionData: CreateCheckoutSessionResponse; -// paymentReference: string; -// }; - -export const cartPaymentMethod = Type.Object({ +export const CardPaymentMethod = Type.Object({ type: Type.Literal('card'), number: Type.String(), expiryMonth: Type.Number(), @@ -30,19 +10,19 @@ export const cartPaymentMethod = Type.Object({ }); export const PaymentRequestSchema = Type.Object({ - paymentMethod: Type.Composite([cartPaymentMethod]), + paymentMethod: Type.Composite([CardPaymentMethod]), paymentReference: Type.String(), }); -export enum paymentOutcome { +export enum PaymentOutcome { AUTHORIZED = 'Authorized', REJECTED = 'Rejected', } -export const paymentOutcomeSchema = Type.Enum(paymentOutcome); +export const PaymentOutcomeSchema = Type.Enum(PaymentOutcome); export const PaymentResponseSchema = Type.Object({ - outcome: paymentOutcomeSchema, + outcome: PaymentOutcomeSchema, paymentReference: Type.String(), }); diff --git a/processor/src/services/payment.service.ts b/processor/src/services/payment.service.ts index f0e379b..9f3e312 100644 --- a/processor/src/services/payment.service.ts +++ b/processor/src/services/payment.service.ts @@ -1,4 +1,4 @@ -import { CommercetoolsCartService, CommercetoolsPaymentService, } from '@commercetools/connect-payments-sdk'; +import { CommercetoolsCartService, CommercetoolsPaymentService } from '@commercetools/connect-payments-sdk'; import { paymentProviderApi } from '../clients/client'; import { CreatePayment, @@ -6,7 +6,7 @@ import { PaymentServiceOptions } from './types/payment.type'; import { getSessionContext } from '../libs/fastify/context/context'; -import {paymentOutcome, PaymentResponseSchemaDTO} from '../dtos/payment.dto'; +import { PaymentOutcome, PaymentResponseSchemaDTO } from '../dtos/payment.dto'; export class DefaultPaymentService implements PaymentService { private ctCartService: CommercetoolsCartService; @@ -17,93 +17,35 @@ export class DefaultPaymentService implements PaymentService { this.ctPaymentService = opts.ctPaymentService; } - // TODO - - // public async createSession(opts: CreateSession): Promise { - // const ctCart = await this.ctCartService.getCart({ - // id: getSessionContext().cartId, - // }); - // - // //TODO: get payment amount from request and validate in the BE or get it directly in the BE - // const ctPayment = await this.ctPaymentService.createPayment({ - // amountPlanned: this.ctCartService.getPaymentAmount({ - // cart: ctCart, - // }), - // paymentMethodInfo: { - // paymentInterface: 'adyen-connect', - // }, - // ...(ctCart.customerId && { - // customer: { - // typeId: 'customer', - // id: ctCart.customerId, - // }, - // }), - // }); - // - // const updateCart = await this.ctCartService.addPayment({ - // resource: { - // id: ctCart.id, - // version: ctCart.version, - // }, - // paymentId: ctPayment.id, - // }); - // - // //TODO: amount cart != amount payment => update payment amount? - // - // const data = await this.createSessionConverter.convert({ - // data: opts.data, - // cart: updateCart as Cart, - // payment: ctPayment, - // }); - // - // try { - // const res = await paymentProviderApi().PaymentsApi.sessions(data); - // - // //TODO: save PSP interaction - // return { - // sessionData: res, - // paymentReference: ctPayment.id, - // }; - // } catch (e) { - // console.log(e); - // throw e; - // } - // } - public async createPayment(opts: CreatePayment): Promise { let ctCart, ctPayment; ctCart = await this.ctCartService.getCart({ id: getSessionContext().cartId, }); - if (opts.data.paymentReference) { - ctPayment = await this.ctPaymentService.getPayment({ - id: opts.data.paymentReference, - }); - } else { - ctPayment = await this.ctPaymentService.createPayment({ - amountPlanned: this.ctCartService.getPaymentAmount({ - cart: ctCart, - }), - paymentMethodInfo: { - paymentInterface: 'mock-payment', + ctPayment = await this.ctPaymentService.createPayment({ + amountPlanned: this.ctCartService.getPaymentAmount({ + cart: ctCart, + }), + paymentMethodInfo: { + // TODO: Fetch payment method from PSP + paymentInterface: 'mock-payment', + }, + ...(ctCart.customerId && { + customer: { + typeId: 'customer', + id: ctCart.customerId, }, - ...(ctCart.customerId && { - customer: { - typeId: 'customer', - id: ctCart.customerId, - }, - }), - }); + }), + }); - ctCart = await this.ctCartService.addPayment({ - resource: { - id: ctCart.id, - version: ctCart.version, - }, - paymentId: ctPayment.id, - }); - } + ctCart = await this.ctCartService.addPayment({ + resource: { + id: ctCart.id, + version: ctCart.version, + }, + paymentId: ctPayment.id, + }); //TODO: consolidate payment amount if needed const data = { @@ -122,7 +64,7 @@ export class DefaultPaymentService implements PaymentService { type: 'Authorization', amount: ctPayment.amountPlanned, interactionId: res.pspReference, - state: this.convertPaymentResultCode(res.resultCode as paymentOutcome), + state: this.convertPaymentResultCode(res.resultCode as PaymentOutcome), }, }); @@ -132,11 +74,11 @@ export class DefaultPaymentService implements PaymentService { }; } - private convertPaymentResultCode(resultCode: paymentOutcome): string { + private convertPaymentResultCode(resultCode: PaymentOutcome): string { switch (resultCode) { - case paymentOutcome.AUTHORIZED: + case PaymentOutcome.AUTHORIZED: return 'Success'; - case paymentOutcome.REJECTED: + case PaymentOutcome.REJECTED: return 'Failure'; default: return 'Initial';