
# TrustPayWay SDK (Node.js / TypeScript / JavaScript)

A simple, lightweight Node.js SDK for interacting with the [TrustPayWay](https://mobilewallet.trustpayway.com) mobile payment API. Supports MTN, Orange, and others.

Built with TypeScript. Usable in both TypeScript and plain JavaScript projects.

---

## 🚀 Features

- 🔐 Authenticate via application token
- 💸 Initiate payments (MTN & Orange supported)
- 📊 Check payment status
- 🧩 Fully typed for TypeScript support
- ⚙️ Works in any Node.js project (TS or JS)

---

## 📦 Installation

```bash
npm install trustpayway-sdk
```

---

## 🧑‍💻 Usage

### ▶️ TypeScript Example

```ts
import { TrustPayWay } from 'trustpayway-sdk';

const tpw = new TrustPayWay({
  baseUrl: 'https://mobilewallet.trustpayway.com',//https://test.trustpayway.com (for testing purposes)
  appToken: 'your-app-token',
  authToken: 'your-bearer-token',
  network: 'orange', // or 'mtn'
});

async function run() {
  const result = await tpw.processPayment({
    amount: '100',
    currency: 'XAF',
    phone: '237600000000',
    description: 'Invoice #123',
    orderId: 'TPW-123',
    notifUrl: 'https://webhook.site/your-webhook',
  });

  console.log('Transaction Response:', result);
}
```

---

### 💡 JavaScript (Vanilla Node.js) Example

```js
const { TrustPayWay } = require('trustpayway-sdk');

const tpw = new TrustPayWay({
  baseUrl: 'https://mobilewallet.trustpayway.com',//https://test.trustpayway.com (for testing purposes)
  appToken: 'your-app-token',
  authToken: 'your-bearer-token',
  network: 'mtn', // or 'orange'
});

async function run() {
  try {
    const result = await tpw.processPayment({
      amount: '100',
      currency: 'XAF',
      phone: '237600000000',
      description: 'Invoice #123',
      orderId: 'TPW-123',
      notifUrl: 'https://webhook.site/your-webhook',
    });

    console.log('Transaction Response:', result);
  } catch (error) {
    console.error('Payment Failed:', error);
  }
}

run();
```

---

## 📚 API Reference

### `new TrustPayWay(config)`

Create a new SDK instance.

**Parameters:**
- `baseUrl` (string): API base URL (e.g. `https://mobilewallet.trustpayway.com`)
- `appToken` (string): Your application token from TrustPayWay
- `authToken` (string): Authorization token (Bearer)
- `network` (string): `'mtn'` or `'orange'`

---

### `getToken(): Promise<string>`

Fetches a session token from TrustPayWay. Automatically called before `processPayment()` or `checkPaymentStatus()` if not already set.

---

### `processPayment(params): Promise<object>`

Initiates a payment via mobile money.

**Params:**
- `amount` (string): Amount to charge
- `currency` (string): e.g. `'XAF'`
- `phone` (string): Receiver phone (with country code)
- `description` (string): Description for the transaction example Your APP Name
- `orderId` (string): Unique order identifier
- `notifUrl` (string): Callback URL for status updates

---

### `checkPaymentStatus({ transactionId }): Promise<object>`

Checks the status of a transaction.

**Params:**
- `transactionId` (string): The transaction ID returned by `processPayment`

---

## 📄 License

MIT

---

## 🔗 Links

- 📘 [TrustPayWay Official Site](https://mobilewallet.trustpayway.com/#documentation)
- 🧑‍💻 Author: [TrustPayWay](https://github.com/trustpayway)
- 🐛 Issues: [GitHub Issues](https://github.com/trustpayway/trustpayway-sdk/issues)
