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

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