-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDohoneSDK.js
133 lines (115 loc) · 4.07 KB
/
DohoneSDK.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var AbstractDohoneSDK = require('./AbstractDohoneSDK')
var md5 = require('md5')
function DohoneSDK(merchantKey, dohoneAppName, hashCode, notifyUrl) {
AbstractDohoneSDK.call(this, hashCode, notifyUrl)
this.merchantKey = merchantKey
this.dohoneAppName = dohoneAppName
this.BASE_URL = 'https://www.my-dohone.com/dohone/pay'
this.OPERATORS = {
'DOHONE_MOMO': 1, // MTN Mobile Money
'DOHONE_OM': 2, // Orange Money
'DOHONE_EU': 3, // Express Union Mobile Money
'DOHONE_TRANSFER': 10 // Dohone Account Transfer
}
}
DohoneSDK.prototype = Object.create(AbstractDohoneSDK.prototype)
DohoneSDK.prototype.constructor = DohoneSDK
DohoneSDK.prototype.parseDohoneResponse = function (res) {
var words = res.split(' ');
var cmd = words.length > 1 ? words[1] : null; // Second word
var message = res.substr(res.indexOf(':') + 2);
var refIndex = message.indexOf('REF');
var needCFRMSMS = message.indexOf('SMS') >= 0;
var REF = refIndex >= 0 ? message.substr(refIndex + 5) : null;
var response = Object.getPrototypeOf(DohoneSDK.prototype).parseDohoneResponse(res)
response.cmd = cmd
response.message = message
response.needCFRMSMS = needCFRMSMS
response.REF = REF
return response
}
DohoneSDK.prototype.quote = function (transaction, params, callback) {
params = {
cmd: 'cotation',
rH: this.merchantKey,
rMo: this.getOperatorCodeFromSlug(transaction.operator),
rMt: transaction.amount,
rDvs: transaction.currency || 'XAF',
levelFeeds: params.mode || 0
}
this.request(params, callback)
}
DohoneSDK.prototype.start = function (transaction, params, callback) {
var notifyUrl = this.notifyUrl
if (transaction.notifyUrl)
notifyUrl = transaction.notifyUrl
params = {
cmd: 'start',
rN: transaction.customerName,
rT: transaction.customerPhoneNumber,
rE: transaction.customerEmail,
rI: transaction.ref,
rH: this.merchantKey,
rMo: this.getOperatorCodeFromSlug(transaction.operator || 'DOHONE_OM'),
rOTP: params.OTP,
rMt: transaction.amount,
rDvs: transaction.currency || 'XAF',
source: this.dohoneAppName,
notifyPage: notifyUrl,
motif: transaction.reason
}
this.request(params, callback)
}
DohoneSDK.prototype.confirmSMS = function (transaction, params, callback, tries) {
var _params = {
cmd: 'cfrmsms',
rCS: params.code,
rT: transaction.customerPhoneNumber
}
var _this = this
this.request(_params, function (err, dohoneRes) {
if (err && tries)
_this.confirmSMS(transaction, params, callback, tries - 1)
else
callback(err, dohoneRes)
})
}
DohoneSDK.prototype.verify = function (transaction, callback) {
var params = {
cmd: 'verify',
rI: transaction.ref,
rMt: transaction.amount,
rDvs: transaction.currency || 'XAF',
idReqDoh: transaction.dohoneRef
}
this.request(params, callback)
}
DohoneSDK.prototype.mapNotificationData = function (data) {
var map = {
rI: 'ref',
rMt: 'amount',
rDvs: 'currency',
mode: 'operator',
motif: 'reason',
idReqDoh: 'dohoneRef',
rH: 'merchantKey',
hash: 'hash'
}
for(var key in map)
if (map.hasOwnProperty(key) && data.hasOwnProperty(key))
data[map[key]] = data[key]
return data;
}
DohoneSDK.prototype.checkHash = function (notificationData) {
if (notificationData.merchantKey !== this.merchantKey)
return false
var dohoneRef = notificationData.dohoneRef
var ref = notificationData.ref
var amount = notificationData.amount
var hash1 = md5(dohoneRef + ref + amount + this.hashCode)
var hash2 = md5(dohoneRef + ref + parseInt(amount) + this.hashCode)
return notificationData.hash === hash1 || notificationData.hash === hash2
}
module.exports = function (merchantKey, dohoneAppName, hashCode, notifyUrl) {
return new DohoneSDK(merchantKey, dohoneAppName, hashCode, notifyUrl)
}