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

Last change on this file since 655 was 653, checked in by namnd, 10 years ago
File size: 14.6 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                $aryResult = array('case' => $case, 'content' => $aryContent);
88                return $aryResult;
89        }
90       
91        /**
92         * processMORequest
93         *
94         * @todo process action analysed from messageAnalys
95         *
96         * @param unknown $sentNumber
97         * @param unknown $message
98         * @return string
99         */
100        public function processMORequest ($sentNumber, $message)
101        {
102                $debugMsg = $message."\n";
103               
104                $sentNumber = $this->formatPhoneNumber($sentNumber);
105               
106                $smsReturn = '';
107                $aryMOAnalys = $this->messageAnalys($message);         
108                $case = $aryMOAnalys['case'];
109                $aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
110                $this->load->model('frontend/user_model', 'objUserModel');
111                $debugMsg .= $case;
112               
113                switch ($case) {
114                        case _SBG_MO_REGISTER:
115                        case _SBG_MO_REGISTER_FOR:
116                                if ($case == _SBG_MO_REGISTER_FOR) {
117                                        if (!preg_match('/^([0-9]{10,11}$)$/', $aryContents[0])) {
118                                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
119                                                break;
120                                        }
121                                }
122                               
123                                $userName = isset($aryContents[0]) ? $this->formatPhoneNumber($aryContents[0]) : $sentNumber; 
124                                $collaborator = isset($aryContents[0]) ? $sentNumber:null;
125                                $collaboratorMsg = '';
126                               
127                                $sms = $this->objUserModel->register($userName, $collaborator, $collaboratorMsg);
128                                if ($case == _SBG_MO_REGISTER_FOR) {
129                                        $smsReturn = $collaboratorMsg;
130                                        $arySMS = explode ('|', $sms);
131                                        if ((int)$arySMS[0] == 1 )
132                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $arySMS[1]);
133                                }
134                                else {
135                                        $smsReturn = $sms;
136                                }
137                                break;
138                        case _SBG_MO_PHONENUM_CHANGE:
139                                if (!preg_match('/^(09\d{8}|01\d{9})$/', $aryContents[0])) {
140                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
141                                        break;
142                                }
143                               
144                                $oldPhoneNumber = $this->formatPhoneNumber($aryContents[0]);
145                                $password = $aryContents[1];
146                                $smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
147                               
148                                $arySMS = explode('|', $smsReturn);
149                                $changeResult = (int)$arySMS[0];
150                               
151                                $aryPatterns = array('/:sentNumber:/');
152                                $sms = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_USER_OLD_PHONENUM_CHANGE_SUCCESS_MSG'));
153                                $sendMT = $changeResult == 1 ? $this->sendMT($this->formatPhoneNumber($oldPhoneNumber, -1), $sms) : '';
154                                break;
155                        case _SBG_MO_PASSWORD_RECOVERY:
156                                $smsReturn = $this->objUserModel->changePassword($sentNumber);
157                                break;
158                        case _SBG_MO_VIP_REGISTER:
159                                $p_code = $aryContents[0];
160                                $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code);
161                                $this->objUserModel->insertRegisterVipLog($sentNumber, $p_code, true);
162                                break;
163                        case _SBG_MO_PAY_TO_ACCOUNT:
164                        case _SBG_MO_PAY_TO_ACCOUNT_FOR:
165                                $userName = isset($aryContents[1]) ? $this->formatPhoneNumber($aryContents[1]) : $sentNumber;
166                               
167                                if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR && !preg_match('/^(09\d{8}|01\d{9})$/', $userName)) {
168                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_PHONENUM_WRONG_FORMAT_MSG');
169                                        break;
170                                }
171                               
172                                $amount = $aryContents[0];
173                                if (!preg_match('/^[0-9]{3,}$/', $amount)) {
174                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_AMOUNT_WRONG_FORMAT_MSG');
175                                        break;
176                                }
177                               
178                                $result =$this->chargeRootAccountProcess($sentNumber, $amount, $sentNumber.' paid for SBG');
179                               
180                                if ($result == _SBG_CHARGING_SUCCESS) {
181                                        $smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
182                                        if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
183                                                $arySmsReturn = explode ('|', $smsReturn);
184                                                $aryPatterns = array('/:amount:/', '/:sentNumber:/');
185                                                $smsMT = preg_replace($aryPatterns, array($amount, $sentNumber), lang('_SBG_MO_PAY_TO_ACCOUNT_FOR_MSG'));
186                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $smsMT);
187                                        }
188                                       
189                                        //write to paid log
190                                        $paidTime = date('Y-m-d H:i:s');
191                                        $this->objUserModel->insertPaidlog($sentNumber, $userName, _SBG_PAIDTYPE_SMS, $amount, $paidTime);
192                                }
193                                else if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
194                                        $aryPatterns = array ('/:amount:/');
195                                        $smsReturn = preg_replace($aryPatterns, array($amount), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_MSG'));
196                                        if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
197                                                $aryPatterns = array ('/:amount:/', '/:username:/');
198                                                $smsReturn = preg_replace($aryPatterns, array($amount, $userName), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_FOR_MSG'));
199                                        }
200                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
201                                }
202                                else if ($result == _SBG_CHARGING_FAIL_MORETHAN_LIMIT) {
203                                        $aryPatterns = array ('/:sendid:/');
204                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_MORETHAN_LIMIT_MSG'));
205                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
206                                }else if ($result == _SBG_CHARGING_FAIL_NOT_VIETTEL || $result == _SBG_CHARGING_NUMBER_NOT_EXISTED || $result == _SBG_CHARGING_NUMBER_NOT_AVAILABLE) {
207                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_NOT_VIETTEL_MSG');
208                                }                               
209                                else {
210                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_MSG');
211                                }
212                                break;
213                        case _SBG_MO_SUPPOR_SERVICE:
214                                $smsReturn = '1|'.lang('_SBG_MO_SUPPORT_MSG');
215                                break;
216                        case _SBG_MO_UNSUBSCRIBE_SERVICE:
217                        case _SBG_MO_SYNTAX_ERR:
218                        default:
219                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_SYNTAX_ERR_MSG');
220                                break;
221                }
222               
223                return $smsReturn.'|'.$case;
224        }
225       
226       
227        /**
228         * cardPaidProcess
229         *
230         * @todo Call Topup webservice to check and lock prepaid-card
231         * @param Array $aryCardInfo
232         * @param Array $aryTopupResult
233         * @return string
234         */
235        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
236        {
237                $url = $this->config->item('topup_service_url');
238                $action = 'http://tempuri.org/ITopupService/TopUp';
239                $xml = $this->load->view('topup', $aryCardInfo, true);
240               
241                $client = new nusoap_client($url);
242                $client->useHTTPPersistentConnection();
243                $client->soap_defencoding = 'UTF-8';
244                $client->operation = $action;
245               
246                $result = $client->send($xml, $action);
247               
248                if ($client->fault) {
249                        return _SBG_CARD_PAY_FAIL;
250                }
251               
252                $keys = array_keys($result);
253               
254                $aryTopupResult = $result[$keys[0]];
255
256                $debugMsg = 'Logged time: '. date('Y-m-d H:i:s') ."\n";
257                $debugMsg .=  'Send:'."\n" .$xml ."\n\n";
258                $debugMsg .= 'Result:'."\n". var_export($result, TRUE);
259                write_file('./log/topup_log', $debugMsg);
260
261                if ($aryTopupResult['Success'] == 'false') {
262                        return _SBG_CARD_PAY_FAIL;
263                }
264               
265                return _SBG_CARD_PAY_SUCCESS;
266        }
267       
268        /**
269         * chargeRootAccountProcess
270         *
271         * @todo Call charging webservice to charged fee from phone root account
272         * @param String $sentID
273         * @param Integer $amount
274         * @param String $contents
275         * @return string|number
276         */
277        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
278        {
279                $url = $this->config->item('charging_service_url');
280                $username = $this->config->item('charging_username');
281                $password = $this->config->item('charging_password');
282                $action = 'http://tempuri.org/processCharging';         
283               
284                $aryParams = array(
285                                'user' => $username,
286                                'pass' => $password,
287                                'MSISDN' => $sentID,
288                                'Charging' => $amount,
289                                'Contents' => $contents
290                );
291               
292                $xml = $this->load->view('charging', $aryParams, true);
293                $client = new nusoap_client($url);
294                $client->useHTTPPersistentConnection();
295                $client->soap_defencoding = 'UTF-8';
296                $client->operation = $action;
297                $result = $client->send($xml, $action);
298
299                if ($client->fault) {
300                        return _SBG_CHARGING_FAIL;
301                }
302               
303                $aryErrs = array (      _SBG_CHARGING_FAIL_NOT_ENOUGH,
304                                                        _SBG_CHARGING_FAIL_NOT_VIETTEL,
305                                                        _SBG_CHARGING_FAIL_MORETHAN_LIMIT,
306                                                        _SBG_CHARGING_NUMBER_NOT_EXISTED,
307                                                        _SBG_CHARGING_NUMBER_NOT_AVAILABLE,
308                                                        _SBG_CHARGING_NUMBER_CHANGED_OWNER,
309                                                        _SBG_CHARGING_GENERAL_ERROR,
310                                                        _SBG_CHARGING_NUMBER_NOT_REGISTED);
311               
312                if (in_array((int)$result['processChargingResult'], $aryErrs))
313                        return (int)$result['processChargingResult'];           
314               
315                return _SBG_CHARGING_SUCCESS;
316        }
317       
318        /**
319         * sendMT
320         *
321         * @todo Calling MT webservice to send a SMS to a phone number
322         * @param String $receiverID
323         * @param String $message
324         * @return string
325         */
326        public function sendMT ($receiverID, $message)
327        {
328                $aryMatch = array();
329               
330                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
331                        $receiverID = '84'. $aryMatch[2];
332                }
333               
334                $username = $this->config->item('mt_username');
335                $password = $this->config->item('mt_password');
336                $action = 'http://tempuri.org/InsertMT';
337               
338                $aryParams = array(
339                                'User' => $username,
340                                'Pass' => $password,
341                                'CPCode' => _SBG_MT_CP_CODE,
342                                'UserID' => _SBG_MT_SEND_NUMBER,
343                                'RequestID' => '4',
344                                'ReceiverID' => $receiverID,
345                                'ServiceID' => _SBG_SERVICE_SERVICEID,
346                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
347                                'ContentType' => '0',
348                                'Info' => $message
349                );
350               
351                $xml = $this->load->view('mt', $aryParams, true);
352                $url = $this->config->item('mt_service_url');
353                $client = new nusoap_client($url);
354                $client->useHTTPPersistentConnection();
355                $client->soap_defencoding = 'UTF-8';
356                $client->operation = $action;
357                $result = $client->send($xml, $action);
358               
359                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
360                        return _SBG_SEND_MT_FAIL;
361                }
362               
363                return _SBG_SEND_MT_SUCCESS;
364        }
365       
366        /**
367         * formatPhoneNumber
368         *
369         * @todo format phone number to prefix 0 or 84
370         * @param String $phoneNumber
371         * @param Integer $direction
372         * @return String
373         */
374        public function formatPhoneNumber ($phoneNumber, $direction = 1) {
375                $aryMatch = array();
376                if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == 1) {
377                        $result = '0'. $aryMatch[2];
378                }
379                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == 1) {
380                        $result = $phoneNumber;
381                }
382                else if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == -1) {
383                        $result = $phoneNumber;
384                }
385                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == -1) {
386                        $result = '84'. $aryMatch[2];
387                }
388               
389                return $result;
390        }
391}
Note: See TracBrowser for help on using the repository browser.