Skip to content

Instantly share code, notes, and snippets.

@DavidInnocent
Last active April 22, 2020 07:22
Show Gist options
  • Save DavidInnocent/38bc16f0cec587d8cfefc67aa8a22f13 to your computer and use it in GitHub Desktop.
Save DavidInnocent/38bc16f0cec587d8cfefc67aa8a22f13 to your computer and use it in GitHub Desktop.
const functions = require('firebase-functions')
const crypto = require('crypto')
const fs = require('fs');
const superagent = require('superagent')
const tokenURL = 'https://uat.jengahq.io/identity/v2/token'
const accountBalanceURL = 'https://uat.jengahq.io/account/v2/accounts/balances/KE/0744444444444'
const billValidationURL = 'https://uat.jengahq.io/transaction/v2/bills/validation'
const sendMoneyURL = 'https://uat.jengahq.io/transaction/v2/remittance'
const accountId = '0744444444444'
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.AccountBalance = functions.https.onRequest((request, response) => {
let returned = EncryptData()
let accountData = {
'username': '6266666666',
'password': 'cZaxYq3fLSQCOk8uXccccccccokq08k6b'
}
superagent
.post(tokenURL)
.set({
'Authorization': 'Basic SGxVbHRVRE9tQWhSY0cccccccccccccccccOVGVnUWg6c0cxQnJFN2JHTFJvREZLbw==',
'Content-Type': 'application/x-www-form-urlencoded'
})
.send(accountData)
.then((token_data) => {
GetAccountBalance(token_data)
return true;
})
.catch(erro => {
console.log(erro)
response.send("Hellos from " + erro.toString())
}
)
response.send('Done done')
})
function EncryptData() {
let privateKey = fs.readFileSync('./privatekey.pem', 'utf8').toString('ascii')
let bufferToEncrypt = Buffer.from('KE0744444444444')
let encrypted = crypto.privateEncrypt(privateKey, bufferToEncrypt)
return encrypted.toString('base64');
}
function GetAccountBalance(token_data) {
let bearer = 'Bearer ' + token_data.body.access_token;
let signature = EncryptData();
superagent
.get(accountBalanceURL)
.set('Authorization', bearer)
.set('signature', signature)
.then(data => {
console.log('data returned \n' + JSON.stringify(data))
return true
})
.catch(error => {
// console.log(error)
return false;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment