· 6 years ago · Jun 27, 2019, 09:32 PM
1/**
2 * Created by macbookpro on 27/07/2017.
3 */
4/**
5 * Created by macbookpro on 19/07/2017.
6 */
7
8let Lang = require('../lang/lang.js');
9
10const tables = singleton.seq.tables;
11
12
13module.exports = class OrangeMoneyController {
14
15
16 static async sendRequest({req, res, currentTransaction, sellerCustomer}) {
17 let pack = require('locutus/php/misc/pack');
18 let date = require('locutus/php/datetime/date');
19 let md5 = require('locutus/php/strings/md5');
20 let http_build_query = require('locutus/php/url/http_build_query');
21 let base64_encode = require('locutus/php/url/base64_encode');
22 let time = require('locutus/php/datetime/time');
23 let strtoupper = require('locutus/php/strings/strtoupper');
24
25 let hash_hmac = (algo, message, key) => {
26 let crypto = require('crypto');
27
28 let hmac = crypto.createHmac(algo, key);
29 return hmac.update(new Buffer(message, 'utf-8')).digest("hex")
30 };
31
32 let strtolower = require('locutus/php/strings/strtolower');
33
34 let secretKey = 'C820171244834EECED32EF23086AA4292821015162BAB08A0E3DE47E213338DE';
35 let secretKey2 = 'A5272DF494854B9732F1E342965377019CFE915EA738279870C154F18AE8E795';
36 let binKey = new Buffer(secretKey, "hex");
37 let binKey2 = new Buffer(secretKey2, "hex");
38 let dateH = date('c');
39 let dateH2 = date('c');
40 let algo = 'SHA512';
41 let algo2 = 'SHA512';
42 let identifier = md5('2729570356');
43 let identifier2 = md5('2842317971');
44 let command = "Paiement Payexpresse " + currentTransaction.token;
45 let command2 = "Paiement Payexpresse " + currentTransaction.token;
46 let site = md5('1111117583');
47 let site2 = md5('1257226205');
48 let amount = currentTransaction.sentAmount;
49 let amount2 = currentTransaction.sentAmount;
50 let refCommand = base64_encode(http_build_query({
51 d: time(),
52 token: currentTransaction.token
53 }));
54 let refCommand2 = base64_encode(http_build_query({
55 d: time(),
56 token: currentTransaction.token
57 }));
58
59 let message = `S2M_COMMANDE=${command}&S2M_DATEH=${dateH}&S2M_HTYPE=${algo2}&S2M_IDENTIFIANT=${identifier}&S2M_REF_COMMANDE=${refCommand}&S2M_SITE=${site}&S2M_TOTAL=${amount}`;
60 let message2 = `S2M_COMMANDE=${command2}&S2M_DATEH=${dateH2}&S2M_HTYPE=${algo2}&S2M_IDENTIFIANT=${identifier2}&S2M_REF_COMMANDE=${refCommand2}&S2M_SITE=${site2}&S2M_TOTAL=${amount2}`;
61
62 let hmac = strtoupper(hash_hmac(strtolower(algo), message, binKey));
63 let hmac2 = strtoupper(hash_hmac(strtolower(algo2), message2, binKey2));
64
65
66 try {
67 let firstResponse = await singleton.rp({
68 url: 'https://api.paiementorangemoney.com/',
69 jar: true,
70 method: 'POST',
71 rejectUnauthorized: true,
72 form: {
73 S2M_IDENTIFIANT: identifier2,
74 S2M_SITE: site2,
75 S2M_TOTAL: amount2,
76 S2M_REF_COMMANDE: refCommand2,
77 S2M_COMMANDE: command2,
78 S2M_DATEH: dateH2,
79 S2M_HTYPE: algo2,
80 S2M_HMAC: hmac2
81 },
82 simple: true,
83 resolveWithFullResponse: true
84 });
85
86 singleton.saveLog(currentTransaction.token, firstResponse.body + ' | ' + firstResponse.status, 'https://api.paiementorangemoney.com/');
87
88 if (firstResponse.body.indexOf("https://payexpresse.com/api/payment/om/cancel") > -1) {
89
90
91 if (firstResponse.body.indexOf("https://payexpresse.com/api/payment/om/success") > -1) {
92
93 let now = (new Date()).toISOString().replace('T', ' ').substr(0, 19);
94
95 await currentTransaction.update({
96 state: 'success',
97 date_checkout: now,
98 payment_method: 'Orange Money',
99 client_phone: req.query.phone,
100 });
101
102 let newIpn = tables.Ipn.build({
103 url: currentTransaction.ipn_url,
104 data: JSON.stringify({
105 type_event: 'sale_complete',
106 custom_field: singleton.base64Encode(currentTransaction.custom_field),
107 ref_command: currentTransaction.ref_command,
108 item_name: currentTransaction.item_name,
109 item_price: currentTransaction.item_price,
110 currency: currentTransaction.currency,
111 command_name: currentTransaction.command_name,
112 token: currentTransaction.token,
113 env: currentTransaction.env,
114 payment_method: 'Orange Money',
115 client_phone: req.query.phone,
116 api_key_sha256: singleton.SHA256Encrypt(sellerCustomer.public_key),
117 api_secret_sha256: singleton.SHA256Encrypt(sellerCustomer.private_key),
118 })
119 });
120
121 await newIpn.save();
122
123 singleton.sendSms(req.query.phone, currentTransaction);
124 singleton.sendMNP(currentTransaction);
125
126 return res.end(JSON.stringify({
127 success: 1,
128 message: Lang.transalte('om-success')
129 }));
130
131 }
132 else /*if ((firstResponse.body.indexOf("invalide") > -1)*/ {
133 return res.end(JSON.stringify({
134 success: -1,
135 message: Lang.transalte('om-err1')
136 }));
137 }
138
139 }
140 else {
141 throw new Error('cannot get response from orange');
142 }
143 } catch (e) {
144 singleton.saveError(e);
145 res.end(JSON.stringify({
146 success: -1,
147 message: Lang.transalte('err-unknow')
148 }));
149 }
150
151 }
152
153
154 /**
155 * @route('ALL','/api/payment/orange-money/:token')
156 */
157 async payment(req, res) {
158
159 Lang.setLang(req.query.lang);
160
161 try {
162
163 let currentTransaction = await tables.Transaction.findOne({
164 where: {
165 token: req.params.token
166 }
167 });
168
169 if (!currentTransaction) {
170 return res.status(403).json({
171 success: -1,
172 message: Lang.transalte('err-invalid-payment')
173 });
174 }
175
176 let sellerCustomer = await currentTransaction.getCustomerBelongs();
177
178 if (!((sellerCustomer.validation_prod || currentTransaction.env !== 'prod') && sellerCustomer.status && sellerCustomer.sms_confirmed && sellerCustomer.mail_confirmed)) {
179 return res.end(JSON.stringify({
180 success: -1,
181 message: "Le vendeur n'a pas encore vérifiér son compte",
182 }));
183 }
184
185 if (currentTransaction.target_payment !== null && currentTransaction.target_payment.indexOf('Orange Money') === -1) {
186 return res.end(JSON.stringify({
187 success: -1,
188 message: Lang.transalte('err-not-payment-allowed')
189 }));
190 }
191
192 currentTransaction.total = currentTransaction.item_price + singleton.toLocalFeeDecimal(currentTransaction.currency, currentTransaction.item_price, (sellerCustomer.g_fee_rel || currentTransaction.fee_rel), currentTransaction.no_calculate_fee, false);
193
194 if (currentTransaction.env === 'prod') {
195 currentTransaction.sentAmount = singleton.toDecimal(currentTransaction.currency, "XOF", currentTransaction.total, true)
196 }
197 else {
198 currentTransaction.sentAmount = 2;
199 }
200
201 currentTransaction.notification = sellerCustomer.notification;
202 currentTransaction.business_name = sellerCustomer.business_name;
203 currentTransaction.success_url = currentTransaction.success_url || sellerCustomer.default_success_url;
204 currentTransaction.cancel_url = currentTransaction.cancel_url || sellerCustomer.default_cancel_url;
205 currentTransaction.error_url = currentTransaction.error_url || sellerCustomer.default_error_url;
206 currentTransaction.ipn_url = currentTransaction.ipn_url || sellerCustomer.default_ipn_url;
207
208
209 let _ = OrangeMoneyController.sendRequest({req, res, currentTransaction, sellerCustomer});
210 } catch (e) {
211 singleton.saveError(e);
212 res.end(JSON.stringify({
213 success: -1,
214 message: Lang.transalte('err-server-angu'),
215 }));
216 }
217
218 }
219 success(req,res){
220 res.end(JSON.stringify({
221 success: 1,
222 message: "SUCCESS ORANGE MONEY"
223 }));
224 }
225 cancel(req,res){
226 res.end(JSON.stringify({
227 success: 1,
228 message: "CANCEL ORANGE MONEY"
229 }));
230 }
231 failled(req,res){
232 res.end(JSON.stringify({
233 success: 1,
234 message: "FAILLED ORANGE MONEY"
235 }));
236 }
237 ipn(req,res){
238 res.end(JSON.stringify({
239 success: 1,
240 message: "IPN ORANGE MONEY"
241 }));
242 }
243
244 // success(req,res)
245 // {
246 // res.send("success");
247 // }
248 //
249 // cancel(req,res)
250 // {
251 // res.send("cancel");
252 // }
253 //
254 // ipn(req,res)
255 // {
256 // res.send("ipn");
257 // }
258 //
259 // failled(req,res)
260 // {
261 // res.send("failled");
262 // }
263
264 test(req,res){
265 let pack = require('locutus/php/misc/pack');
266 let date = require('locutus/php/datetime/date');
267 let md5 = require('locutus/php/strings/md5');
268 let http_build_query = require('locutus/php/url/http_build_query');
269 let base64_encode = require('locutus/php/url/base64_encode');
270 let time = require('locutus/php/datetime/time');
271 let strtoupper = require('locutus/php/strings/strtoupper');
272
273 let hash_hmac = (algo, message, key) => {
274 let crypto = require('crypto');
275
276 let hmac = crypto.createHmac(algo, key);
277 return hmac.update(new Buffer(message, 'utf-8')).digest("hex")
278 };
279
280 let strtolower = require('locutus/php/strings/strtolower');
281
282 let secretKey = 'C820171244834EECED32EF23086AA4292821015162BAB08A0E3DE47E213338DE';
283 let secretKey2 = 'A5272DF494854B9732F1E342965377019CFE915EA738279870C154F18AE8E795';
284 let binKey = new Buffer(secretKey, "hex");
285 let binKey2 = new Buffer(secretKey2, "hex");
286 let dateH = date('c');
287 let dateH2 = date('c');
288 let algo = 'SHA512';
289 let algo2 = 'SHA512';
290 let identifier = md5('2729570356');
291 let identifier2 = md5('2842317971');
292 let command = "Paiement Payexpresse " + Math.random().toString().substr(1,8);
293 let command2 = "Paiement Payexpresse " + Math.random().toString().substr(1,8);
294 let site = md5('1111117583');
295 let site2 = md5('1257226205');
296 let amount = 1;
297 let amount2 =1;
298 let refCommand = base64_encode(http_build_query({
299 d: time(),
300 token: Math.random().toString().substr(1,8)
301 }));
302 let refCommand2 = base64_encode(http_build_query({
303 d: time(),
304 token: Math.random().toString().substr(1,8)
305 }));
306
307 let message = `S2M_COMMANDE=${command}&S2M_DATEH=${dateH}&S2M_HTYPE=${algo2}&S2M_IDENTIFIANT=${identifier}&S2M_REF_COMMANDE=${refCommand}&S2M_SITE=${site}&S2M_TOTAL=${amount}`;
308 let message2 = `S2M_COMMANDE=${command2}&S2M_DATEH=${dateH2}&S2M_HTYPE=${algo2}&S2M_IDENTIFIANT=${identifier2}&S2M_REF_COMMANDE=${refCommand2}&S2M_SITE=${site2}&S2M_TOTAL=${amount2}`;
309
310 let hmac = strtoupper(hash_hmac(strtolower(algo), message, binKey));
311 let hmac2 = strtoupper(hash_hmac(strtolower(algo2), message2, binKey2));
312 res.render('test.html.twig', {
313 form: {
314 S2M_IDENTIFIANT: identifier2,
315 S2M_SITE: site2,
316 S2M_TOTAL: amount2,
317 S2M_REF_COMMANDE: refCommand2,
318 S2M_COMMANDE: command2,
319 S2M_DATEH: dateH2,
320 S2M_HTYPE: algo2,
321 S2M_HMAC: hmac2
322 }
323 });
324 }
325
326};