Skip to content

Commit

Permalink
ENC_PARAM will be moved to extra_payload in payment notificaton
Browse files Browse the repository at this point in the history
  • Loading branch information
kn9ts committed Sep 11, 2016
1 parent 06121f2 commit d1ec16b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
11 changes: 10 additions & 1 deletion server/controllers/PaymentSuccess.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const request = require('request');
const statusCodes = require('../config/statusCodes');

class PaymentSuccess {
constructor() {
Expand Down Expand Up @@ -28,9 +29,17 @@ class PaymentSuccess {

if ('enc_params' in response) {
// decrypted encrypted extra parameters provided in ENC_PARAMS
response.enc_params = JSON.parse(new Buffer(response.enc_params, 'base64').toString());
response.extra_payload = JSON.parse(new Buffer(response.enc_params, 'base64').toString());
delete response.enc_params;
}

const extractCode = statusCodes
.find(stc => stc.return_code === parseInt(response.return_code, 10));

Object.assign(response, extractCode);
// console.log('PAYMENT NOTIFICATON from SAG');
// console.log({ response });

const requestParams = {
method: 'POST',
uri: endpoint,
Expand Down
2 changes: 0 additions & 2 deletions server/utils/GenEncryptedPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ module.exports = class GenEncryptedPassword {
const hash = crypto.createHash('sha256');
this.hashedPassword = hash.update(concatenatedString).digest('hex'); // or 'binary'
this.hashedPassword = new Buffer(this.hashedPassword).toString('base64');
// this.hashedPassword = this.hashedPassword.toUpperCase();
// console.log('hashedPassword ==> ', this.hashedPassword);
}
};
2 changes: 0 additions & 2 deletions server/utils/ParseResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ module.exports = class ParseResponse {

// Get the children tagName and its values
$(this.bodyTagName).children().each((i, el) => {
// if (el.children.length > 1) return;
if (el.children.length === 1) {
// console.log(el.name, el.children[0].data);
let value = el.children[0].data.replace(/\s{2,}/gi, ' ');
value = value.replace(/\n/gi, '').trim();
this.json[el.name] = value;
Expand Down
1 change: 1 addition & 0 deletions server/utils/ucFirst.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
// ucFirst (typeof String):
// returns String with first character uppercased

module.exports = (string) => {
const word = string;
let ucFirstWord = '';
Expand Down
25 changes: 18 additions & 7 deletions test/controllers/PaymentSuccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ describe('paymentSuccess', () => {
res.sendStatus = sinon.stub();
const next = sinon.stub();

const response = { response: {} };
const response = {};
for (const x of Object.keys(req.body)) {
const prop = x.toLowerCase().replace(/\-/g, '');
response.response[prop] = req.body[x];
response[prop] = req.body[x];
}

let error = false;
Expand All @@ -47,16 +47,27 @@ describe('paymentSuccess', () => {

const spyCall = paymentSuccess.request.getCall(0);
const args = spyCall.args[0];

// convert the args.body.response.enc_params to base64
const argsResponseBody = JSON.parse(args.body);
const encParamsObject = JSON.stringify(argsResponseBody.response.enc_params)
argsResponseBody.response.enc_params = new Buffer(encParamsObject).toString('base64');

assert.isTrue(res.sendStatus.calledWithExactly(200));
assert.isTrue(paymentSuccess.request.called);
assert.isFalse(next.called);
expect(response.response).to.deep.equal(argsResponseBody.response);
assert.sameMembers(Object.keys(argsResponseBody.response), [
'amount',
'description',
'extra_payload',
'merchant_transaction_id',
'message',
'mpesa_trx_date',
'mpesa_trx_id',
'msisdn',
'password',
'return_code',
'status_code',
'trx_id',
'trx_status',
'username'
]);
});

it('If MERCHANT_ENDPOINT is not provided, next is passed an error', () => {
Expand Down

0 comments on commit d1ec16b

Please sign in to comment.