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

Last change on this file since 629 was 629, checked in by dungnv, 10 years ago
File size: 14.6 KB
Line 
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3if ( ! defined('_SBG_MO_REGISTER')) define('_SBG_MO_REGISTER', 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
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                $aryResult = (bool)$result ? array('case' => $key, 'content' => $aryContent):array('case' => $key);
128                return $aryResult;
129        }
130       
131        public function processMORequest ($sentNumber,$message)
132        {
133                $debugMsg = $message."\n";
134               
135                $sentNumber = $this->formatPhoneNumber($sentNumber);
136               
137                $smsReturn = '';
138                $aryMOAnalys = $this->messageAnalys($message);         
139                $case = $aryMOAnalys['case'];
140                $aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
141                $this->load->model('frontend/user_model', 'objUserModel');
142                $debugMsg .= $case;
143               
144                switch ($case) {
145                        case _SBG_MO_REGISTER:
146                        case _SBG_MO_REGISTER_FOR:
147                                if ($case == _SBG_MO_REGISTER_FOR) {
148                                        if (!preg_match('/^([0-9]{10,11}$)$/', $aryContents[2])) {
149                                                $smsReturn = '0|So dien thoai khach hang khong hop le. Vui long kiem tra lai.';
150                                                break;
151                                        }
152                                }
153                               
154                                $userName = isset($aryContents[2]) ? $this->formatPhoneNumber($aryContents[2]) : $sentNumber; 
155                                $collaborator = isset($aryContents[2]) ? $sentNumber:null;
156                                $collaboratorMsg = '';
157                               
158                                $sms = $this->objUserModel->register($userName, $collaborator, $collaboratorMsg);
159                                if ($case == _SBG_MO_REGISTER_FOR) {
160                                        $smsReturn = $collaboratorMsg;
161                                        $arySMS = explode ('|', $sms);
162                                        if ((int)$arySMS[0] == 1 )
163                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $arySMS[1]);
164                                }
165                                else {
166                                        $smsReturn = $sms;
167                                }
168                                break;
169                        case _SBG_MO_PHONENUM_CHANGE:
170                                if (!preg_match('/^(09\d{8}|01\d{9})$/', $aryContents[2])) {
171                                        $smsReturn = '0|So dien thoai khach hang khong hop le. Vui long kiem tra lai.';
172                                        break;
173                                }
174                               
175                                $oldPhoneNumber = $this->formatPhoneNumber($aryContents[2]);
176                                $password = $aryContents[3];
177                                $smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
178                               
179                                $arySMS = explode('|', $smsReturn);
180                                $changeResult = (int)$arySMS[0];
181                               
182                                $sms = 'Ban da doi thanh cong so dien thoai tren SBG Online sang so dien thoai ' .$sentNumber. '. Tran trong.';
183                                $sendMT = $changeResult == 1 ? $this->sendMT($this->formatPhoneNumber($oldPhoneNumber, -1), $sms) : '';
184                                break;
185                        case _SBG_MO_PASSWORD_RECOVERY:
186                        /* case _SBG_MO_PASSWORD_CHANGE: */
187                                /* $password = isset($aryContents[2]) ? $aryContents[2] : null; */
188                                $smsReturn = $this->objUserModel->changePassword($sentNumber/* , $password */);
189                                break;                                         
190                        case _SBG_MO_VIP_REGISTER:
191                                $p_code = $aryContents[2];
192                                $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code);
193                                break;
194                        case _SBG_MO_PAY_TO_ACCOUNT:
195                        case _SBG_MO_PAY_TO_ACCOUNT_FOR:
196                                $userName = isset($aryContents[3]) ? $this->formatPhoneNumber($aryContents[3]) : $sentNumber;
197                               
198                                if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR && !preg_match('/^(09\d{8}|01\d{9})$/', $userName)) {
199                                        $smsReturn = '0|So dien thoai khach hang khong hop le. Vui long kiem tra lai.';
200                                        break;
201                                }
202                               
203                                $amount = $aryContents[2];
204                                if (!preg_match('/^[0-9]{3,}$/', $amount)) {
205                                        $smsReturn = '0|So tien nap khong hop le. Vui long kiem tra lai.';
206                                        break;
207                                }
208                               
209                                $result =$this->chargeRootAccountProcess($sentNumber, $amount, $sentNumber.' paid for SBG');
210                               
211                                if ($result == _SBG_CHARGING_SUCCESS) {
212                                        $smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
213                                        if ($case == _SBG_MO_PAY_TO_ACCOUNT_FOR) {
214                                                $arySmsReturn = explode ('|', $smsReturn);
215                                                $smsMT = 'Ban da nap thanh cong ' .$amount . 'VND vao tai khoan ' .$username. ' tren SBG Online tu thue bao ' .$sentNumber. '. Tran trong.';
216                                                $sendMT = $this->sendMT($this->formatPhoneNumber($userName, -1), $smsMT);
217                                        }
218                                        //write to paid log
219                                        $paidTime = date('Y-m-d H:i:s');
220                                        $this->objUserModel->insertPaidlog($sentNumber, $userName, _SBG_PAIDTYPE_SMS, $amount, $paidTime);
221                                }
222                                else if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
223                                        //$aryPatterns = array ('/:sendid:/', '/:amount:/', '/:username:/');
224                                        //$smsReturn = preg_replace($aryPatterns, array($sentNumber, $amount, $userName), _SBG_CHARGING_FAIL_NOT_ENOUGH_MSG);
225                                        $aryPatterns = array ('/:amount:/', '/:username:/');
226                                        $smsReturn = preg_replace($aryPatterns, array($amount, $userName), _SBG_CHARGING_FAIL_NOT_ENOUGH_MSG);
227                                        $smsReturn = '0|'.$smsReturn;
228                                }
229                                else if ($result == _SBG_CHARGING_FAIL_MORETHAN_LIMIT) {
230                                        $aryPatterns = array ('/:sendid:/');
231                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber), _SBG_CHARGING_FAIL_MORETHAN_LIMIT_MSG);
232                                        $smsReturn = '0|'.$smsReturn;
233                                }else if ($result == _SBG_CHARGING_FAIL_NOT_VIETTEL || $result == _SBG_CHARGING_NUMBER_NOT_EXISTED || $result == _SBG_CHARGING_NUMBER_NOT_AVAILABLE) {
234                                        $smsReturn = '0|'._SBG_CHARGING_FAIL_NOT_VIETTEL_MSG;
235                                }                               
236                                else {
237                                        $smsReturn = '0|Loi, khong thanh toan duoc tu tai khoan goc';
238                                }
239                                break;
240                        case _SBG_MO_UNSUBSCRIBE_SERVICE:
241                        case _SBG_MO_SYNTAX_ERR:
242                        default:
243                                $smsReturn = '0|'._SBG_MO_SYNTAX_ERR_MSG;
244                                break;
245                }
246               
247                return $smsReturn.'|'.$case;
248        }
249       
250        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
251        {
252                $url = $this->config->item('topup_service_url');
253                $action = 'http://tempuri.org/ITopupService/TopUp';
254                $xml = $this->load->view('topup', $aryCardInfo, true);
255               
256                $client = new nusoap_client($url);
257                $client->useHTTPPersistentConnection();
258                $client->soap_defencoding = 'UTF-8';
259                $client->operation = $action;
260               
261                $result = $client->send($xml, $action);
262               
263                if ($client->fault) {
264                        return _SBG_CARD_PAY_FAIL;
265                }
266               
267                $keys = array_keys($result);
268               
269                $aryTopupResult = $result[$keys[0]];
270               
271                if ($aryTopupResult['Success'] == 'false') {
272                        return _SBG_CARD_PAY_FAIL;
273                }
274               
275                return _SBG_CARD_PAY_SUCCESS;
276        }
277       
278        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
279        {
280                $url = $this->config->item('charging_service_url');
281                $username = $this->config->item('charging_username');
282                $password = $this->config->item('charging_password');
283                $action = 'http://tempuri.org/processCharging';         
284               
285                $aryParams = array(
286                                'user' => $username,
287                                'pass' => $password,
288                                'MSISDN' => $sentID,
289                                'Charging' => $amount,
290                                'Contents' => $contents
291                );
292               
293                $xml = $this->load->view('charging', $aryParams, true);
294                $client = new nusoap_client($url);
295                $client->useHTTPPersistentConnection();
296                $client->soap_defencoding = 'UTF-8';
297                $client->operation = $action;
298                $result = $client->send($xml, $action);
299
300                if ($client->fault) {
301                        return _SBG_CHARGING_FAIL;
302                }
303               
304                $aryErrs = array (_SBG_CHARGING_FAIL_NOT_ENOUGH,
305                                _SBG_CHARGING_FAIL_NOT_VIETTEL,
306                                _SBG_CHARGING_FAIL_MORETHAN_LIMIT,
307                                _SBG_CHARGING_NUMBER_NOT_EXISTED,
308                                _SBG_CHARGING_NUMBER_NOT_AVAILABLE,
309                                _SBG_CHARGING_NUMBER_CHANGED_OWNER,
310                                _SBG_CHARGING_GENERAL_ERROR,
311                                _SBG_CHARGING_NUMBER_NOT_REGISTED
312                );
313               
314                if (in_array((int)$result['processChargingResult'], $aryErrs))
315                        return (int)$result['processChargingResult'];           
316               
317                return _SBG_CHARGING_SUCCESS;
318        }
319       
320        public function sendMT ($receiverID, $message)
321        {
322                $aryMatch = array();
323               
324                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
325                        $receiverID = '84'. $aryMatch[2];
326                }
327               
328                $username = $this->config->item('mt_username');
329                $password = $this->config->item('mt_password');
330                $action = 'http://tempuri.org/InsertMT';
331               
332                $aryParams = array(
333                                'User' => $username,
334                                'Pass' => $password,
335                                'CPCode' => _SBG_MT_CP_CODE,
336                                'UserID' => _SBG_MT_SEND_NUMBER,
337                                'RequestID' => '4',
338                                'ReceiverID' => $receiverID,
339                                'ServiceID' => _SBG_SERVICE_SERVICEID,
340                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
341                                'ContentType' => '0',
342                                'Info' => $message
343                );
344               
345                $xml = $this->load->view('mt', $aryParams, true);
346                $url = $this->config->item('mt_service_url');
347                $client = new nusoap_client($url);
348                $client->useHTTPPersistentConnection();
349                $client->soap_defencoding = 'UTF-8';
350                $client->operation = $action;
351                $result = $client->send($xml, $action);
352               
353                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
354                        return _SBG_SEND_MT_FAIL;
355                }
356               
357                return _SBG_SEND_MT_SUCCESS;
358        }
359       
360        public function formatPhoneNumber ($phoneNumber, $direction = 1) {
361                $aryMatch = array();
362                if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == 1) {
363                        $result = '0'. $aryMatch[2];
364                }
365                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == 1) {
366                        $result = $phoneNumber;
367                }
368                else if (preg_match('/^(84)([0-9]{9,10})$/', $phoneNumber) == 1 && $direction == -1) {
369                        $result = $phoneNumber;
370                }
371                else if (preg_match('/^(0)([0-9]{9,10})$/', $phoneNumber, $aryMatch) == 1 && $direction == -1) {
372                        $result = '84'. $aryMatch[2];
373                }
374               
375                return $result;
376        }
377}
Note: See TracBrowser for help on using the repository browser.