> ## Documentation Index
> Fetch the complete documentation index at: https://developers.pcibooking.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Payments Library Setup

> Install and initialize PCI Booking's JavaScript Payments Library. Script loading, configuration, and first render.

## Prerequisites

* A PCI Booking merchant account.
* Credentials configured for the payment methods you want to accept. Contact your PCI Booking account manager for setup.

## Installation

### npm

```bash theme={null}
npm install @pcibooking/ewallet
```

```javascript theme={null}
import eWallet from '@pcibooking/ewallet';
```

### CDN

```html theme={null}
<script src="https://cdn.jsdelivr.net/npm/@pcibooking/ewallet@latest/dist/index.js"></script>
```

The library exposes a global `eWallet` object when loaded via CDN.

## Create a Session

Before initializing the library, your server must create a [payment session](/api-reference/payments-library/create-session) with PCI Booking. The session defines the operation type (e.g. `TOKENIZE`, `CHARGE`) and returns a session token.

Pass the session token to your frontend to initialize the library.

<CodeGroup>
  ```javascript Node.js theme={null}
  const response = await fetch('https://service.pcibooking.net/api/eWalletOperation', {
    method: 'POST',
    headers: {
      'Authorization': 'APIKEY your-api-key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      operation: 'CHARGE',
      mode: 'LIVE',
      PaymentGatewayAccountId: 'my-gateway-credentials',
      AllowedeWalletAccountIds: 'my-applepay,my-googlepay',
      CurrencyCode: 'USD',
      Amount: 99.99,
      CountryCode: 'US',
      AllowedBrands: 'visa,mastercard,amex',
      CustomerEmail: 'customer@example.com',
      merchantReference: 'ORDER-12345'
    })
  });
  const { sessionId } = await response.json();
  // Pass sessionId to your frontend
  ```

  ```python Python theme={null}
  import requests

  response = requests.post(
      'https://service.pcibooking.net/api/eWalletOperation',
      headers={
          'Authorization': 'APIKEY your-api-key',
          'Content-Type': 'application/json'
      },
      json={
          'operation': 'CHARGE',
          'mode': 'LIVE',
          'PaymentGatewayAccountId': 'my-gateway-credentials',
          'AllowedeWalletAccountIds': 'my-applepay,my-googlepay',
          'CurrencyCode': 'USD',
          'Amount': 99.99,
          'CountryCode': 'US',
          'AllowedBrands': 'visa,mastercard,amex',
          'CustomerEmail': 'customer@example.com',
          'merchantReference': 'ORDER-12345'
      }
  )
  session_id = response.json()['sessionId']
  # Pass session_id to your frontend
  ```
</CodeGroup>

## Minimal Working Example

```html theme={null}
<!DOCTYPE html>
<html>
<head>
  <title>Checkout</title>
  <script src="https://cdn.jsdelivr.net/npm/@pcibooking/ewallet@latest/dist/index.js"></script>
</head>
<body>
  <div id="payment-buttons"></div>

  <script>
    async function startCheckout() {
      // Session token from your server (via Create Session API)
      const sessionToken = 'YOUR_SESSION_TOKEN';

      const engine = new eWallet.Engine(sessionToken);
      const methods = await engine.checkAvailability();

      engine.payBy(methods, function(result) {
        if (!result) {
          alert('Payment cancelled');
          return;
        }

        const [data, success] = engine.parseResultToken(result.token);
        if (data.clientSuccess && success) {
          // Send result.token to your server for validation
          console.log('Success, token:', result.token);
        }
      }, {
        containerSelector: '#payment-buttons'
      });
    }

    startCheckout();
  </script>
</body>
</html>
```

<Info>
  Session tokens are single-use. Create a new session for each checkout attempt.
</Info>

## Server-Side Validation

After the customer completes the payment, your callback receives a result token. Send this token to your server and call the [Validate Operation](/api-reference/payments-library/validate-operation) endpoint to confirm the result is authentic.

<CodeGroup>
  ```javascript Node.js theme={null}
  // In your server endpoint that receives the result token from the frontend
  async function validatePaymentResult(resultToken) {
    const response = await fetch('https://service.pcibooking.net/api/eWalletOperation/validateResults', {
      method: 'POST',
      headers: {
        'Authorization': 'APIKEY your-api-key',
        'Content-Type': 'text/plain'
      },
      body: resultToken
    });
    const data = await response.json();
    return data.valid; // true if the token is authentic
  }
  ```

  ```python Python theme={null}
  import requests

  def validate_payment_result(result_token):
      """Call from your server after receiving the result token from the frontend."""
      response = requests.post(
          'https://service.pcibooking.net/api/eWalletOperation/validateResults',
          headers={
              'Authorization': 'APIKEY your-api-key',
              'Content-Type': 'text/plain'
          },
          data=result_token
      )
      data = response.json()
      return data['valid']  # True if the token is authentic
  ```
</CodeGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="Supported Payment Methods" icon="book" href="/payments-library/supported-methods">
    See which methods are available and their supported operations
  </Card>

  <Card title="Library Reference" icon="code" href="/payments-library/reference">
    Full API surface: constructor parameters, all methods, UI options
  </Card>
</CardGroup>
