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

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