source: pro-violet-viettel/sourcecode/application/modules/services/models/services_model.php @ 677

Last change on this file since 677 was 677, checked in by dungnv, 10 years ago
File size: 15.4 KB
Line 
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3if ( ! defined('_SBG_MO_REGISTER'))                                     define('_SBG_MO_REGISTER',                              'SUBSCR');
4if ( ! defined('_SBG_MO_REGISTER_FOR'))                                 define('_SBG_MO_REGISTER_FOR',                  'SUBSCR_FOR');
5if ( ! defined('_SBG_MO_PHONENUM_CHANGE'))                              define('_SBG_MO_PHONENUM_CHANGE',               'PHONE_CHANGE');
6if ( ! defined('_SBG_MO_PASSWORD_RECOVERY'))                    define('_SBG_MO_PASSWORD_RECOVERY',     'PWD_RECOVER');
7if ( ! defined('_SBG_MO_VIP_REGISTER'))                                 define('_SBG_MO_VIP_REGISTER',                  'REG_VIP');
8if ( ! defined('_SBG_MO_PAY_TO_ACCOUNT'))                               define('_SBG_MO_PAY_TO_ACCOUNT',                'PAY');
9if ( ! defined('_SBG_MO_PAY_TO_ACCOUNT_FOR'))                   define('_SBG_MO_PAY_TO_ACCOUNT_FOR',    'PAY_FOR');
10if ( ! defined('_SBG_MO_UNSUBSCRIBE_SERVICE'))                  define('_SBG_MO_UNSUBSCRIBE_SERVICE',   'UNSUBSCR');
11if ( ! defined('_SBG_MO_SUPPOR_SERVICE'))                               define('_SBG_MO_SUPPOR_SERVICE',                'SUPPORT');
12if ( ! defined('_SBG_MO_SYNTAX_ERR'))                                   define('_SBG_MO_SYNTAX_ERR',                    'SYNTAX_ERR');
13
14if ( ! defined('_SBG_CARD_PAY_FAIL'))                                   define('_SBG_CARD_PAY_FAIL',                    -1);
15if ( ! defined('_SBG_CARD_PAY_SUCCESS'))                                define('_SBG_CARD_PAY_SUCCESS',                 1);
16
17if ( ! defined('_SBG_CHARGING_FAIL'))                                   define('_SBG_CHARGING_FAIL', -1);
18if ( ! defined('_SBG_CHARGING_SUCCESS'))                                define('_SBG_CHARGING_SUCCESS', 1);
19if ( ! defined('_SBG_CHARGING_FAIL_NOT_ENOUGH'))                define('_SBG_CHARGING_FAIL_NOT_ENOUGH', 401);
20if ( ! defined('_SBG_CHARGING_FAIL_NOT_VIETTEL'))               define('_SBG_CHARGING_FAIL_NOT_VIETTEL', 402);
21if ( ! defined('_SBG_CHARGING_NUMBER_NOT_EXISTED'))     define('_SBG_CHARGING_NUMBER_NOT_EXISTED', 403);
22if ( ! defined('_SBG_CHARGING_NUMBER_NOT_AVAILABLE'))   define('_SBG_CHARGING_NUMBER_NOT_AVAILABLE', 404);
23if ( ! defined('_SBG_CHARGING_NUMBER_CHANGED_OWNER'))   define('_SBG_CHARGING_NUMBER_CHANGED_OWNER', 405);
24if ( ! defined('_SBG_CHARGING_FAIL_MORETHAN_LIMIT'))    define('_SBG_CHARGING_FAIL_MORETHAN_LIMIT', 406);
25if ( ! defined('_SBG_CHARGING_GENERAL_ERROR'))                  define('_SBG_CHARGING_GENERAL_ERROR', 440);
26if ( ! defined('_SBG_CHARGING_NUMBER_NOT_REGISTED'))    define('_SBG_CHARGING_NUMBER_NOT_REGISTED', 501);
27
28if ( ! defined('_SBG_SEND_MT_FAIL'))                                    define('_SBG_SEND_MT_FAIL', -1);
29if ( ! defined('_SBG_SEND_MT_SUCCESS'))                                 define('_SBG_SEND_MT_SUCCESS', 1);
30if ( ! defined('_SBG_MT_SEND_NUMBER'))                                  define('_SBG_MT_SEND_NUMBER', '841662860000');
31if ( ! defined('_SBG_MT_CP_CODE'))                                              define('_SBG_MT_CP_CODE', '601');
32
33if ( ! defined('_SBG_SERVICE_COMMAND_CODE'))                    define('_SBG_SERVICE_COMMAND_CODE', 'SBG');
34if ( ! defined('_SBG_SERVICE_SERVICEID'))                               define('_SBG_SERVICE_SERVICEID', '8062');
35
36if ( ! defined('_SBG_MO_PROCCESS_ERR'))                                 define('_SBG_MO_PROCCESS_ERR', 0);
37
38Class Services_model extends CI_Model
39{
40        private $_arySmsMO = array(
41                _SBG_MO_REGISTER                        => array('pattern' => '/^(SBG)([[:blank:]])(DK)$/'),
42                _SBG_MO_REGISTER_FOR            => array('pattern' => '/^(SBG)([[:blank:]])(DK)([[:blank:]])([0-9]{10,11})$/'),
43                _SBG_MO_PHONENUM_CHANGE         => array('pattern' => '/^(SBG)([[:blank:]])(DDT)([[:blank:]])([0-9]{10,11})([[:blank:]])([a-zA-Z0-9]{1,})$/'),
44                _SBG_MO_PASSWORD_RECOVERY       => array('pattern' => '/^(SBG)([[:blank:]])(MK)$/'),
45                _SBG_MO_VIP_REGISTER            => array('pattern' => '/^(SBG)([[:blank:]])(VIP)([[:blank:]])([a-zA-Z0-9]{1,})$/'),
46                _SBG_MO_PAY_TO_ACCOUNT          => array('pattern' => '/^(SBG)([[:blank:]])(NAP)([[:blank:]])([0-9]{3,})$/'),
47                _SBG_MO_PAY_TO_ACCOUNT_FOR      => array('pattern' => '/^(SBG)([[:blank:]])(NAP)([[:blank:]])([0-9]{3,})([[:blank:]])([0-9]{10,11})$/'),
48                _SBG_MO_SUPPOR_SERVICE          => array('pattern' => '/^(SBG)([[:blank:]])(HD)$/'),
49                _SBG_MO_UNSUBSCRIBE_SERVICE => array('pattern' => '/^(SBG)([[:blank:]])(HUY)$/')
50        );     
51       
52        public function __construct(){
53                parent::__construct();
54                $this->load->helper('language');
55                $this->lang->load('messages', 'message');
56        }
57       
58        /**
59         * messageAnalys
60         *
61         * @todo Analysis content of incoming message
62         *
63         * @param String $message - coming SMS
64         * @return multitype:Ambigous <string, multitype> multitype:multitype
65         */
66        public function messageAnalys ($message)
67        {
68                $aryMatched = array(); 
69                $aryContent = array();
70                $result = FALSE;
71               
72                foreach ($this->_arySmsMO as $case => $pattern)
73                {
74                        $result = preg_match($pattern['pattern'], strtoupper(trim($message)));
75                        if ((bool)$result) {
76                                $aryMatched = explode(' ', trim($message));
77                                break;
78                        }
79                }
80               
81                $case = (bool)$result ? $case:_SBG_MO_SYNTAX_ERR;
82               
83                foreach($aryMatched as $k => $val) {
84                        if ($k > 1) $aryContent[] = $val;                       
85                }
86               
87                return array('case' => $case, 'content' => $aryContent);;
88        }
89       
90        /**
91         * processMORequest
92         *
93         * @todo process action analysed from messageAnalys
94         *
95         * @param unknown $sentNumber
96         * @param unknown $message
97         * @return string
98         */
99        public function processMORequest ($sentNumber, $message)
100        {
101                $debugMsg = $message."\n";
102               
103                $sentNumber = $this->formatPhoneNumber($sentNumber);
104               
105                $smsReturn = '';
106                $aryMOAnalys = $this->messageAnalys($message);         
107                $case = $aryMOAnalys['case'];
108                $aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
109                $this->load->model('frontend/user_model', 'objUserModel');
110                $debugMsg .= $case;
111               
112                switch ($case) {
113                        case _SBG_MO_REGISTER:
114                        case _SBG_MO_REGISTER_FOR:
115                                if ($case == _SBG_MO_REGISTER_FOR) {
116                                        if (!preg_match('/^([0-9]{10,11}$)$/', $aryContents[0])) {
117                                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
118                                                break;
119                                        }
120                                }
121                               
122                                $userName = isset($aryContents[0]) ? $this->formatPhoneNumber($aryContents[0]) : $sentNumber; 
123                                $collaborator = isset($aryContents[0]) ? $sentNumber:null;
124                                $collaboratorMsg = '';
125                               
126                                $sms = $this->objUserModel->register($userName, $collaborator, $collaboratorMsg);
127                                if ($case == _SBG_MO_REGISTER_FOR) {
128                                        $smsReturn = $collaboratorMsg;
129                                        $arySMS = explode ('|', $sms);
130                                        if ((int)$arySMS[0] == 1 )
131                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $arySMS[1]);
132                                }
133                                else {
134                                        $smsReturn = $sms;
135                                }
136                                break;
137                        case _SBG_MO_PHONENUM_CHANGE:
138                                if (!preg_match('/^(09\d{8}|01\d{9})$/', $aryContents[0])) {
139                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
140                                        break;
141                                }
142                               
143                                $oldPhoneNumber = $this->formatPhoneNumber($aryContents[0]);
144                                $password = $aryContents[1];
145                                $smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
146                               
147                                $arySMS = explode('|', $smsReturn);
148                                $changeResult = (int)$arySMS[0];
149                               
150                                $aryPatterns = array('/:sentNumber:/');
151                                $sms = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_USER_OLD_PHONENUM_CHANGE_SUCCESS_MSG'));
152                                $sendMT = $changeResult == 1 ? $this->sendMT($this->formatPhoneNumber($oldPhoneNumber, -1), $sms) : '';
153                                break;
154                        case _SBG_MO_PASSWORD_RECOVERY:
155                                $smsReturn = $this->objUserModel->changePassword($sentNumber);
156                                break;
157                        case _SBG_MO_VIP_REGISTER:
158                                $p_code = $aryContents[0];
159                                $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code);
160                                $this->objUserModel->insertRegisterVipLog($sentNumber, $p_code, true);
161                                break;
162                        case _SBG_MO_PAY_TO_ACCOUNT:
163                        case _SBG_MO_PAY_TO_ACCOUNT_FOR:
164                                $userName = isset($aryContents[1]) ? $this->formatPhoneNumber($aryContents[1]) : $sentNumber;
165                               
166                                if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR && !preg_match('/^(09\d{8}|01\d{9})$/', $userName)) {
167                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
168                                        break;
169                                }
170                               
171                                $amount = $aryContents[0];
172                                if (!preg_match('/^[0-9]{3,}$/', $amount)) {
173                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_AMOUNT_WRONG_FORMAT_MSG');
174                                        break;
175                                }
176                               
177                                $result =$this->chargeRootAccountProcess($sentNumber, $amount, $sentNumber.' paid for SBG');
178                               
179                                if ($result == _SBG_CHARGING_SUCCESS) {
180                                        $smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
181                                        if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
182                                                $arySmsReturn = explode ('|', $smsReturn);
183                                                $aryPatterns = array('/:amount:/', '/:sentNumber:/');
184                                                $smsMT = preg_replace($aryPatterns, array($amount, $sentNumber), lang('_SBG_MO_PAY_TO_ACCOUNT_FOR_MSG'));
185                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $smsMT);
186                                        }
187                                       
188                                        //write to paid log
189                                        $paidTime = date('Y-m-d H:i:s');
190                                        $this->objUserModel->insertPaidlog($sentNumber, $userName, _SBG_PAIDTYPE_SMS, $amount, $paidTime);
191                                }
192                                else if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
193                                        $aryPatterns = array ('/:amount:/');
194                                        $smsReturn = preg_replace($aryPatterns, array($amount), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_MSG'));
195                                        if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
196                                                $aryPatterns = array ('/:amount:/', '/:username:/');
197                                                $smsReturn = preg_replace($aryPatterns, array($amount, $userName), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_FOR_MSG'));
198                                        }
199                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
200                                }
201                                else if ($result == _SBG_CHARGING_FAIL_MORETHAN_LIMIT) {
202                                        $aryPatterns = array ('/:sendid:/');
203                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_MORETHAN_LIMIT_MSG'));
204                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
205                                }else if ($result == _SBG_CHARGING_FAIL_NOT_VIETTEL || $result == _SBG_CHARGING_NUMBER_NOT_EXISTED || $result == _SBG_CHARGING_NUMBER_NOT_AVAILABLE) {
206                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_NOT_VIETTEL_MSG');
207                                }                               
208                                else {
209                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_MSG');
210                                }
211                                break;
212                        case _SBG_MO_SUPPOR_SERVICE:
213                                $smsReturn = '1|'.lang('_SBG_MO_SUPPORT_MSG');
214                                break;
215                        case _SBG_MO_UNSUBSCRIBE_SERVICE:
216                        case _SBG_MO_SYNTAX_ERR:
217                        default:
218                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_SYNTAX_ERR_MSG');
219                                break;
220                }
221               
222                return $smsReturn.'|'.$case;
223        }
224       
225       
226        /**
227         * cardPaidProcess
228         *
229         * @todo Call Topup webservice to check and lock prepaid-card
230         * @param Array $aryCardInfo
231         * @param Array $aryTopupResult
232         * @return string
233         */
234        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
235        {
236                $url = $this->config->item('topup_service_url');
237                $action = 'http://tempuri.org/ITopupService/TopUp';
238                $xml = $this->load->view('topup', $aryCardInfo, true);
239               
240                $client = new nusoap_client($url);
241                $client->useHTTPPersistentConnection();
242                $client->soap_defencoding = 'UTF-8';
243                $client->operation = $action;
244               
245                $result = $client->send($xml, $action);
246               
247                if ($client->fault) {
248                        return _SBG_CARD_PAY_FAIL;
249                }
250               
251                $keys = array_keys($result);
252               
253                $aryTopupResult = $result[$keys[0]];
254
255                $debugMsg = 'Logged time: '. date('Y-m-d H:i:s') ."\n";
256                $debugMsg .=  'Send:'."\n" .$xml ."\n\n";
257                $debugMsg .= 'Result:'."\n". var_export($result, TRUE);
258                write_file('./log/topup_log', $debugMsg);
259
260                if ($aryTopupResult['Success'] == 'false') {
261                        return _SBG_CARD_PAY_FAIL;
262                }
263               
264                return _SBG_CARD_PAY_SUCCESS;
265        }
266       
267        /**
268         * chargeRootAccountProcess
269         *
270         * @todo Call charging webservice to charged fee from phone root account
271         * @param String $sentID
272         * @param Integer $amount
273         * @param String $contents
274         * @return string|number
275         */
276        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
277        {
278                $url = $this->config->item('charging_service_url');
279                $username = $this->config->item('charging_username');
280                $password = $this->config->item('charging_password');
281                $action = 'http://tempuri.org/processCharging';         
282               
283                $aryParams = array(
284                                'user' => $username,
285                                'pass' => $password,
286                                'MSISDN' => $sentID,
287                                'Charging' => $amount,
288                                'Contents' => $contents
289                );
290               
291                $xml = $this->load->view('charging', $aryParams, true);
292                $client = new nusoap_client($url);
293                $client->useHTTPPersistentConnection();
294                $client->soap_defencoding = 'UTF-8';
295                $client->operation = $action;
296                $result = $client->send($xml, $action);
297
298                if ($client->fault) {
299                        return _SBG_CHARGING_FAIL;
300                }
301               
302                $aryErrs = array (      _SBG_CHARGING_FAIL_NOT_ENOUGH,
303                                                        _SBG_CHARGING_FAIL_NOT_VIETTEL,
304                                                        _SBG_CHARGING_FAIL_MORETHAN_LIMIT,
305                                                        _SBG_CHARGING_NUMBER_NOT_EXISTED,
306                                                        _SBG_CHARGING_NUMBER_NOT_AVAILABLE,
307                                                        _SBG_CHARGING_NUMBER_CHANGED_OWNER,
308                                                        _SBG_CHARGING_GENERAL_ERROR,
309                                                        _SBG_CHARGING_NUMBER_NOT_REGISTED);
310               
311                if (in_array((int)$result['processChargingResult'], $aryErrs))
312                        return (int)$result['processChargingResult'];           
313               
314                return _SBG_CHARGING_SUCCESS;
315        }
316       
317        /**
318         * sendMT
319         *
320         * @todo Calling MT webservice to send a SMS to a phone number
321         * @param String $receiverID
322         * @param String $message
323         * @return string
324         */
325        public function sendMT ($receiverID, $message)
326        {
327                $aryMatch = array();
328               
329                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
330                        $receiverID = '84'. $aryMatch[2];
331                }
332               
333                $username = $this->config->item('mt_username');
334                $password = $this->config->item('mt_password');
335                $action = 'http://tempuri.org/InsertMT';
336               
337                $aryParams = array(
338                                'User' => $username,
339                                'Pass' => $password,
340                                'CPCode' => _SBG_MT_CP_CODE,
341                                'UserID' => _SBG_MT_SEND_NUMBER,
342                                'RequestID' => '4',
343                                'ReceiverID' => $receiverID,
344                                'ServiceID' => _SBG_SERVICE_SERVICEID,
345                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
346                                'ContentType' => '0',
347                                'Info' => $message
348                );
349               
350                $xml = $this->load->view('mt', $aryParams, true);
351                $url = $this->config->item('mt_service_url');
352                $client = new nusoap_client($url);
353                $client->useHTTPPersistentConnection();
354                $client->soap_defencoding = 'UTF-8';
355                $client->operation = $action;
356                $result = $client->send($xml, $action);
357               
358                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
359                        return _SBG_SEND_MT_FAIL;
360                }
361               
362                return _SBG_SEND_MT_SUCCESS;
363        }
364       
365        public function getCollaborator ($type = 2, $page = 10, $displaypage = 1) {
366                $action = 'http://tempuri.org/InsertMT';
367                $url = $this->config->item('bccs_service_url');
368                $username = $this->config->item('bccs_username');
369                $password = $this->config->item('bccs_password');
370                $wscode = 'getListShopAndStaftInfo';
371                $aryParams = array(
372                                'user' => $username,
373                                'pass' => $password,
374                                'wscode' => $wscode,
375                                'type' => $type,
376                                'page' => $page,
377                                'displaypage' => $displaypage
378                );
379                $xml = $this->load->view('bscc', $aryParams, true);
380               
381                $client = new nusoap_client($url);
382                $client->useHTTPPersistentConnection();
383                $client->soap_defencoding = 'UTF-8';
384                $client->operation = $action;
385                $result = $client->send($xml, $action);
386                return $result;
387        }
388       
389        /**
390         * formatPhoneNumber
391         *
392         * @todo format phone number to prefix 0 or 84
393         * @param String $phoneNumber
394         * @param Integer $direction
395         * @return String
396         */
397        public function formatPhoneNumber ($phoneNumber, $direction = 1) {
398                $aryMatch = array();
399                if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == 1) {
400                        $result = '0'. $aryMatch[2];
401                }
402                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == 1) {
403                        $result = $phoneNumber;
404                }
405                else if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == -1) {
406                        $result = $phoneNumber;
407                }
408                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == -1) {
409                        $result = '84'. $aryMatch[2];
410                }
411               
412                return $result;
413        }
414}
Note: See TracBrowser for help on using the repository browser.