# @exode-team/ofd-uz

Node.js module for interacting with OFD (Operator of Fiscal Data) tax system in Uzbekistan.

## Installation

```bash
npm install @exode-team/ofd-uz
```

## Usage

```typescript
import { OFDClient, QRPaymentInfo, Receipt } from '@exode-team/ofd-uz';

// Initialize client
const client = new OFDClient(
  'https://test.ofd.uz',
  privateKeyPEM, // Your private key in PEM format
  certificatePEM  // Your certificate in PEM format
);

// Send QR payment
const payment: QRPaymentInfo = {
  PaymentId: 'unique-payment-id',
  PaidSum: 100000, // in tiyins (100 sum = 10000 tiyin)
  DateTime: '2023-07-20 15:30:00',
  TIN: '123456789',
  PhoneNumber: '998901234567',
  SalePointAddress: 'Test address',
  IsRefund: 0,
  ReceiptInfo: {
    TerminalID: 'TEST123',
    ReceiptSeq: '1'
  }
};

try {
  const response = await client.sendQRPayment(payment);
  console.log('Payment sent:', response);
} catch (error) {
  console.error('Error sending payment:', error);
}

// Send receipt
const receipt: Receipt = {
  ReceiptSeq: 1,
  IsRefund: 0,
  Items: [
    {
      Name: 'Test Product',
      SPIC: '12345678901234567',
      PackageCode: 'PKG123',
      OwnerType: 0,
      GoodPrice: 50000,
      Price: 50000,
      VAT: 5000,
      VATPercent: 12,
      Amount: 1000
    }
  ],
  ReceivedCash: 0,
  ReceivedCard: 50000,
  TotalVAT: 5000,
  Time: '2023-07-20 15:30:00',
  ReceiptType: 0
};

try {
  const response = await client.sendReceipt(receipt);
  console.log('Receipt sent:', response);
} catch (error) {
  console.error('Error sending receipt:', error);
}
```

## Features

- Full TypeScript support
- Comprehensive type definitions
- Input validation
- PKCS#7 signing
- Error handling
- Helper functions for common operations

## API Documentation

### OFDClient

Main class for interacting with the OFD API.

#### Constructor

```typescript
constructor(baseURL: string, privateKeyPEM: string, certificatePEM: string)
```

#### Methods

- `sendQRPayment(payment: QRPaymentInfo): Promise<OFDResponse>`
- `sendReceipt(receipt: Receipt): Promise<OFDResponse>`

### Helper Functions

- `createTimestamp()`: Creates a timestamp in the required format
- `validatePhoneNumber(phone: string)`: Validates phone number format
- `calculateTotalAmount(items: Array<any>)`: Calculates total amount for items

## Error Handling

The module includes custom error handling:

```typescript
try {
  await client.sendReceipt(receipt);
} catch (error) {
  if (error instanceof ValidationError) {
    // Handle validation errors
  } else {
    // Handle other errors
  }
}
```

## License

MIT
