Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,94 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
- Website - [https://nestjs.com](https://nestjs.com/)
- Twitter - [@nestframework](https://twitter.com/nestframework)

## Payment Request Validation

The backend supports validation and normalization of payment requests (including direct fields, serialized JSON strings, and URI schemes).

### Endpoint
`POST /v1/payment-requests/validate`

### Curl Examples

#### 1. Direct Structured Request (Valid)
```bash
curl -X POST http://localhost:3000/v1/payment-requests/validate \
-H "Content-Type: application/json" \
-d '{
"asset": "USDC",
"recipient": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"amount": 250.0
}'
```

Response:
```json
{
"valid": true,
"normalizedPayload": {
"asset": "USDC",
"recipient": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"amount": 250
}
}
```

#### 2. URI String Payment Request (Valid)
```bash
curl -X POST http://localhost:3000/v1/payment-requests/validate \
-H "Content-Type: application/json" \
-d '{
"payload": "ethereum:0x742d35Cc6634C0532925a3b844Bc454e4438f44e?amount=150.0&asset=USDC"
}'
```

Response:
```json
{
"valid": true,
"normalizedPayload": {
"asset": "USDC",
"recipient": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
"amount": 150
}
}
```

#### 3. Request with Validation Errors (Invalid)
```bash
curl -X POST http://localhost:3000/v1/payment-requests/validate \
-H "Content-Type: application/json" \
-d '{
"asset": "DOGE",
"recipient": "0xInvalidEthAddress",
"amount": -100
}'
```

Response:
```json
{
"valid": false,
"errors": [
{
"code": "INVALID_ASSET",
"message": "Asset 'DOGE' is not supported. Supported assets are: USDC, USDT, ETH, BTC, SOL, USD, EUR.",
"field": "asset"
},
{
"code": "INVALID_RECIPIENT",
"message": "Recipient address starts with 0x but is not a valid Ethereum address.",
"field": "recipient"
},
{
"code": "INVALID_AMOUNT",
"message": "Amount must be greater than zero.",
"field": "amount"
}
]
}
```

## License

Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
98 changes: 84 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@nestjs/config": "^4.0.4",
"@nestjs/core": "^11.0.1",
"@nestjs/platform-express": "^11.0.1",
"@prisma/client": "^7.8.0",
"@prisma/client": "^6.3.0",
"@supabase/supabase-js": "^2.104.1",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1"
Expand All @@ -45,6 +45,7 @@
"globals": "^16.0.0",
"jest": "^30.0.0",
"prettier": "^3.4.2",
"prisma": "^6.3.0",
"source-map-support": "^0.5.21",
"supertest": "^7.0.0",
"ts-jest": "^29.2.5",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SupabaseModule } from './supabase/supabase.module';
import { UsersModule } from './users/users.module';
import { PaymentsModule } from './payments/payments.module';
import { TransactionsModule } from './transactions/transactions.module';
import { PaymentRequestsModule } from './payment-requests/payment-requests.module';

@Module({
imports: [
Expand All @@ -18,6 +19,7 @@ import { TransactionsModule } from './transactions/transactions.module';
UsersModule,
PaymentsModule,
TransactionsModule,
PaymentRequestsModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
7 changes: 7 additions & 0 deletions src/payment-requests/dto/validate-payment-request.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class ValidatePaymentRequestDto {
asset?: string;
recipient?: string;
amount?: number | string;
expiresAt?: string | number;
payload?: any;
}
41 changes: 41 additions & 0 deletions src/payment-requests/payment-requests.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PaymentRequestsController } from './payment-requests.controller';
import { PaymentRequestsService } from './payment-requests.service';

describe('PaymentRequestsController', () => {
let controller: PaymentRequestsController;
let service: PaymentRequestsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PaymentRequestsController],
providers: [PaymentRequestsService],
}).compile();

controller = module.get<PaymentRequestsController>(
PaymentRequestsController,
);
service = module.get<PaymentRequestsService>(PaymentRequestsService);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('validate', () => {
it('should call service.validate and return result', () => {
const dto = {
asset: 'USDC',
recipient: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
amount: 100,
};

const spy = jest.spyOn(service, 'validate');
const result = controller.validate(dto);

expect(spy).toHaveBeenCalledWith(dto);
expect(result.valid).toBe(true);
expect(result.normalizedPayload?.amount).toBe(100);
});
});
});
19 changes: 19 additions & 0 deletions src/payment-requests/payment-requests.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Controller, Post, Body, HttpCode, HttpStatus } from '@nestjs/common';
import {
PaymentRequestsService,
ValidationResult,
} from './payment-requests.service';
import { ValidatePaymentRequestDto } from './dto/validate-payment-request.dto';

@Controller('v1/payment-requests')
export class PaymentRequestsController {
constructor(
private readonly paymentRequestsService: PaymentRequestsService,
) {}

@Post('validate')
@HttpCode(HttpStatus.OK)
validate(@Body() dto: ValidatePaymentRequestDto): ValidationResult {
return this.paymentRequestsService.validate(dto);
}
}
10 changes: 10 additions & 0 deletions src/payment-requests/payment-requests.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { PaymentRequestsService } from './payment-requests.service';
import { PaymentRequestsController } from './payment-requests.controller';

@Module({
providers: [PaymentRequestsService],
controllers: [PaymentRequestsController],
exports: [PaymentRequestsService],
})
export class PaymentRequestsModule {}
Loading
Loading