-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from commercetools/dasanorct/SCC-2148_adyen_apis
feat(adyen-template): added a first version of adyen APIs
- Loading branch information
Showing
21 changed files
with
727 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
processor/src/clients/adyen/adyen.client.ts → processor/src/clients/adyen.client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { CreateCheckoutSessionRequest } from '@adyen/api-library/lib/src/typings/checkout/createCheckoutSessionRequest'; | ||
import { CreateCheckoutSessionResponse } from '@adyen/api-library/lib/src/typings/checkout/createCheckoutSessionResponse'; | ||
import { PaymentDetailsRequest } from '@adyen/api-library/lib/src/typings/checkout/paymentDetailsRequest'; | ||
import { PaymentDetailsResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentDetailsResponse'; | ||
import { PaymentMethodsRequest } from '@adyen/api-library/lib/src/typings/checkout/paymentMethodsRequest'; | ||
import { PaymentMethodsResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentMethodsResponse'; | ||
import { PaymentRequest } from '@adyen/api-library/lib/src/typings/checkout/paymentRequest'; | ||
import { PaymentResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentResponse'; | ||
import { Notification } from '@adyen/api-library/lib/src/typings/notification/notification'; | ||
|
||
export type PaymentMethodsRequestDTO = Omit<PaymentMethodsRequest, 'amount' | 'merchantAccount' | 'countryCode'>; | ||
export type PaymentMethodsResponseDTO = PaymentMethodsResponse; | ||
|
||
export type CreateSessionRequestDTO = Omit< | ||
CreateCheckoutSessionRequest, | ||
| 'amount' | ||
| 'merchantAccount' | ||
| 'countryCode' | ||
| 'returnUrl' | ||
| 'reference' | ||
| 'storePaymentMethod' | ||
| 'shopperReference' | ||
| 'recurringProcessingModel' | ||
| 'storePaymentMethodMode' | ||
>; | ||
|
||
export type CreateSessionResponseDTO = { | ||
sessionData: CreateCheckoutSessionResponse; | ||
paymentReference: string; | ||
}; | ||
|
||
export type CreatePaymentRequestDTO = Omit< | ||
PaymentRequest, | ||
| 'amount' | ||
| 'additionalAmount' | ||
| 'merchantAccount' | ||
| 'countryCode' | ||
| 'returnUrl' | ||
| 'lineItems' | ||
| 'reference' | ||
| 'shopperReference' | ||
| 'recurringProcessingModel' | ||
> & { | ||
paymentReference?: string; | ||
}; | ||
|
||
export type CreatePaymentResponseDTO = Pick< | ||
PaymentResponse, | ||
'action' | 'resultCode' | 'threeDS2ResponseData' | 'threeDS2Result' | 'threeDSPaymentData' | ||
> & { | ||
paymentReference: string; | ||
}; | ||
|
||
export type ConfirmPaymentRequestDTO = PaymentDetailsRequest & { | ||
paymentReference: string; | ||
}; | ||
|
||
export type ConfirmPaymentResponseDTO = Pick< | ||
PaymentDetailsResponse, | ||
'resultCode' | 'threeDS2ResponseData' | 'threeDS2Result' | 'threeDSPaymentData' | ||
> & { | ||
paymentReference: string; | ||
}; | ||
|
||
export type NotificationRequestDTO = Notification; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import HmacValidator from '@adyen/api-library/lib/src/utils/hmacValidator'; | ||
import { config } from '../../../config/config'; | ||
import { FastifyRequest } from 'fastify'; | ||
import { ErrorAuthErrorResponse } from '@commercetools/connect-payments-sdk'; | ||
import { NotificationRequestDTO } from '../../../dtos/adyen-payment.dto'; | ||
|
||
export class HmacAuthHook { | ||
constructor() {} | ||
|
||
public authenticate() { | ||
return async (request: FastifyRequest) => { | ||
const data = request.body as NotificationRequestDTO; | ||
if (!data.notificationItems || data.notificationItems.length === 0) { | ||
throw new ErrorAuthErrorResponse('Unexpected payload'); | ||
} | ||
|
||
const validator = new HmacValidator(); | ||
const item = data.notificationItems[0].NotificationRequestItem; | ||
|
||
if (!validator.validateHMAC(item, config.adyenHMACKey)) { | ||
throw new ErrorAuthErrorResponse('HMAC is not valid'); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,114 @@ | ||
import { SessionAuthenticationHook } from '@commercetools/connect-payments-sdk'; | ||
import { FastifyInstance, FastifyPluginOptions } from 'fastify'; | ||
import { PaymentNotificationSchemaDTO } from '../dtos/adyen-payment.dts'; | ||
import { | ||
ConfirmPaymentRequestDTO, | ||
ConfirmPaymentResponseDTO, | ||
CreatePaymentRequestDTO, | ||
CreatePaymentResponseDTO, | ||
CreateSessionRequestDTO, | ||
CreateSessionResponseDTO, | ||
NotificationRequestDTO, | ||
PaymentMethodsRequestDTO, | ||
PaymentMethodsResponseDTO, | ||
} from '../dtos/adyen-payment.dto'; | ||
import { AdyenPaymentService } from '../services/adyen-payment.service'; | ||
|
||
const ACK_NOTIFICATION = '[accepted]'; | ||
import { config } from '../config/config'; | ||
import { HmacAuthHook } from '../libs/fastify/hooks/hmac-auth.hook'; | ||
|
||
type PaymentRoutesOptions = { | ||
paymentService: AdyenPaymentService; | ||
sessionAuthHook: SessionAuthenticationHook; | ||
hmacAuthHook: HmacAuthHook; | ||
}; | ||
|
||
export const paymentRoutes = async (fastify: FastifyInstance, opts: FastifyPluginOptions & PaymentRoutesOptions) => { | ||
/** | ||
* Listen to the notification from Adyen | ||
*/ | ||
fastify.post<{ Body: PaymentNotificationSchemaDTO; Reply: any }>('/notifications', {}, async (request, reply) => { | ||
await opts.notificationService.processNotification({ | ||
data: request.body, | ||
export const adyenPaymentRoutes = async ( | ||
fastify: FastifyInstance, | ||
opts: FastifyPluginOptions & PaymentRoutesOptions, | ||
) => { | ||
fastify.post<{ Body: PaymentMethodsRequestDTO; Reply: PaymentMethodsResponseDTO }>( | ||
'/payment-methods', | ||
{ | ||
preHandler: [opts.sessionAuthHook.authenticate()], | ||
}, | ||
async (request, reply) => { | ||
const resp = await opts.paymentService.getPaymentMethods({ | ||
data: request.body, | ||
}); | ||
|
||
return reply.status(200).send(resp); | ||
}, | ||
); | ||
|
||
fastify.post<{ Body: CreateSessionRequestDTO; Reply: CreateSessionResponseDTO }>( | ||
'/sessions', | ||
{ | ||
preHandler: [opts.sessionAuthHook.authenticate()], | ||
}, | ||
async (request, reply) => { | ||
const resp = await opts.paymentService.createSession({ | ||
data: request.body, | ||
}); | ||
|
||
return reply.status(200).send(resp); | ||
}, | ||
); | ||
|
||
fastify.post<{ Body: CreatePaymentRequestDTO; Reply: CreatePaymentResponseDTO }>( | ||
'/payments', | ||
{ | ||
preHandler: [opts.sessionAuthHook.authenticate()], | ||
}, | ||
async (request, reply) => { | ||
const resp = await opts.paymentService.createPayment({ | ||
data: request.body, | ||
}); | ||
|
||
return reply.status(200).send(resp); | ||
}, | ||
); | ||
|
||
fastify.get<{ Reply: ConfirmPaymentResponseDTO }>('/payments/details', {}, async (request, reply) => { | ||
const queryParams = request.query as any; | ||
const res = await opts.paymentService.confirmPayment({ | ||
data: { | ||
details: { | ||
redirectResult: queryParams.redirectResult as string, | ||
}, | ||
paymentReference: queryParams.paymentReference as string, | ||
}, | ||
}); | ||
|
||
return reply.status(200).send(ACK_NOTIFICATION); | ||
return reply.redirect(buildRedirectUrl(res.paymentReference)); | ||
}); | ||
|
||
fastify.post<{ Body: ConfirmPaymentRequestDTO; Reply: ConfirmPaymentResponseDTO }>( | ||
'/payments/details', | ||
{}, | ||
async (request, reply) => { | ||
const res = await opts.paymentService.confirmPayment({ | ||
data: request.body, | ||
}); | ||
return reply.status(200).send(res); | ||
}, | ||
); | ||
|
||
fastify.post<{ Body: NotificationRequestDTO }>( | ||
'/notifications', | ||
{ | ||
preHandler: [opts.hmacAuthHook.authenticate()], | ||
}, | ||
async (request, reply) => { | ||
await opts.notificationService.processNotification({ | ||
data: request.body, | ||
}); | ||
|
||
return reply.status(200).send('[accepted]'); | ||
}, | ||
); | ||
}; | ||
|
||
const buildRedirectUrl = (paymentReference: string) => { | ||
const redirectUrl = new URL(config.merchantReturnUrl); | ||
redirectUrl.searchParams.append('paymentReference', paymentReference); | ||
return redirectUrl.toString(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { FastifyInstance } from 'fastify'; | ||
import { paymentSDK } from '../../payment-sdk'; | ||
import { paymentRoutes } from '../../routes/mock-payment.route'; | ||
import { MockPaymentService } from '../../services/mock-payment.service'; | ||
import { adyenPaymentRoutes } from '../../routes/adyen-payment.route'; | ||
import { AdyenPaymentService } from '../../services/adyen-payment.service'; | ||
import { HmacAuthHook } from '../../libs/fastify/hooks/hmac-auth.hook'; | ||
|
||
export default async function (server: FastifyInstance) { | ||
const mockPaymentService = new MockPaymentService({ | ||
const paymentService = new AdyenPaymentService({ | ||
ctCartService: paymentSDK.ctCartService, | ||
ctPaymentService: paymentSDK.ctPaymentService, | ||
}); | ||
|
||
await server.register(paymentRoutes, { | ||
paymentService: mockPaymentService, | ||
await server.register(adyenPaymentRoutes, { | ||
paymentService, | ||
sessionAuthHook: paymentSDK.sessionAuthHookFn, | ||
hmacAuthHook: new HmacAuthHook(), | ||
}); | ||
} |
Oops, something went wrong.