Skip to content

Commit

Permalink
Remove create session and adyen specific libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
praveenkumarct committed Jan 29, 2024
1 parent 043e4ae commit 3a64d5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 112 deletions.
2 changes: 0 additions & 2 deletions processor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
30 changes: 5 additions & 25 deletions processor/src/dtos/payment.dto.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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(),
});

Expand Down
112 changes: 27 additions & 85 deletions processor/src/services/payment.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { CommercetoolsCartService, CommercetoolsPaymentService, } from '@commercetools/connect-payments-sdk';
import { CommercetoolsCartService, CommercetoolsPaymentService } from '@commercetools/connect-payments-sdk';
import { paymentProviderApi } from '../clients/client';
import {
CreatePayment,
PaymentService,
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;
Expand All @@ -17,93 +17,35 @@ export class DefaultPaymentService implements PaymentService {
this.ctPaymentService = opts.ctPaymentService;
}

// TODO

// public async createSession(opts: CreateSession): Promise<SessionData> {
// 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<PaymentResponseSchemaDTO> {
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 = {
Expand All @@ -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),
},
});

Expand All @@ -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';
Expand Down

0 comments on commit 3a64d5a

Please sign in to comment.