Retour aux SDKsGitHub
N
Node.js SDK
v2.1.0SDK officiel pour Node.js et JavaScript/TypeScript
Installation
npm install @simiz/node-sdkOu avec yarn: yarn add @simiz/node-sdk
Configuration
import { Simiz } from '@simiz/node-sdk';
// Initialiser le client
const simiz = new Simiz(process.env.SIMIZ_SECRET_KEY);
// Ou avec des options
const simiz = new Simiz(process.env.SIMIZ_SECRET_KEY, {
apiVersion: '2024-01',
timeout: 30000,
maxRetries: 3,
});Utilisation
Créer une transaction
const transaction = await simiz.transactions.create({
amount: 5000,
currency: 'XAF',
payment_method: 'ORANGE_MONEY',
payer: {
phone: '237690000000',
name: 'John Doe',
email: 'john@example.com',
},
description: t('public.docs.sdks.nodejs.achatSurMaBoutique'),
reference: 'ORDER-123',
metadata: {
order_id: '123',
customer_id: 'cust_456',
},
callback_url: 'https://votre-site.com/webhooks/simiz',
return_url: 'https://votre-site.com/payment/success',
});
console.log(transaction.id); // tx_xxx
console.log(transaction.status); // PENDING
console.log(transaction.payment_url); // URL de paiementRécupérer une transaction
const transaction = await simiz.transactions.retrieve('tx_xxx');
if (transaction.status === 'COMPLETED') {
console.log('Paiement réussi!');
}Lister les transactions
// Avec pagination
const transactions = await simiz.transactions.list({
limit: 20,
status: 'COMPLETED',
created_after: '2024-01-01',
});
for (const tx of transactions.data) {
console.log(tx.id, tx.amount);
}
// Pagination automatique
for await (const tx of simiz.transactions.list({ limit: 100 })) {
console.log(tx.id);
}Créer un remboursement
const refund = await simiz.refunds.create({
transaction_id: 'tx_xxx',
amount: 2500, // remboursement partiel
reason: 'Article retourné',
});
console.log(refund.status); // PENDINGWebhooks
import express from 'express';
import { Simiz } from '@simiz/node-sdk';
const app = express();
const simiz = new Simiz(process.env.SIMIZ_SECRET_KEY);
app.post('/webhooks/simiz', express.raw({ type: 'application/json' }), (req, res) => {
const signature = req.headers['x-simiz-signature'] as string;
try {
// Vérifier la signature
const event = simiz.webhooks.constructEvent(
req.body,
signature,
process.env.SIMIZ_WEBHOOK_SECRET
);
// Traiter l'événement
switch (event.type) {
case 'transaction.completed':
const transaction = event.data;
console.log('Paiement reçu:', transaction.id);
// Mettre à jour votre base de données
break;
case 'transaction.failed':
console.log('Paiement échoué:', event.data.id);
break;
case 'refund.completed':
console.log('Remboursement effectué:', event.data.id);
break;
}
res.status(200).json({ received: true });
} catch (err) {
console.error('Webhook error:', err.message);
res.status(400).send(`Webhook Error: ${err.message}`);
}
});Gestion des erreurs
import { Simiz, SimizError, APIError, AuthenticationError } from '@simiz/node-sdk';
try {
const transaction = await simiz.transactions.create({...});
} catch (error) {
if (error instanceof AuthenticationError) {
// Clé API invalide
console.error('Vérifiez votre clé API');
} else if (error instanceof APIError) {
// Erreur API (validation, etc.)
console.error('API Error:', error.message);
console.error('Param:', error.param);
} else if (error instanceof SimizError) {
// Erreur réseau ou autre
console.error('Error:', error.message);
}
}TypeScript
Le SDK est écrit en TypeScript et inclut toutes les définitions de types.
import { Simiz, Transaction, TransactionCreateParams } from '@simiz/node-sdk';
const params: TransactionCreateParams = {
amount: 5000,
currency: 'XAF',
payment_method: 'ORANGE_MONEY',
payer: { phone: '237690000000' },
};
const transaction: Transaction = await simiz.transactions.create(params);