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

Last change on this file since 647 was 647, checked in by dungnv, 10 years ago
File size: 14.5 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:/', '/:username:/', '/:sentNumber:/');
185                                                $smsMT = preg_replace($aryPatterns, array($amount, $username, $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:/', '/:username:/');
195                                        $smsReturn = preg_replace($aryPatterns, array($amount, $userName), lang('_SBG_MO_CHARGING_FAIL_NOT_ENOUGH_MSG'));
196                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
197                                }
198                                else if ($result == _SBG_CHARGING_FAIL_MORETHAN_LIMIT) {
199                                        $aryPatterns = array ('/:sendid:/');
200                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), lang('_SBG_MO_CHARGING_FAIL_MORETHAN_LIMIT_MSG'));
201                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.$smsReturn;
202                                }else if ($result == _SBG_CHARGING_FAIL_NOT_VIETTEL || $result == _SBG_CHARGING_NUMBER_NOT_EXISTED || $result == _SBG_CHARGING_NUMBER_NOT_AVAILABLE) {
203                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_NOT_VIETTEL_MSG');
204                                }                               
205                                else {
206                                        $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_CHARGING_FAIL_MSG');
207                                }
208                                break;
209                        case _SBG_MO_SUPPOR_SERVICE:
210                                $smsReturn = '1|'.lang('_SBG_MO_SUPPORT_MSG');
211                                break;
212                        case _SBG_MO_UNSUBSCRIBE_SERVICE:
213                        case _SBG_MO_SYNTAX_ERR:
214                        default:
215                                $smsReturn = _SBG_MO_PROCCESS_ERR.'|'.lang('_SBG_MO_SYNTAX_ERR_MSG');
216                                break;
217                }
218               
219                return $smsReturn.'|'.$case;
220        }
221       
222       
223        /**
224         * cardPaidProcess
225         *
226         * @todo Call Topup webservice to check and lock prepaid-card
227         * @param Array $aryCardInfo
228         * @param Array $aryTopupResult
229         * @return string
230         */
231        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
232        {
233                $url = $this->config->item('topup_service_url');
234                $action = 'http://tempuri.org/ITopupService/TopUp';
235                $xml = $this->load->view('topup', $aryCardInfo, true);
236               
237                $client = new nusoap_client($url);
238                $client->useHTTPPersistentConnection();
239                $client->soap_defencoding = 'UTF-8';
240                $client->operation = $action;
241               
242                $result = $client->send($xml, $action);
243               
244                if ($client->fault) {
245                        return _SBG_CARD_PAY_FAIL;
246                }
247               
248                $keys = array_keys($result);
249               
250                $aryTopupResult = $result[$keys[0]];
251
252                $debugMsg = 'Logged time: '. date('Y-m-d H:i:s') ."\n";
253                $debugMsg .=  'Send:'."\n" .$xml ."\n\n";
254                $debugMsg .= 'Result:'."\n". var_export($result, TRUE);
255                write_file('./log/topup_log', $debugMsg);
256
257                if ($aryTopupResult['Success'] == 'false') {
258                        return _SBG_CARD_PAY_FAIL;
259                }
260               
261                return _SBG_CARD_PAY_SUCCESS;
262        }
263       
264        /**
265         * chargeRootAccountProcess
266         *
267         * @todo Call charging webservice to charged fee from phone root account
268         * @param String $sentID
269         * @param Integer $amount
270         * @param String $contents
271         * @return string|number
272         */
273        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
274        {
275                $url = $this->config->item('charging_service_url');
276                $username = $this->config->item('charging_username');
277                $password = $this->config->item('charging_password');
278                $action = 'http://tempuri.org/processCharging';         
279               
280                $aryParams = array(
281                                'user' => $username,
282                                'pass' => $password,
283                                'MSISDN' => $sentID,
284                                'Charging' => $amount,
285                                'Contents' => $contents
286                );
287               
288                $xml = $this->load->view('charging', $aryParams, true);
289                $client = new nusoap_client($url);
290                $client->useHTTPPersistentConnection();
291                $client->soap_defencoding = 'UTF-8';
292                $client->operation = $action;
293                $result = $client->send($xml, $action);
294
295                if ($client->fault) {
296                        return _SBG_CHARGING_FAIL;
297                }
298               
299                $aryErrs = array (      _SBG_CHARGING_FAIL_NOT_ENOUGH,
300                                                        _SBG_CHARGING_FAIL_NOT_VIETTEL,
301                                                        _SBG_CHARGING_FAIL_MORETHAN_LIMIT,
302                                                        _SBG_CHARGING_NUMBER_NOT_EXISTED,
303                                                        _SBG_CHARGING_NUMBER_NOT_AVAILABLE,
304                                                        _SBG_CHARGING_NUMBER_CHANGED_OWNER,
305                                                        _SBG_CHARGING_GENERAL_ERROR,
306                                                        _SBG_CHARGING_NUMBER_NOT_REGISTED);
307               
308                if (in_array((int)$result['processChargingResult'], $aryErrs))
309                        return (int)$result['processChargingResult'];           
310               
311                return _SBG_CHARGING_SUCCESS;
312        }
313       
314        /**
315         * sendMT
316         *
317         * @todo Calling MT webservice to send a SMS to a phone number
318         * @param String $receiverID
319         * @param String $message
320         * @return string
321         */
322        public function sendMT ($receiverID, $message)
323        {
324                $aryMatch = array();
325               
326                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
327                        $receiverID = '84'. $aryMatch[2];
328                }
329               
330                $username = $this->config->item('mt_username');
331                $password = $this->config->item('mt_password');
332                $action = 'http://tempuri.org/InsertMT';
333               
334                $aryParams = array(
335                                'User' => $username,
336                                'Pass' => $password,
337                                'CPCode' => _SBG_MT_CP_CODE,
338                                'UserID' => _SBG_MT_SEND_NUMBER,
339                                'RequestID' => '4',
340                                'ReceiverID' => $receiverID,
341                                'ServiceID' => _SBG_SERVICE_SERVICEID,
342                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
343                                'ContentType' => '0',
344                                'Info' => $message
345                );
346               
347                $xml = $this->load->view('mt', $aryParams, true);
348                $url = $this->config->item('mt_service_url');
349                $client = new nusoap_client($url);
350                $client->useHTTPPersistentConnection();
351                $client->soap_defencoding = 'UTF-8';
352                $client->operation = $action;
353                $result = $client->send($xml, $action);
354               
355                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
356                        return _SBG_SEND_MT_FAIL;
357                }
358               
359                return _SBG_SEND_MT_SUCCESS;
360        }
361       
362        /**
363         * formatPhoneNumber
364         *
365         * @todo format phone number to prefix 0 or 84
366         * @param String $phoneNumber
367         * @param Integer $direction
368         * @return String
369         */
370        public function formatPhoneNumber ($phoneNumber, $direction = 1) {
371                $aryMatch = array();
372                if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == 1) {
373                        $result = '0'. $aryMatch[2];
374                }
375                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == 1) {
376                        $result = $phoneNumber;
377                }
378                else if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == -1) {
379                        $result = $phoneNumber;
380                }
381                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == -1) {
382                        $result = '84'. $aryMatch[2];
383                }
384               
385                return $result;
386        }
387}
Note: See TracBrowser for help on using the repository browser.