# @mandarvl/vanilla-pay-international

A Node.js SDK for **Vanilla Pay International API**, supporting payments via mobile money, international cards, and more.

## Installation

```bash
npm install @mandarvl/vanilla-pay-international
```

---

## Usage

### 1. Setup environment variables

After creating and generating your keys from Vanilla Pay International, in your environment variables, add these :

```env
CLIENT_ID=your_client_id
CLIENT_SECRET=your_client_secret
ENV=PROD   # or PREPROD
```

---

### 2. Initialize client and make a payment

```ts
import dotenv from 'dotenv';
import { VanillaPayInternational, Environment } from '@mandarvl/vanilla-pay-international';

dotenv.config();

async function main() {
  const clientId = process.env.CLIENT_ID;
  const clientSecret = process.env.CLIENT_SECRET;
  const env = process.env.ENV as Environment;

  if (!clientId || !clientSecret || !env) {
    throw new Error('CLIENT_ID, CLIENT_SECRET, and ENV are required');
  }

  const vanillaPay = new VanillaPayInternational(env);

  try {
    const auth = await vanillaPay.auth.generateToken(clientId, clientSecret);
    vanillaPay.setAccessToken(auth.Token);

    const payment = await vanillaPay.payment.init({
      amount: 10000,
      cart: '123',
      currency: 'MGA',
      reference: '123',
      paymentMethod: 'mobile_money',
      callbackUrl: 'https://your-callback-url.com',
      redirectUrl: 'https://your-redirect-url.com'
    });

    console.log(payment);

    const paymentDetails = await vanillaPay.payment.get(payment.id, 'mobile_money');
    console.log(paymentDetails);
  } catch (err) {
    console.error(err);
  }
}

main();
```

---

### 3. Handle Payment Callbacks

Vanilla Pay International posts to your `callbackUrl` with headers:

* `VPI-Signature`
* `Content-Type: application/x-www-form-urlencoded`

Validate the payload using:

```ts
import { Utils } from '@mandarvl/vanilla-pay-international';

const isValid = Utils.formatAndValidatePayload(vpiSignature, body, keySecret);

if (isValid) {
  // process payment
} else {
  // invalid signature
}
```

* `vpiSignature`: value from headers
* `body`: payload object
* `keySecret`: generated from your account

---

### 4. Supported Payment Methods

* `mobile_money` (Mvola, Orange Money, Airtel Money)
* `international` (Bank Cards and Paypal)

---

### 5. License

MIT © Manda Ravalison