Skip to content

Instantly share code, notes, and snippets.

@johnotu
Created December 7, 2018 17:52
Show Gist options
  • Select an option

  • Save johnotu/a055ee6b0dfece9dbbb26d255579d463 to your computer and use it in GitHub Desktop.

Select an option

Save johnotu/a055ee6b0dfece9dbbb26d255579d463 to your computer and use it in GitHub Desktop.
Payment for Messenger - Process Mobilemoney payments
/**
* Process mobile money payments using Mazzuma API
*
*/
'use strict';
const request = require('request');
const sendTextMessage = require('../actions/sendTextMsg');
const processMobilemoney = (senderID, orderObj, numberPrefix, network, route, pageObj) => {
// Check if number prefix matches a Ghanaian telco
if (!(['023', '024', '054', '055', '026', '056', '027', '057', '020', '050', '028'].includes(numberPrefix))) {
sendTextMessage({
recipientId: senderID,
messageText:
"Sorry, it seems you didn't enter a valid Ghanian mobile number. Please try again.",
page_token: pageObj.page_token
});
return;
}
// Set originating network according to prefix
if (numberPrefix === '024' || numberPrefix === '054' || numberPrefix === '055') {
network = 'mtn';
} else if (numberPrefix === '026' || numberPrefix === '056') {
network = 'airtel';
} else if (numberPrefix === '027' || numberPrefix === '057') {
network = 'tigo';
}
// Handle non-supported telco
if (!network) {
return sendTextMessage({
recipientId: senderID,
messageText:
'Apologies, we only support MTN, Airtel and Tigo mobile money payments for now. Sorry for the inconvenience.',
page_token: pageObj.page_token
});
}
// Set route option according to sending and receiving network
switch (network) {
case 'mtn':
route = 'rmta';
break;
case 'tigo':
route = 'rtta';
break;
case 'airtel':
route = 'rata';
break;
}
// Handle failure in setting route option
if (!route) {
return sendTextMessage({
recipientId: senderID,
messageText:
'We think an error has occurred. Kindly contact support.',
page_token: pageObj.page_token
});
}
// Notify user to watch out for USSD prompt
sendTextMessage({
recipientId: senderID,
messageText:
'You will be prompted to accept the transaction and enter your mobile money pin via USSD. Please note that you might incur additional charge from your telco as transaction cost.',
page_token: pageObj.page_token,
callback: () => {
sendTextMessage({
recipientId: senderID,
messageText:
'Please note that you might incur additional charge from your telco as transaction cost.',
page_token: pageObj.page_token
});
}
});
// Query Mazzuma mobile money API
request({
url: 'https://client.teamcyst.com/api_call.php',
headers: { 'content-type': 'application/json' },
method: 'POST',
json: {
'price': orderObj.orderTotal,
'network': network,
'recipient_number': pageObj.mm_number,
'sender': orderObj.orderOwnerPhone,
'option': route,
'apikey': process.env.MAZZUMA_TOKEN
}
}, (error, response, body) => {
if (error) { return console.log('Error processing mobile money ', error); }
if (body && body.code === 1) {
// Payment successful. Send appropriate notification to user
sendTextMessage({
recipientId: senderID,
messageText: `Payment has been received for order #${orderObj.orderNumber}. Your order is on the way!`,
page_token: pageObj.page_token
});
} else {
console.log('payment error: ', error);
// Payment failure. notify user accordingly
return sendTextMessage({
recipientId: senderID,
messageText: `Oops! An error occured and payment has NOT been received for order #${orderObj.orderNumber}. Please try again or contact support!`,
page_token: pageObj.page_token
});
}
});
};
module.exports = processMobilemoney;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment