Skip to content

Instantly share code, notes, and snippets.

@jsbeaudry
Created July 8, 2023 17:24
Show Gist options
  • Save jsbeaudry/31fc42890cffe00f4f96257bea3d573b to your computer and use it in GitHub Desktop.
Save jsbeaudry/31fc42890cffe00f4f96257bea3d573b to your computer and use it in GitHub Desktop.
Digicel MonCash - MonCash is a mobile wallet that facilitates reliable, safe and convenient financial transactions to reduce the distance between people regardless of their location in Haiti. While providing its services to its customer base of over 1.5 million people, MonCash maintains its goal of expanding its range of available services.
const express = require('express');
const { createPayment, getCaptureById } = require('./moncash-api');
const app = express();
// API route to create payment
app.get('/payment/create', createPayment); // Parameters: amount, orderId
// API route to get capture by ID
app.get('/capture/getById', getCaptureById); // Parameters: orderId
// Start the server
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
const Moncash = require("moncash");
const moncash = new Moncash({
mode: process.env.MONCASH_CLIENT_MODE, // 'sandbox' | 'live'
clientId: process.env.MONCASH_CLIENT_ID,
clientSecret: process.env.MONCASH_CLIENT_SECRET,
});
async function createPayment(req, res) {
try {
moncash.payment.create(
{
amount: req.query.amount, // Ex: 50
orderId: req.query.orderId, // Must be unique
},
(err, payment) => {
if (err) {
console.log(err.type); // see Error handler section
return false;
}
const paymentURI = moncash.payment.redirectUri(payment);
console.log(payment, paymentURI);
res.status(200).json({
payment,
paymentURI,
});
}
);
} catch (error) {
res.status(200).json({
error: true,
errMessage: error,
});
}
}
async function getCaptureById(req, res) {
try {
moncash.capture.getByOrderId(req.query.orderId, (err, capture) => {
if (capture === null) {
res.status(200).json({
error: true,
errMessage: err,
});
} else {
// console.log(capture)
res.status(200).json({
error: false,
data: capture,
});
}
});
} catch (error) {
res.status(200).json({
error: true,
errMessage: error,
});
}
}
module.exports = { createPayment, getCaptureById };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment