spaark-payapi-sdk 1.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Spaark
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,354 @@
1
+ # spaark-payapi-sdk
2
+
3
+ TypeScript SDK for Pawapay Mobile Money API (V2). Simplifies integration with Mobile Money operators in Africa.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/spaark-payapi-sdk.svg)](https://www.npmjs.com/package/spaark-payapi-sdk)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ # npm
12
+ npm install spaark-payapi-sdk
13
+
14
+ # pnpm
15
+ pnpm add spaark-payapi-sdk
16
+
17
+ # yarn
18
+ yarn add spaark-payapi-sdk
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ```typescript
24
+ import { SpaarkPayApiSdk } from 'spaark-payapi-sdk';
25
+
26
+ const sdk = new SpaarkPayApiSdk({
27
+ apiKey: 'pk_sandbox_xxxxxxxxxxxx',
28
+ environment: 'sandbox', // or 'production'
29
+ });
30
+
31
+ // Initiate a deposit (collect payment)
32
+ const deposit = await sdk.transactions.initiateDeposit({
33
+ amount: 5000,
34
+ currency: 'XAF',
35
+ provider: 'MTN_MOMO_CMR',
36
+ phoneNumber: '237670000000',
37
+ transactionId: sdk.utils.generateTransactionId(),
38
+ customerMessage: 'Payment for order',
39
+ });
40
+
41
+ console.log(deposit.depositId, deposit.status);
42
+ ```
43
+
44
+ ## Features
45
+
46
+ - **Transactions**: Deposits, Payouts, Refunds, Payment Page
47
+ - **Toolkit**: Predict Provider, Active Configuration, Provider Availability
48
+ - **Finances**: Wallet Balances, Statement Generation
49
+ - **Webhooks**: Signature verification, Event parsing
50
+ - **React Component**: Test dashboard with demo mode
51
+ - **Full TypeScript**: Complete type definitions
52
+
53
+ ## Supported Providers
54
+
55
+ | Provider | Country | Currency |
56
+ |----------|---------|----------|
57
+ | `MTN_MOMO_CMR` | Cameroon | XAF |
58
+ | `ORANGE_CMR` | Cameroon | XAF |
59
+ | `MTN_MOMO_COG` | Congo | XAF |
60
+ | `AIRTEL_COG` | Congo | XAF |
61
+ | `MTN_MOMO_GAB` | Gabon | XAF |
62
+ | `AIRTEL_GAB` | Gabon | XAF |
63
+
64
+ ## API Reference
65
+
66
+ ### Initialization
67
+
68
+ ```typescript
69
+ import { SpaarkPayApiSdk } from 'spaark-payapi-sdk';
70
+
71
+ const sdk = new SpaarkPayApiSdk({
72
+ apiKey: process.env.PAWAPAY_API_KEY,
73
+ environment: 'sandbox', // 'sandbox' | 'production'
74
+ timeout: 30000, // Optional: request timeout in ms
75
+ retries: 3, // Optional: retry attempts
76
+ logLevel: 'info', // Optional: 'debug' | 'info' | 'warn' | 'error' | 'none'
77
+ });
78
+ ```
79
+
80
+ ### Transactions
81
+
82
+ #### Initiate Deposit (Collect Payment)
83
+
84
+ ```typescript
85
+ const deposit = await sdk.transactions.initiateDeposit({
86
+ amount: 5000,
87
+ currency: 'XAF',
88
+ provider: 'MTN_MOMO_CMR',
89
+ phoneNumber: '237670000000',
90
+ transactionId: sdk.utils.generateTransactionId(),
91
+ customerMessage: 'Payment description', // 4-22 chars
92
+ clientReferenceId: 'order-123', // Optional
93
+ metadata: [{ orderId: 'ORD-123' }], // Optional
94
+ });
95
+
96
+ // Response
97
+ {
98
+ depositId: 'uuid',
99
+ status: 'ACCEPTED',
100
+ created: '2025-01-31T12:00:00Z',
101
+ nextStep: 'FINAL_STATUS' // or 'REDIRECT' | 'PRE_AUTHORISATION'
102
+ }
103
+ ```
104
+
105
+ #### Initiate Payout (Send Money)
106
+
107
+ ```typescript
108
+ const payout = await sdk.transactions.initiatePayout({
109
+ amount: 5000,
110
+ currency: 'XAF',
111
+ provider: 'MTN_MOMO_CMR',
112
+ phoneNumber: '237670000000',
113
+ transactionId: sdk.utils.generateTransactionId(),
114
+ });
115
+
116
+ // Response
117
+ {
118
+ payoutId: 'uuid',
119
+ status: 'ACCEPTED',
120
+ created: '2025-01-31T12:00:00Z'
121
+ }
122
+ ```
123
+
124
+ #### Check Status
125
+
126
+ ```typescript
127
+ // Check deposit status
128
+ const status = await sdk.transactions.checkDepositStatus('deposit-uuid');
129
+
130
+ // Check payout status
131
+ const status = await sdk.transactions.checkPayoutStatus('payout-uuid');
132
+
133
+ // Auto-detect (tries deposit first, then payout)
134
+ const status = await sdk.transactions.checkStatus('transaction-uuid');
135
+ ```
136
+
137
+ #### Poll Until Complete
138
+
139
+ ```typescript
140
+ const result = await sdk.transactions.pollUntilComplete('transaction-uuid', {
141
+ interval: 5000, // Poll every 5 seconds
142
+ maxAttempts: 12, // Max 12 attempts (1 minute)
143
+ onStatusChange: (status) => console.log('Status:', status),
144
+ });
145
+ ```
146
+
147
+ #### Refund
148
+
149
+ ```typescript
150
+ const refund = await sdk.transactions.refund({
151
+ depositId: 'original-deposit-uuid',
152
+ amount: 5000,
153
+ transactionId: sdk.utils.generateTransactionId(),
154
+ });
155
+ ```
156
+
157
+ #### Payment Page (Hosted Checkout)
158
+
159
+ ```typescript
160
+ const page = await sdk.transactions.createPaymentPage({
161
+ depositId: deposit.depositId,
162
+ returnUrl: 'https://yoursite.com/payment/complete',
163
+ phoneNumber: '237670000000', // Optional
164
+ amountDetails: { amount: 5000, currency: 'XAF' }, // Optional
165
+ language: 'FR', // Optional
166
+ country: 'CMR', // Optional
167
+ reason: 'Ticket purchase', // Optional
168
+ });
169
+
170
+ // Redirect customer to:
171
+ console.log(page.redirectUrl);
172
+ ```
173
+
174
+ #### Resend Callbacks & Cancel
175
+
176
+ ```typescript
177
+ await sdk.transactions.resendDepositCallback('deposit-uuid');
178
+ await sdk.transactions.resendPayoutCallback('payout-uuid');
179
+ await sdk.transactions.cancelEnqueuedPayout('payout-uuid');
180
+ ```
181
+
182
+ ### Toolkit
183
+
184
+ #### Predict Provider
185
+
186
+ ```typescript
187
+ const prediction = await sdk.utils.predictProvider('+237 670 000 000');
188
+ // { country: 'CMR', provider: 'MTN_MOMO_CMR', phoneNumber: '237670000000' }
189
+ ```
190
+
191
+ #### Provider Availability
192
+
193
+ ```typescript
194
+ const availability = await sdk.utils.getProviderAvailability({
195
+ country: 'CMR', // Optional
196
+ operationType: 'DEPOSIT', // Optional
197
+ });
198
+ ```
199
+
200
+ #### Active Configuration
201
+
202
+ ```typescript
203
+ const config = await sdk.utils.getActiveConfiguration();
204
+ ```
205
+
206
+ #### Check MMO Availability
207
+
208
+ ```typescript
209
+ const status = await sdk.utils.checkMMOAvailability('MTN_MOMO_CMR');
210
+ // { correspondent: 'MTN_MOMO_CMR', available: true, degraded: false }
211
+ ```
212
+
213
+ ### Finances
214
+
215
+ #### Wallet Balances
216
+
217
+ ```typescript
218
+ const balances = await sdk.finances.getWalletBalances();
219
+ // [{ country: 'CMR', currency: 'XAF', balance: '1250000.00' }]
220
+ ```
221
+
222
+ #### Generate Statement
223
+
224
+ ```typescript
225
+ const statement = await sdk.finances.generateStatement({
226
+ wallet: { country: 'CMR', currency: 'XAF' },
227
+ callbackUrl: 'https://yoursite.com/webhooks/statement',
228
+ startDate: '2025-01-01T00:00:00Z',
229
+ endDate: '2025-01-31T23:59:59Z',
230
+ compressed: true,
231
+ });
232
+
233
+ const result = await sdk.finances.pollStatementUntilComplete(statement.statementId);
234
+ console.log(result.downloadUrl);
235
+ ```
236
+
237
+ ### Webhooks
238
+
239
+ ```typescript
240
+ // Set secret
241
+ sdk.setWebhookSecret(process.env.PAWAPAY_WEBHOOK_SECRET);
242
+
243
+ // Verify signature
244
+ const isValid = sdk.webhooks.verifySignature(body, signature);
245
+
246
+ // Parse event
247
+ const event = sdk.webhooks.parseEvent(body);
248
+
249
+ switch (event.eventType) {
250
+ case 'deposit.completed':
251
+ console.log('Deposit completed:', event.data.depositId);
252
+ break;
253
+ case 'deposit.failed':
254
+ console.log('Deposit failed:', event.data.failureReason);
255
+ break;
256
+ case 'payout.completed':
257
+ console.log('Payout completed:', event.data.payoutId);
258
+ break;
259
+ }
260
+ ```
261
+
262
+ ### Utilities
263
+
264
+ ```typescript
265
+ // Generate UUID v4
266
+ const txId = sdk.utils.generateTransactionId();
267
+
268
+ // Validate transaction ID
269
+ const isValid = sdk.utils.validateTransactionId(txId);
270
+
271
+ // Format phone number
272
+ const phone = sdk.utils.formatPhoneNumber('670000000', 'CMR');
273
+
274
+ // Validate phone for provider
275
+ const isValid = sdk.utils.validatePhoneNumber('237670000000', 'MTN_MOMO_CMR');
276
+
277
+ // Get provider info
278
+ const info = sdk.utils.getCorrespondentInfo('MTN_MOMO_CMR');
279
+
280
+ // Detect provider from phone
281
+ const provider = sdk.utils.detectCorrespondent('237670000000');
282
+
283
+ // Get transaction limits
284
+ const limits = await sdk.utils.getTransactionLimits('MTN_MOMO_CMR');
285
+ ```
286
+
287
+ ## React Dashboard Component
288
+
289
+ ```tsx
290
+ 'use client';
291
+
292
+ import { PawapayTestDashboard } from 'spaark-payapi-sdk';
293
+
294
+ export default function TestPage() {
295
+ return (
296
+ <PawapayTestDashboard
297
+ environment="sandbox"
298
+ apiBasePath="/api/pawapay"
299
+ demoMode={false}
300
+ onDepositComplete={(res) => console.log('Deposit:', res)}
301
+ onPayoutComplete={(res) => console.log('Payout:', res)}
302
+ onError={(err) => console.error(err)}
303
+ />
304
+ );
305
+ }
306
+ ```
307
+
308
+ Use `demoMode={true}` to test without API key.
309
+
310
+ ## TypeScript Types
311
+
312
+ ```typescript
313
+ import type {
314
+ SpaarkPayApiSdkConfig,
315
+ DepositRequest,
316
+ DepositResponse,
317
+ PayoutRequest,
318
+ PayoutResponse,
319
+ TransactionStatus,
320
+ TransactionStatusResponse,
321
+ Correspondent,
322
+ Currency,
323
+ // ... and more
324
+ } from 'spaark-payapi-sdk';
325
+ ```
326
+
327
+ ## Error Handling
328
+
329
+ ```typescript
330
+ import { PawapayError } from 'spaark-payapi-sdk';
331
+
332
+ try {
333
+ await sdk.transactions.initiateDeposit({ ... });
334
+ } catch (error) {
335
+ if (error instanceof PawapayError) {
336
+ console.log(error.code); // Error code
337
+ console.log(error.message); // Human-readable message
338
+ console.log(error.retryable); // Whether to retry
339
+ console.log(error.statusCode); // HTTP status
340
+ }
341
+ }
342
+ ```
343
+
344
+ ## Environment Variables
345
+
346
+ ```bash
347
+ PAWAPAY_API_KEY=pk_sandbox_xxxxxxxxxxxx
348
+ PAWAPAY_ENVIRONMENT=sandbox
349
+ PAWAPAY_WEBHOOK_SECRET=whsec_xxxxxxxxxxxx
350
+ ```
351
+
352
+ ## License
353
+
354
+ MIT