-
Notifications
You must be signed in to change notification settings - Fork 7
feat: implement public endpoints for invoice resolution and payment confirmation #23
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
+199
−23
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,23 @@ | ||
| import type { JestConfigWithTsJest } from 'ts-jest'; | ||
|
|
||
| const jestConfig: JestConfigWithTsJest = { | ||
| preset: 'ts-jest/presets/default-esm', | ||
| testEnvironment: 'node', | ||
| extensionsToTreatAsEsm: ['.ts'], | ||
| moduleNameMapper: { | ||
| '^(\\.{1,2}/.*)\\.js$': '$1', | ||
| }, | ||
| transform: { | ||
| '^.+\\.tsx?$': [ | ||
| 'ts-jest', | ||
| { | ||
| useESM: true, | ||
| }, | ||
| ], | ||
| }, | ||
| modulePathIgnorePatterns: ['<rootDir>/dist/'], | ||
| roots: ['<rootDir>/tests/'], | ||
| setupFiles: ['<rootDir>/tests/jest.setup.ts'], | ||
| preset: 'ts-jest/presets/default-esm', | ||
| testEnvironment: 'node', | ||
| extensionsToTreatAsEsm: ['.ts'], | ||
| moduleNameMapper: { | ||
| '^(\\.{1,2}/.*)\\.js$': '$1', | ||
| }, | ||
| transform: { | ||
| '^.+\\.tsx?$': [ | ||
| 'ts-jest', | ||
| { | ||
| useESM: true, | ||
| }, | ||
| ], | ||
| }, | ||
| modulePathIgnorePatterns: ['<rootDir>/dist/'], | ||
| roots: ['<rootDir>/tests/'], | ||
| setupFiles: ['<rootDir>/tests/jest.setup.ts'], | ||
| }; | ||
|
|
||
| export default jestConfig; |
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 |
|---|---|---|
| @@ -1,14 +1,14 @@ | ||
| // This file was generated by Prisma, and assumes you have installed the following: | ||
| // npm install --save-dev prisma dotenv | ||
| import "dotenv/config"; | ||
| import { defineConfig } from "prisma/config"; | ||
| import 'dotenv/config'; | ||
| import { defineConfig } from 'prisma/config'; | ||
|
|
||
| export default defineConfig({ | ||
| schema: "prisma/schema.prisma", | ||
| schema: 'prisma/schema.prisma', | ||
| migrations: { | ||
| path: "prisma/migrations", | ||
| path: 'prisma/migrations', | ||
| }, | ||
| datasource: { | ||
| url: process.env["DATABASE_URL"], | ||
| url: process.env['DATABASE_URL'], | ||
| }, | ||
| }); |
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,51 @@ | ||
| import { Request, Response } from 'express'; | ||
| import { resolveInvoiceBySlug, confirmPayment } from '../services/pay.services.js'; | ||
| import { AppError } from '../utils/errors.js'; | ||
|
|
||
| export const resolveInvoiceController = async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { slug } = req.params; | ||
| const invoice = await resolveInvoiceBySlug(slug); | ||
| res.status(200).json(invoice); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| if (error.statusCode === 410 && error.message === 'expired') { | ||
| res.status(410).json({ reason: 'expired' }); | ||
| return; | ||
| } | ||
| res.status(error.statusCode).json({ error: error.message }); | ||
| return; | ||
| } | ||
| res.status(500).json({ error: 'Internal Server Error' }); | ||
| } | ||
| }; | ||
|
|
||
| export const confirmPaymentController = async (req: Request, res: Response): Promise<void> => { | ||
| try { | ||
| const { slug } = req.params; | ||
| const { payerAddress, txHash } = req.body; | ||
|
|
||
| if (!payerAddress || typeof payerAddress !== 'string') { | ||
| res.status(400).json({ error: 'payerAddress is required and must be a string' }); | ||
| return; | ||
| } | ||
|
|
||
| if (txHash !== undefined && typeof txHash !== 'string') { | ||
| res.status(400).json({ error: 'txHash must be a string if provided' }); | ||
| return; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| await confirmPayment(slug, payerAddress, txHash); | ||
| res.status(202).json({ message: 'Payment confirmation received' }); | ||
| } catch (error) { | ||
| if (error instanceof AppError) { | ||
| if (error.statusCode === 410 && error.message === 'expired') { | ||
| res.status(410).json({ reason: 'expired' }); | ||
| return; | ||
| } | ||
| res.status(error.statusCode).json({ error: error.message }); | ||
| return; | ||
| } | ||
| res.status(500).json({ error: 'Internal Server Error' }); | ||
| } | ||
| }; | ||
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 |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| import merchantRoutes from './merchant.routes.js'; | ||
| import authRoutes from './auth.routes.js'; | ||
| import invoiceRoutes from './invoice.routes.js'; | ||
| import payRoutes from './pay.routes.js'; | ||
| import { Router } from 'express'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.use('/merchants', merchantRoutes); | ||
| router.use('/auth', authRoutes); | ||
| router.use('/invoices', invoiceRoutes); | ||
| router.use('/pay', payRoutes); | ||
|
|
||
| export default router; |
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,12 @@ | ||
| import { Router } from 'express'; | ||
| import { | ||
| resolveInvoiceController, | ||
| confirmPaymentController, | ||
| } from '../controllers/pay.controllers.js'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.get('/:slug', resolveInvoiceController); | ||
| router.post('/:slug/confirm', confirmPaymentController); | ||
|
|
||
| export default router; |
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,98 @@ | ||
| import prisma from '../config/prisma.js'; | ||
| import { AppError } from '../utils/errors.js'; | ||
| import type { InvoiceStatus as PrismaInvoiceStatus } from '@prisma/client'; | ||
|
|
||
| const InvoiceStatus = { | ||
| DRAFT: 'DRAFT', | ||
| PENDING: 'PENDING', | ||
| PAID: 'PAID', | ||
| CANCELLED: 'CANCELLED', | ||
| REFUNDED: 'REFUNDED', | ||
| } as const satisfies Record<string, PrismaInvoiceStatus>; | ||
|
|
||
| export const resolveInvoiceBySlug = async (slug: string) => { | ||
| const invoice = await prisma.invoice.findUnique({ | ||
| where: { paymentSlug: slug }, | ||
| select: { | ||
| paymentSlug: true, | ||
| description: true, | ||
| amount: true, | ||
| token: true, | ||
| status: true, | ||
| expiresAt: true, | ||
| pricingMode: true, | ||
| merchant: { | ||
| select: { | ||
| businessName: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!invoice) { | ||
| throw new AppError(404, 'Invoice not found'); | ||
| } | ||
|
|
||
| if ( | ||
| invoice.status === InvoiceStatus.CANCELLED || | ||
| invoice.status === InvoiceStatus.PAID || | ||
| invoice.status === InvoiceStatus.REFUNDED | ||
| ) { | ||
| throw new AppError(410, 'Invoice is no longer available'); | ||
| } | ||
|
|
||
| if (invoice.expiresAt && invoice.expiresAt < new Date()) { | ||
| throw new AppError(410, 'expired'); | ||
| } | ||
|
|
||
| return { | ||
| slug: invoice.paymentSlug, | ||
| description: invoice.description, | ||
| amount: invoice.amount.toString(), | ||
| token: invoice.token, | ||
| status: invoice.status, | ||
| merchantName: invoice.merchant.businessName, | ||
| expiresAt: invoice.expiresAt, | ||
| pricingMode: invoice.pricingMode, | ||
| }; | ||
| }; | ||
|
|
||
| export const confirmPayment = async (slug: string, payerAddress: string, txHash?: string) => { | ||
| return await prisma.$transaction(async tx => { | ||
| const invoice = await tx.invoice.findUnique({ | ||
| where: { paymentSlug: slug }, | ||
| }); | ||
|
|
||
| if (!invoice) { | ||
| throw new AppError(404, 'Invoice not found'); | ||
| } | ||
|
|
||
| if ( | ||
| invoice.status === InvoiceStatus.CANCELLED || | ||
| invoice.status === InvoiceStatus.PAID || | ||
| invoice.status === InvoiceStatus.REFUNDED | ||
| ) { | ||
| throw new AppError(410, 'Invoice is no longer available'); | ||
| } | ||
|
|
||
| if (invoice.expiresAt && invoice.expiresAt < new Date()) { | ||
| throw new AppError(410, 'expired'); | ||
| } | ||
|
|
||
| const idempotencyKey = `${invoice.id}-${payerAddress}-${txHash || 'none'}`; | ||
|
|
||
| const confirmation = await tx.paymentConfirmation.upsert({ | ||
| where: { idempotencyKey }, | ||
| update: {}, | ||
| create: { | ||
| invoiceId: invoice.id, | ||
| merchantId: invoice.merchantId, | ||
| payerAddress, | ||
| txHash: txHash || null, | ||
| idempotencyKey, | ||
| }, | ||
| }); | ||
|
|
||
| return confirmation; | ||
| }); | ||
| }; |
Oops, something went wrong.
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.