tsapay-sdk 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +106 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# TsaPay Node.js SDK
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
The official Node.js / TypeScript SDK for **TsaPay**, the universal Mobile Money aggregator for the CEMAC zone (MTN MoMo, Orange Money).
|
|
7
|
+
|
|
8
|
+
Integrate Mobile Money payments into your e-commerce platform, mobile app backend, or SaaS in just a few lines of code.
|
|
9
|
+
|
|
10
|
+
## 📦 Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install tsapay-sdk
|
|
14
|
+
# or
|
|
15
|
+
yarn add tsapay-sdk
|
|
16
|
+
# or
|
|
17
|
+
pnpm add tsapay-sdk
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 🚀 Quick Start
|
|
21
|
+
|
|
22
|
+
### 1. Initialization
|
|
23
|
+
|
|
24
|
+
First, get your API Key from your TsaPay Merchant Dashboard.
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { TinguilinClient } from 'tsapay-sdk';
|
|
28
|
+
|
|
29
|
+
const tsapay = new TinguilinClient({
|
|
30
|
+
apiKey: 'sk_test_YOUR_API_KEY_HERE',
|
|
31
|
+
// baseUrl is optional, defaults to the production gateway
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2. Create a Payment
|
|
36
|
+
|
|
37
|
+
Initiate a USSD push directly to the customer's phone.
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
try {
|
|
41
|
+
const response = await tsapay.createPayment({
|
|
42
|
+
amount: 1500,
|
|
43
|
+
currency: 'XAF', // Optional, defaults to XAF
|
|
44
|
+
provider: 'mtn_momo', // or 'orange_money'
|
|
45
|
+
phoneNumber: '+237670000000', // Customer's phone number
|
|
46
|
+
description: 'Order #12345',
|
|
47
|
+
reference: 'REF-12345', // Your internal order reference
|
|
48
|
+
callbackUrl: 'https://your-site.com/api/webhooks/tsapay', // Where we should send the webhook
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
console.log('Payment initiated successfully:', response.payment.id);
|
|
52
|
+
console.log('Status:', response.payment.status); // 'pending'
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.error('Payment failed:', error.message);
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 3. Check Payment Status
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
const response = await tsapay.getPayment('pay_123456789');
|
|
62
|
+
console.log('Current status:', response.payment.status);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 4. Verify Webhooks
|
|
66
|
+
|
|
67
|
+
When a payment succeeds or fails, TsaPay will send a POST request to your `callbackUrl`. To ensure this request actually came from TsaPay, you must verify the signature using your Webhook Secret.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import express from 'express';
|
|
71
|
+
|
|
72
|
+
const app = express();
|
|
73
|
+
const WEBHOOK_SECRET = 'whsec_YOUR_WEBHOOK_SECRET';
|
|
74
|
+
|
|
75
|
+
app.post('/api/webhooks/tsapay', express.raw({ type: 'application/json' }), (req, res) => {
|
|
76
|
+
const signature = req.headers['x-tsapay-signature'] as string;
|
|
77
|
+
const payload = req.body.toString();
|
|
78
|
+
|
|
79
|
+
// Verify the signature
|
|
80
|
+
const isValid = tsapay.verifyWebhookSignature(payload, signature, WEBHOOK_SECRET);
|
|
81
|
+
|
|
82
|
+
if (!isValid) {
|
|
83
|
+
return res.status(400).send('Invalid signature');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const event = JSON.parse(payload);
|
|
87
|
+
|
|
88
|
+
if (event.event_type === 'payment.success') {
|
|
89
|
+
console.log('Payment successful for:', event.data.reference);
|
|
90
|
+
// TODO: Fulfill the order in your database
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
res.send('Webhook received');
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## 📚 Features
|
|
98
|
+
|
|
99
|
+
- **Zero dependencies** (uses native `fetch`)
|
|
100
|
+
- **Full TypeScript support** with strict types
|
|
101
|
+
- **Built-in idempotency** to prevent duplicate charges
|
|
102
|
+
- **Secure webhook verification** helper included
|
|
103
|
+
|
|
104
|
+
## 📄 License
|
|
105
|
+
|
|
106
|
+
MIT
|