From bb2fe7f2a566bd325f3fc9727af930357d607037 Mon Sep 17 00:00:00 2001 From: Ludo Date: Mon, 30 Jun 2025 09:10:16 +0000 Subject: [PATCH] =?UTF-8?q?Nahr=C3=A1t=20soubory=20do=20=E2=80=9E/?= =?UTF-8?q?=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ server.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 README.md create mode 100644 server.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..3eba3e4 --- /dev/null +++ b/README.md @@ -0,0 +1,65 @@ +# VAT Number Validation API + +This Node.js application provides an API interface to validate European VAT numbers using the official VIES SOAP service. + +## Features + +- Validate VAT numbers for any EU member state +- GET and POST HTTP support +- JSON responses with company name, address, and validity + +## Installation + +```bash +npm install express cors body-parser soap +``` + +## Usage + +### Start the server + +```bash +node server.js +``` + +### Endpoints + +#### GET `/api/validate-vat` + +**Query parameters:** + +- `countryCode`: ISO 2-letter country code (e.g., SK, DE, FR) +- `vatNumber`: VAT number without country code + +**Example:** + +``` +http://localhost:3000/api/validate-vat?countryCode=SK&vatNumber=1234567890 +``` + +#### POST `/api/validate-vat` + +**Request body (JSON):** + +```json +{ + "countryCode": "SK", + "vatNumber": "1234567890" +} +``` + +**Response:** + +```json +{ + "country": "SK", + "VAT": "1234567890", + "name": "Example Company", + "address": "Main Street 123, City, Country", + "valid": true +} +``` + +## License + +MIT \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 0000000..a108d89 --- /dev/null +++ b/server.js @@ -0,0 +1,68 @@ +const express = require('express'); +const cors = require('cors'); +const bodyParser = require('body-parser'); +const soap = require('soap'); + +const app = express(); +const PORT = 3000; + +app.use(cors()); +app.use(bodyParser.json()); + +const VIES_WSDL = 'https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl'; + +// GET endpoint for validating VAT via URL +app.get('/api/validate-vat', async (req, res) => { + const countryCode = req.query.countryCode; + const vatNumber = req.query.vatNumber; + + if (!countryCode || !vatNumber) { + return res.status(400).json({ error: 'Missing countryCode or vatNumber in query parameters.' }); + } + + try { + const client = await soap.createClientAsync(VIES_WSDL); + const [result] = await client.checkVatAsync({ countryCode, vatNumber }); + + res.json({ + country: countryCode.toUpperCase(), + VAT: vatNumber, + name: result.name || null, + address: result.address || null, + valid: result.valid + }); + } catch (error) { + console.error('SOAP error:', error); + res.status(500).json({ error: 'Failed to validate VAT number.' }); + } +}); + +// POST endpoint (original) +app.post('/api/validate-vat', async (req, res) => { + const { countryCode, vatNumber } = req.body; + + if (!countryCode || !vatNumber) { + return res.status(400).json({ error: 'Missing required fields.' }); + } + + try { + const client = await soap.createClientAsync(VIES_WSDL); + const [result] = await client.checkVatAsync({ countryCode, vatNumber }); + + res.json({ + country: countryCode.toUpperCase(), + VAT: vatNumber, + name: result.name || null, + address: result.address || null, + valid: result.valid + }); + } catch (error) { + console.error('SOAP error:', error); + res.status(500).json({ error: 'Failed to validate VAT number.' }); + } +}); + +// Start server +app.listen(PORT, () => { + console.log(`✅ Server is running at http://localhost:${PORT}`); +}); \ No newline at end of file