-
Notifications
You must be signed in to change notification settings - Fork 102
feat(point-of-sale): POST payment route #3597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f5cb933
Payment route structure
oana-lolea 2d9416b
Merge branch 'pos-card-services' into oana/raf-1114
oana-lolea 3500840
Added routes test file
oana-lolea 2ce8ed4
Merge branch 'pos-card-services' into oana/raf-1114
oana-lolea 12b4d0e
Merge branch 'pos-card-services' into oana/raf-1114
oana-lolea 2528be6
Removed some comments
oana-lolea aedfd22
Added test for unknown error
oana-lolea e7d34d8
Updated graphql generated files
oana-lolea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,128 @@ | ||
| import { IocContract } from '@adonisjs/fold' | ||
| import { initIocContainer } from '..' | ||
| import { AppServices } from '../app' | ||
| import { Config } from '../config/app' | ||
| import { TestContainer, createTestApp } from '../tests/app' | ||
| import { PaymentContext, PaymentRoutes } from './routes' | ||
| import { truncateTables } from '../tests/tableManager' | ||
| import { PaymentService } from './service' | ||
| import { CardServiceClient, Result } from '../card-service-client/client' | ||
| import { createContext } from '../tests/context' | ||
| import { CardServiceClientError } from '../card-service-client/errors' | ||
|
|
||
| describe('Payment Routes', () => { | ||
| let deps: IocContract<AppServices> | ||
| let appContainer: TestContainer | ||
| let paymentRoutes: PaymentRoutes | ||
| let paymentService: PaymentService | ||
| let cardServiceClient: CardServiceClient | ||
|
|
||
| beforeAll(async () => { | ||
| deps = initIocContainer(Config) | ||
| appContainer = await createTestApp(deps) | ||
| paymentService = await deps.use('paymentClient') | ||
| cardServiceClient = await deps.use('cardServiceClient') | ||
| paymentRoutes = await deps.use('paymentRoutes') | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks() | ||
| }) | ||
|
|
||
| afterEach(async (): Promise<void> => { | ||
| await truncateTables(deps) | ||
| }) | ||
|
|
||
| afterAll(async (): Promise<void> => { | ||
| await appContainer.shutdown() | ||
| }) | ||
|
|
||
| describe('payment', () => { | ||
| test('returns 200 with result approved', async () => { | ||
| const ctx = createPaymentContext() | ||
| mockPaymentService() | ||
| jest | ||
| .spyOn(cardServiceClient, 'sendPayment') | ||
| .mockResolvedValueOnce(Result.APPROVED) | ||
|
|
||
| await paymentRoutes.payment(ctx) | ||
| expect(ctx.response.body).toBe(Result.APPROVED) | ||
| expect(ctx.status).toBe(200) | ||
| }) | ||
|
|
||
| test('returns 401 with result card_expired or invalid_signature', async () => { | ||
| const ctx = createPaymentContext() | ||
| mockPaymentService() | ||
| jest | ||
| .spyOn(cardServiceClient, 'sendPayment') | ||
| .mockResolvedValueOnce(Result.CARD_EXPIRED) | ||
|
|
||
| await paymentRoutes.payment(ctx) | ||
| expect(ctx.response.body).toBe(Result.CARD_EXPIRED) | ||
| expect(ctx.status).toBe(401) | ||
| }) | ||
|
|
||
| test('returns cardService error code when thrown', async () => { | ||
| const ctx = createPaymentContext() | ||
| mockPaymentService() | ||
| jest | ||
| .spyOn(cardServiceClient, 'sendPayment') | ||
| .mockRejectedValue(new CardServiceClientError('Some error', 404)) | ||
| await paymentRoutes.payment(ctx) | ||
| expect(ctx.response.body).toBe('Some error') | ||
| expect(ctx.status).toBe(404) | ||
| }) | ||
|
|
||
| test('returns 400 when there is a paymentService error', async () => { | ||
| const ctx = createPaymentContext() | ||
| jest | ||
| .spyOn(paymentService, 'getWalletAddress') | ||
| .mockRejectedValueOnce(new Error('Wallet address error')) | ||
| await paymentRoutes.payment(ctx) | ||
| expect(ctx.response.body).toBe('Wallet address error') | ||
| expect(ctx.status).toBe(400) | ||
| }) | ||
|
|
||
| test('returns 500 when an unknown error is thrown', async () => { | ||
| const ctx = createPaymentContext() | ||
| jest | ||
| .spyOn(paymentService, 'getWalletAddress') | ||
| .mockRejectedValueOnce('Unknown error') | ||
| await paymentRoutes.payment(ctx) | ||
| expect(ctx.response.body).toBe('Unknown error') | ||
| expect(ctx.status).toBe(500) | ||
| }) | ||
|
|
||
| function mockPaymentService() { | ||
| jest.spyOn(paymentService, 'getWalletAddress').mockResolvedValueOnce({ | ||
| id: 'id', | ||
| assetCode: 'USD', | ||
| assetScale: 1, | ||
| authServer: 'authServer', | ||
| resourceServer: 'resourceServer', | ||
| cardService: 'cardService' | ||
| }) | ||
| jest | ||
| .spyOn(paymentService, 'createIncomingPayment') | ||
| .mockResolvedValueOnce('incoming-payment-url') | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| function createPaymentContext() { | ||
| return createContext<PaymentContext>({ | ||
| headers: { Accept: 'application/json' }, | ||
| method: 'POST', | ||
| url: `/payment`, | ||
| body: { | ||
| card: { | ||
| walletAddress: 'wallet-address', | ||
| trasactionCounter: 0, | ||
| expiry: new Date(new Date().getDate() + 1) | ||
| }, | ||
| signature: 'signature', | ||
| value: 100, | ||
| merchantWalletAddress: 'merchant-wallet-address' | ||
| } | ||
| }) | ||
| } |
This file contains hidden or 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,93 @@ | ||
| import { AppContext } from '../app' | ||
| import { CardServiceClient, Result } from '../card-service-client/client' | ||
| import { AmountInput } from '../graphql/generated/graphql' | ||
| import { BaseService } from '../shared/baseService' | ||
| import { PaymentService } from './service' | ||
| import { CardServiceClientError } from '../card-service-client/errors' | ||
|
|
||
| interface ServiceDependencies extends BaseService { | ||
| paymentService: PaymentService | ||
| cardServiceClient: CardServiceClient | ||
| } | ||
|
|
||
| export type PaymentBody = { | ||
| card: { | ||
| walletAddress: string | ||
| trasactionCounter: number | ||
| expiry: Date | ||
| } | ||
| signature: string | ||
| value: bigint | ||
| merchantWalletAddress: string | ||
| } | ||
| type PaymentRequest = Exclude<AppContext['request'], 'body'> & { | ||
| body: PaymentBody | ||
| } | ||
|
|
||
| export type PaymentContext = Exclude<AppContext, 'request'> & { | ||
| request: PaymentRequest | ||
| } | ||
|
|
||
| export interface PaymentRoutes { | ||
| payment(ctx: PaymentContext): Promise<void> | ||
| } | ||
|
|
||
| export function createPaymentRoutes(deps_: ServiceDependencies): PaymentRoutes { | ||
| const log = deps_.logger.child({ | ||
| service: 'PaymentRoutes' | ||
| }) | ||
|
|
||
| const deps = { | ||
| ...deps_, | ||
| logger: log | ||
| } | ||
|
|
||
| return { | ||
| payment: (ctx: PaymentContext) => payment(deps, ctx) | ||
| } | ||
| } | ||
|
|
||
| async function payment( | ||
| deps: ServiceDependencies, | ||
| ctx: PaymentContext | ||
| ): Promise<void> { | ||
| const body = ctx.request.body | ||
| try { | ||
| const walletAddress = await deps.paymentService.getWalletAddress( | ||
| body.card.walletAddress | ||
| ) | ||
| const incomingAmount: AmountInput = { | ||
| assetCode: walletAddress.assetCode, | ||
| assetScale: walletAddress.assetScale, | ||
| value: body.value | ||
| } | ||
| const incomingPaymentUrl = await deps.paymentService.createIncomingPayment( | ||
| walletAddress.id, | ||
| incomingAmount | ||
| ) | ||
| const result = await deps.cardServiceClient.sendPayment({ | ||
| merchantWalletAddress: body.merchantWalletAddress, | ||
| incomingPaymentUrl, | ||
| date: new Date(), | ||
| signature: body.signature, | ||
| card: body.card | ||
| }) | ||
|
|
||
| ctx.body = result | ||
| ctx.status = result === Result.APPROVED ? 200 : 401 | ||
| } catch (err) { | ||
| const { body, status } = handlePaymentError(err) | ||
| ctx.body = body | ||
| ctx.status = status | ||
| } | ||
| } | ||
|
|
||
| function handlePaymentError(err: unknown) { | ||
| if (err instanceof CardServiceClientError) { | ||
| return { body: err.message, status: err.status } | ||
| } | ||
| if (err instanceof Error) { | ||
| return { body: err.message, status: 400 } | ||
| } | ||
| return { body: err, status: 500 } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.