SDK Node.js
npm install mypaie-node
Installation
npm install mypaie-node
Initialisation
const MyPaie = require('mypaie-node');
const mypaie = new MyPaie('pk_test_votre_cle');
Checkout hébergé
// Express.js
app.post('/create-checkout', async (req, res) => {
const session = await mypaie.checkout.create({
amount: 5000,
currency: 'XOF',
description: 'Commande #123',
success_url: 'https://votresite.com/success',
cancel_url: 'https://votresite.com/cancel',
customer_email: 'client@email.com'
});
res.redirect(session.checkout_url);
});
Orange Money
const payment = await mypaie.payments.orangeMoney({
amount: 5000,
currency: 'XOF',
phone: '+22370123456',
description: 'Achat produit'
});
res.redirect(payment.payment_url);
Carte bancaire
const payment = await mypaie.payments.card({
amount: 10000,
currency: 'XOF',
customer_email: 'client@email.com',
customer_name: 'Amadou Diallo',
description: 'Abonnement Premium'
});
res.redirect(payment.payment_url);
Webhooks (Express)
const { Webhook } = require('mypaie-node');
app.post('/webhooks/mypaie',
express.raw({type: 'application/json'}),
(req, res) => {
const signature = req.headers['x-mypaie-signature'];
try {
const event = Webhook.constructEvent(
req.body.toString(),
signature,
'votre_webhook_secret'
);
switch (event.event) {
case 'payment.success':
console.log('Paiement réussi:', event.data.reference);
break;
case 'payment.failed':
console.log('Paiement échoué');
break;
}
res.status(200).send('OK');
} catch (err) {
res.status(401).send('Signature invalide');
}
}
);
Gestion des erreurs
const { MyPaieError } = require('mypaie-node');
try {
await mypaie.payments.orangeMoney({...});
} catch (err) {
if (err instanceof MyPaieError) {
console.log(err.message); // Message d'erreur
console.log(err.errorCode); // VALIDATION_ERROR, etc.
console.log(err.httpCode); // 400, 401, etc.
}
}