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

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