· 6 years ago · Feb 27, 2020, 04:08 AM
1<?php
2/**
3 * @file
4 * kraken_stock.inc
5 */
6
7/**
8 * Kraken_stock_get_biller_last_trx description.
9 *
10 * Master caller for get deposit in biller by query db.
11 *
12 */
13function kraken_stock_get_biller_last_trx($billers) {
14 $billers = explode('|', $billers);
15 $output = array();
16 foreach ($billers as $biller) {
17 $fcaller = 'kraken_stock_get_' . $biller;
18 $res = $fcaller();
19 $output[$biller] = empty($res) ? '' : $res;
20 }
21 $output = json_encode($output);
22
23 return $output;
24}
25
26/**
27 * Get Indosat Mobo Stock.
28 */
29function kraken_stock_get_indosat() {
30 $settings = variable_get('indosat_saldo_mobo_settings');
31
32 module_load_include('inc', 'indosat_saldo_mobo');
33 module_load_include('inc', 'indosat_saldo_mobo', 'indosat_saldo_mobo.admin');
34
35 $chiper = indosat_saldo_mobo_encrypt($settings['account']['allocator']['password'], $settings['account']['allocator']['password_is_chiper']);
36
37 $initiator = array(
38 'identifier_type' => 11,
39 'identifier' => $settings['account']['allocator']['identifier'],
40 'security_credential' => $chiper,
41 'shortcode' => $settings['account']['allocator']['shortcode'],
42 );
43
44 $receiver = array(
45 'identifier_type' => 4,
46 'identifier' => $settings['account']['allocator']['shortcode'],
47 );
48
49 $accounts = indosat_saldo_mobo_accounts();
50
51 foreach ($accounts as $account => $title) {
52 $data['target'][] = $settings['account'][$account]['shortcode'];
53 }
54 $targets = $data['target'];
55 $data['target'] = array_unique($data['target']);
56 $target_chunk = array_chunk($data['target'], 10);
57
58 try {
59 $api = array();
60
61 // Request Balance Inquiry per chunk.
62 foreach ($target_chunk as $chunk) {
63 $data['target'] = implode(';', $chunk);
64 $api[] = indosat_saldo_mobo_api('QueryOrganizationBalance', $data, $initiator, $receiver);
65 }
66
67 // Join the Result.
68 $dataTarget = indosat_saldo_mobo_balance_join_chunk($api);
69
70 foreach ($targets as $target => $sc) {
71 // Find in result array.
72 $idx = array_search($sc, array_column($dataTarget, 'AccountHolderID'));
73 $balance = $dataTarget[$idx];
74
75 // Check if we get the wrong index, nullify if we did.
76 if ($balance['AccountHolderID'] != $sc) {
77 foreach ($balance as $k => $v) {
78 $balance[$k] = NULL;
79 }
80 }
81
82 foreach ($balance as $key => $value) {
83 switch ($key) {
84 case 'AvailableBalance':
85 case 'ReservedBalance':
86 case 'UnclearedBalance':
87 case 'CurrentBalance':
88 $balance_value = number_format($value);
89 break;
90
91 default:
92 $balance_value = $value;
93 break;
94
95 }
96 $balance_rows[$key][] = $balance_value;
97 }
98 }
99
100 foreach ($balance_rows as $key => $value) {
101 if ($key != 'CurrentBalance') {
102 continue;
103 }
104 $rows[] = $value;
105 }
106
107 $stock = array('header' => $accounts, 'rows' => $rows);
108 $output = theme('table', $stock);
109 }
110 catch (SoapFault $e) {
111 $output = $e->getMessage();
112 }
113
114 return $output;
115}
116
117/**
118 * Get Smartfren Stock.
119 */
120function kraken_stock_get_smartfren() {
121 module_load_include('inc', 'smartfren');
122 $settings = variable_get('smartfren_settings');
123 $data = array(
124 'sourceMsisdn' => $settings['username'],
125 'pin' => $settings['password'],
126 );
127 $output = '';
128 $h2h = smartfren_api('03', $data);
129 if (!empty($h2h['returnCode']) && $h2h['returnCode'] == '00') {
130 $output .= 'Deposit: ' . number_format($h2h['balance']);
131 $denom = explode(' | ', $h2h['denomination']);
132 $balance = explode(' |', $h2h['denominationBalance']);
133 foreach ($denom as $key => $item) {
134 if (!empty($item)) {
135 $header_denom = trim($denom[$key]);
136 $header[] = is_numeric($header_denom) ? number_format(trim($header_denom)) : $header_denom;
137 $rows[] = number_format(trim($balance[$key]));
138 }
139 }
140 $output .= theme('table', array('header' => $header, 'rows' => array($rows)));
141 }
142 else {
143 $output .= '<h4>Error: ' . $h2h['errorCode'] . '-' . $h2h['errorDescription'] . '</h4>';
144 }
145
146 return $output;
147}
148
149/**
150 * Get Tri Stock.
151 */
152function kraken_stock_get_tri() {
153 $output = '';
154
155 module_load_include('inc', 'tri');
156 $settings = variable_get('tri_settings');
157 $rows = array();
158
159 // Get DC Balance.
160 $three = tri_query_account_balance($settings['account']['dc']['msisdn']);
161 $rows[] = empty($three->QueryInventoryResult->STOCK0) ? '-' : number_format($three->QueryInventoryResult->STOCK0);
162
163 // Get SD Balance.
164 $three = tri_query_account_balance($settings['account']['sd']['msisdn']);
165 $rows[] = empty($three->QueryInventoryResult->STOCK0) ? '-' : number_format($three->QueryInventoryResult->STOCK0);
166
167 $headers = array('DC', 'SD');
168
169 // Get RS's Balance.
170 foreach (tri_rs_accounts() as $account => $title) {
171 $three = tri_query_account_balance($settings['account']['rs']['retail'][$account]['msisdn']);
172 $rows[] = empty($three->QueryInventoryResult->STOCK0) ? '-' : number_format($three->QueryInventoryResult->STOCK0);
173 $headers[] = 'RS-' . $title;
174 }
175
176 $output .= theme('table', array('header' => $headers, 'rows' => array($rows)));
177
178 return $output;
179}
180
181/**
182 * Get Tri Store Stock.
183 */
184function kraken_stock_get_tri_store() {
185 $output = '';
186 if (module_exists('tri_store')) {
187 module_load_include('inc', 'tri_store');
188 $settings = variable_get('tri_store_settings');
189 $rows = array();
190
191 // Get DC Balance.
192 $three = tri_store_query_account_balance($settings['account']['dc']['msisdn']);
193 $rows[] = empty($three->QueryInventoryResult->STOCK0) ? '-' : number_format($three->QueryInventoryResult->STOCK0);
194
195 // Get SD Balance.
196 $three = tri_store_query_account_balance($settings['account']['sd']['msisdn']);
197 $rows[] = empty($three->QueryInventoryResult->STOCK0) ? '-' : number_format($three->QueryInventoryResult->STOCK0);
198
199 $headers = array('DC', 'SD');
200
201 // Get RS's Balance.
202 foreach (tri_store_rs_accounts() as $account => $title) {
203 $three = tri_store_query_account_balance($settings['account']['rs']['retail'][$account]['msisdn']);
204 $rows[] = empty($three->QueryInventoryResult->STOCK0) ? '-' : number_format($three->QueryInventoryResult->STOCK0);
205 $headers[] = 'RS-' . $title;
206 }
207
208 $output .= theme('table', array('header' => $headers, 'rows' => array($rows)));
209 }
210
211 return $output;
212}
213
214/**
215 * Get Gemilang Deposit.
216 */
217function kraken_stock_get_gemilang() {
218 $output = '';
219 $result = kraken_stock_get_biller_data('gemilang');
220
221 $deposit = 'Deposit Info Not Found';
222
223 if ($result) {
224 $matches = explode('SAL=', $result->data);
225 $matches = explode(',HRG=', $matches[1]);
226
227 if (!empty($matches[0])) {
228 $deposit = $matches[0];
229 }
230 }
231
232 $output .= "<h4>Deposit: " . $deposit . '</h4>';
233
234 return $output;
235}
236
237/**
238 * Get Fastpay Deposit.
239 */
240function kraken_stock_get_fastpay() {
241 $output = '';
242
243 module_load_include('inc', 'fastpay');
244 $settings = variable_get('fastpay_settings');
245 $xml = new SimpleXMLElement('<methodCall/>');
246 $xml->addChild('methodName', 'fastpay.balance');
247 $xml_params = $xml->addChild('params');
248 $xml_params->addChild('param')->addChild('value')->addChild('string', $settings['username']);
249 $xml_params->addChild('param')->addChild('value')->addChild('string', $settings['password']);
250 $data = $xml->asXML();
251 if (empty($settings['production'])) {
252 module_load_include('inc', 'fastpay_multi', 'fastpay_multi.mock');
253 $fastpay = _fastpay_multi_response_dummy($data, 'balance');
254 }
255 else {
256 $fastpay = fastpay_request_call($data, 'balance');
257 }
258 foreach (simplexml_load_string($fastpay)->params->param->value->array->data->value as $value) {
259 $params[] = (string) $value->string;
260 }
261 $output .= '<h4>Deposit: ' . number_format($params[2]) . '</h4>';
262
263 return $output;
264}
265
266/**
267 * Get Pelangi Deposit.
268 */
269function kraken_stock_get_pelangi() {
270 $output = '';
271
272 $settings = variable_get('pelangi_settings');
273 $url = $settings['endpoint'] . '/apis/accountInformation.json';
274 $trx_date = format_date(REQUEST_TIME, 'custom', 'YmdHis');
275 $signature = md5($settings['username'] . $settings['password'] . $trx_date . $settings['secret']);
276 $options = array(
277 'headers' => array(
278 'Authorization' => 'PELANGIREST username=' . $settings['username'] . '&password=' . $settings['password'] . '&signature=' . $signature,
279 'Content-Type' => 'application/x-www-form-urlencoded',
280 ),
281 'context' => stream_context_create(array(
282 'ssl' => array(
283 'verify_peer' => FALSE,
284 'verify_peer_name' => FALSE,
285 ),
286 )),
287 );
288
289 if ($settings['production']) {
290 $result = drupal_http_request($url, $options);
291 }
292 else {
293 module_load_include('inc', 'pelangi', 'pelangi.mock');
294 $result = pelangi_mock_accountinfo($url, $options);
295 }
296
297 $deposit = '-';
298 $used = '-';
299 if (!empty($result->data)) {
300 $data = json_decode($result->data, TRUE);
301 $deposit = empty($data['data']['accountInfo']['balance']) ? '-' : number_format($data['data']['accountInfo']['balance']);
302 $used = empty($data['data']['accountInfo']['pemakaian_saldo_hari_ini']) ? '-' : number_format($data['data']['accountInfo']['pemakaian_saldo_hari_ini']);
303 }
304 $output .= '<h4>Deposit: ' . $deposit . '</h4>';
305 $output .= '<h4>Pemakaian saldo hari ini: ' . $used . '</h4>';
306
307 return $output;
308}
309
310/**
311 * Get PPS Deposit.
312 */
313function kraken_stock_get_pps() {
314 $output = '';
315
316 $settings = variable_get('pps_mobile_settings');
317
318 if ($settings['production']) {
319 $result = kraken_stock_get_biller_data('pps_mobile');
320 $biller = entity_load_single('kraken_biller', $result->biller_id);
321 $trx_no = $biller->data['ClientNoTrx'];
322 }
323 else {
324 $trx_no = date('YmdHis');
325 }
326 $data = array(
327 'user' => $settings['user'],
328 'notrx' => $trx_no,
329 'signature' => md5($trx_no . md5($settings['pass'])),
330 );
331 module_load_include('inc', 'pps_mobile');
332 $response = pps_mobile_api('StatusTrxWithDeposit', $data);
333 $matches = explode('Deposit Anda saat ini adalah ', $response['Message']);
334 $matches = explode('. Stok', $matches[1]);
335
336 if (!empty($matches[0])) {
337 $output .= "<h4>Deposit: " . $matches[0] . '</h4>';
338 }
339
340 return $output;
341}
342
343/**
344 * Get SMTEL Deposit.
345 */
346function kraken_stock_get_smtel() {
347 $output = '';
348 $result = kraken_stock_get_biller_data('smtel');
349
350 $deposit = 'Deposit Info Not Found';
351
352 if ($result) {
353 $data = $result->data;
354 $data = unserialize($data);
355 $deposit = number_format($data['balance']);
356 }
357
358 $output .= "<h4>Deposit: " . $deposit . '</h4>';
359
360 return $output;
361}
362
363/**
364 * Get PMB Deposit.
365 */
366function kraken_stock_get_pmb() {
367 $output = '';
368 $result = kraken_stock_get_biller_data('pmb');
369
370 $deposit = 'Deposit Info Not Found';
371
372 if ($result) {
373 $data = $result->data;
374 $data = unserialize($data);
375 $deposit = number_format($data['balance']);
376 }
377
378 $output .= "<h4>Deposit: " . $deposit . '</h4>';
379
380 return $output;
381}
382
383/**
384 * Get Indotama Deposit.
385 */
386function kraken_stock_get_indotama() {
387 $output = '';
388 $result = db_query("SELECT biller_id, data FROM {kraken_biller} WHERE type = 'indotama' AND status = 'success' ORDER BY biller_id DESC LIMIT 1")->fetchObject();
389
390 $deposit = 'Deposit Info Not Found';
391
392 if ($result) {
393 $data = strtolower(unserialize($result->data));
394 $data = explode(';', $data);
395 $found = FALSE;
396
397 // Loop all params.
398 foreach ($data as $val) {
399 if (strpos($val, 'saldo akhir') !== FALSE) {
400 $found = TRUE;
401 continue;
402 }
403
404 if ($found) {
405 $val = str_replace('rp.', '', $val);
406 $val = intval(trim($val));
407 $deposit = number_format($val);
408 break;
409 }
410 }
411 }
412
413 $output .= "<h4>Deposit: " . $deposit . '</h4>';
414
415 return $output;
416}
417
418/**
419 * Get Axiata Deposit.
420 */
421function kraken_stock_get_axiata($no_prefix) {
422 $output = '';
423
424 $settings = variable_get('axiata_kraken_inventory_settings');
425 $endpoint = $settings['endpoint'];
426 $timeout = $settings['timeout'];
427 $list_of_no = !empty($settings['no']) ? $settings['no'] : array();
428 $no_dompul = !empty($list_of_no[$no_prefix]) ? $list_of_no[$no_prefix] : NULL;
429 $is_prod = $settings['production'];
430 $is_response_fail = FALSE;
431
432 if (!empty($no_dompul) && !empty($endpoint)) {
433 // Setup url for request.
434 $url = str_replace("[no_prefix]", $no_prefix, $endpoint);
435 $url = str_replace("[no_dompul]", $no_dompul, $url);
436
437 // Setup options for request.
438 $options['timeout'] = $timeout;
439
440 if ($is_prod) {
441 // Execute HTTP Request.
442 $response = drupal_http_request($url, $options);
443 if (!empty($response->error)) {
444 $is_response_fail = TRUE;
445 watchdog('axiata_data', '@message', array('@message' => print_r($response, TRUE)), WATCHDOG_ERROR, 'kraken_stock');
446 }
447 $response = json_decode($response->data, TRUE);
448 }
449 else {
450 // Get response from mock.
451 module_load_include('inc', 'kraken_stock', 'kraken_stock.mock');
452 $response = kraken_stock_mock_api('axiata');
453 }
454 }
455
456 $dealer_name = NULL;
457 $balance = NULL;
458 if (!$is_response_fail) {
459 $data = !empty($response['data']) ? $response['data'] : array();
460 $dealer_name = !empty($data['dealer_name']) ? $data['dealer_name'] : NULL;
461 $balance = !empty($data['balance_po']) ? $data['balance_po'] : NULL;
462 }
463
464 $output = "<h4>Deposit of ";
465 if (!$is_response_fail) {
466 if (($dealer_name !== NULL && $balance !== NULL)) {
467 $balance = number_format($balance);
468 foreach ($data as $key => $value) {
469 if (!empty($value) && strpos($key, 'denom') === 0) {
470 $balance .= " | " . $key . " : " . number_format($value);
471 }
472 }
473 $output .= $dealer_name . " [" . $no_prefix . " - " . $no_dompul . "] : " . $balance . "</h4>";
474 }
475 else {
476 $output .= "[" . $no_prefix . " - " . $no_dompul . "] : " . "Response from biller is not valid.</h4>";
477 }
478 }
479 else {
480 $output .= "[" . $no_prefix . " - " . $no_dompul . "] : " . "Error response from server, please try to reload this page.</h4>";
481 }
482
483 return $output;
484}
485
486/**
487 * Get BMAS Deposit.
488 */
489function kraken_stock_get_bmas() {
490 $output = '';
491 $val = bmas_pln_prepaid_balance_inquiry();
492 $deposit = number_format($val);
493 $output .= "<h4>Deposit: " . $deposit . '</h4>';
494 return $output;
495}
496
497/**
498 * Get MIP Deposit.
499 */
500function kraken_stock_get_mip() {
501 $output = '';
502 $result = kraken_stock_get_biller_data('mip');
503 $deposit = 'Deposit Info Not Found';
504
505 if ($result) {
506 $data = $result->data;
507 $data = unserialize($data);
508 $deposit = number_format($data['message']['SAL']);
509 }
510
511 $output .= "<h4>Deposit: " . $deposit . '</h4>';
512 return $output;
513}
514
515/**
516 * Get MIP PLN Prepaid Deposit.
517 */
518function kraken_stock_get_mip_pln_prepaid() {
519 $output = '';
520 $result = kraken_stock_get_biller_data('mip_pln_prepaid');
521 $deposit = 'Deposit Info Not Found';
522
523 if ($result) {
524 $data = $result->data;
525 $data = unserialize($data);
526 // Pattern.
527 $re = '/(saldo=|sal=)\d+/i';
528 $message = $data['kraken_hidden_params']['message'];
529 preg_match($re, $message, $matches);
530 if ($matches) {
531 $deposit = str_replace($matches[1], '', $matches[0]);
532 $deposit = number_format($deposit);
533 }
534 }
535
536 $output .= "<h4>Deposit: " . $deposit . '</h4>';
537 return $output;
538}
539
540/**
541 * Get Bakoel BPJS Kesehatan Deposit.
542 */
543function kraken_stock_get_bakoel_bpjs_kesehatan() {
544 $output = '';
545 // Not real response will show this.
546 $response = array(
547 'quota' => '987654321',
548 'Description' => 'sepulsa.com',
549 );
550
551 $settings = variable_get('bakoel_bpjs_kesehatan_settings');
552
553 // Overide response if production.
554 if ($settings['production']) {
555 module_load_include('inc', 'bakoel_bpjs_kesehatan');
556 $bakoel_client = bakoel_bpjs_kesehatan_client();
557 if ($bakoel_client) {
558 $response = $bakoel_client->call('ppMitraInfo', array());
559 }
560 else {
561 $response['quota'] = 'RTO';
562 }
563 }
564
565 $deposit = number_format($response['quota']);
566
567 $output .= "<h4>Deposit: " . $deposit . '</h4>';
568 return $output;
569}
570
571/**
572 * Get Tektaya Deposit.
573 */
574function kraken_stock_get_tektaya_deposit() {
575 $output = '';
576 $settings = variable_get('tektaya_settings');
577 module_load_include('inc', 'tektaya');
578 $tektaya = tektaya_xml_check_saldo_api();
579 if (!empty($tektaya->error)) {
580 $response = 'RTO';
581 }
582 else {
583 $tektaya = @simplexml_load_string($tektaya->data);
584 if ($tektaya === FALSE) {
585 $response = 'RTO';
586 }
587 else {
588 $tektaya = json_decode(json_encode($tektaya), TRUE);
589 $response = $tektaya['message'];
590 }
591 }
592
593 $deposit = number_format($response);
594
595 $output .= "<h4>Deposit: " . $deposit . '</h4>';
596 return $output;
597}
598
599/**
600 * Get Masa Deposit.
601 */
602function kraken_stock_get_masa() {
603 $output = '';
604 module_load_include('inc', 'masa');
605 $masa_deposit = masa_get_deposit();
606 if ($masa_deposit === NULL) {
607 $response = 'Deposit Info Not Found';
608 }
609 else {
610 $response = number_format($masa_deposit);
611 }
612
613 $output .= "<h4>Deposit: " . $response . '</h4>';
614 return $output;
615}
616
617/**
618 * Get Ayopay Deposit.
619 */
620function kraken_stock_get_ayopay() {
621 $output = '';
622 $result = kraken_stock_get_biller_data('ayopay');
623 $deposit = 'Deposit Info Not Found';
624
625 if ($result) {
626 $data = $result->data;
627 $data = unserialize($data);
628 if (!isset($data['kraken_hidden_params']['chk'])) {
629 $deposit = number_format($data['kraken_hidden_params']['req']['response']['saldo']);
630 }
631 }
632
633 $output .= "<h4>Deposit: " . $deposit . '</h4>';
634 return $output;
635}
636
637/**
638 * Get Jatelindo BPJS Kesehatan Deposit.
639 */
640function kraken_stock_get_jatelindo_bpjs_kesehatan() {
641 $output = '';
642
643 $settings = variable_get('jatelindo_bpjs_kesehatan_kraken_inventory_settings');
644 $host = strpos($settings['host'], 'http://') ? '' : 'http://';
645 $host .= $settings['host'];
646 $url = $host . ':' . $settings['port'];
647 $trx_date = date('YmdHis');
648 $api_key = md5($settings['pin'] . $trx_date);
649 $data = array(
650 "api_key" => $api_key,
651 "layanan" => $settings['service'],
652 "registrasi_code" => $settings['registration_code'],
653 "tid" => $settings['TID'],
654 "stan" => $trx_date,
655 );
656 $options = array(
657 'headers' => array(
658 'Content-Type' => 'application/json',
659 ),
660 'method' => 'POST',
661 'data' => drupal_json_encode($data),
662 );
663
664 if ($settings['production']) {
665 $result = drupal_http_request($url, $options);
666
667 if ($settings['debug']) {
668 watchdog('jatelindo_bpjs_kesehatan', '<pre>!response</pre>', array('!response' => print_r($result, TRUE)), WATCHDOG_INFO);
669 }
670 }
671 else {
672 module_load_include('inc', 'jatelindo_bpjs_kesehatan', 'jatelindo_bpjs_kesehatan.mock');
673 $result = jatelindo_bpjs_kesehatan_mock_accountinfo($url, $options);
674 }
675
676 $deposit = '-';
677 if (!empty($result->data)) {
678 $data = json_decode($result->data, TRUE);
679 $deposit = empty($data['msg']) ? '-' : number_format($data['msg']);
680 }
681 if($deposit != '-') {
682 $output .= '<h4>Deposit: ' . $deposit . '</h4>';
683 }
684 else {
685 $output .= '<h4>Deposit Info Not Found</h4>';
686 }
687
688 return $output;
689}
690
691/**
692 * Helper for get biller data by.
693 *
694 * Biller type.
695 * Status.
696 * Created last 30 days.
697 */
698function kraken_stock_get_biller_data($biller_name) {
699 $result = db_query("SELECT biller_id, data FROM {kraken_biller} WHERE type = :biller_name AND status = 'success' AND created > :last_30_day ORDER BY biller_id DESC LIMIT 1",
700 array(
701 ':last_30_day' => time() - (30 * 24 * 60 * 60),
702 ':biller_name' => $biller_name,
703 )
704 )->fetchObject();
705
706 return $result;
707}