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

Last change on this file since 557 was 556, checked in by dungnv, 11 years ago
File size: 9.9 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', 'soanbaigiang.smas.vn khong co dich vu nay!');
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_ENOUGH_MSG')) define('_SBG_CHARGING_FAIL_NOT_ENOUGH_MSG', 'Tai khoan chinh cua so dien thoai :sendid: khong du de thuc hien lenh nap :amount: vao tai khoan :username: tren soanbaigiang.smas.vn');
24
25if ( ! defined('_SBG_SEND_MT_FAIL')) define('_SBG_SEND_MT_FAIL', -1);
26if ( ! defined('_SBG_SEND_MT_SUCCESS')) define('_SBG_SEND_MT_SUCCESS', 1);
27
28if ( ! defined('_SBG_MT_SEND_NUMBER')) define('_SBG_MT_SEND_NUMBER', '841662860000');
29if ( ! defined('_SBG_MT_CP_CODE')) define('_SBG_MT_CP_CODE', '601');
30
31if ( ! defined('_SBG_SERVICE_COMMAND_CODE')) define('_SBG_SERVICE_COMMAND_CODE', 'SBG');
32if ( ! defined('_SBG_SERVICE_SERVICEID')) define('_SBG_SERVICE_SERVICEID', '8062');
33
34Class Services_model extends CI_Model
35{
36        private $_arySmsMO = array(
37                _SBG_MO_REGISTER                        => array('pattern' => '/^(SBG[[:blank:]])(DK)$/'),
38                _SBG_MO_REGISTER_FOR            => array('pattern' => '/^(SBG[[:blank:]])(DK)([[:blank:]][0-9]{10,11})$/'),
39                _SBG_MO_PHONENUM_CHANGE         => array('pattern' => '/^(SBG[[:blank:]])(DDT)([[:blank:]][0-9]{10,11})([[:blank:]][a-zA-Z0-9]{4,16})$/'),
40                _SBG_MO_PASSWORD_RECOVERY       => array('pattern' => '/^(SBG[[:blank:]])(MK)$/'),
41                _SBG_MO_PASSWORD_CHANGE         => array('pattern' => '/^(SBG[[:blank:]])(MK)([[:blank:]][a-zA-Z0-9]{4,16})$/'),
42                _SBG_MO_VIP_REGISTER            => array('pattern' => '/^(SBG[[:blank:]])(VIP)([[:blank:]][0-9]{1,2})$/'),
43                _SBG_MO_PAY_TO_ACCOUNT          => array('pattern' => '/^(SBG[[:blank:]])(NAP)([[:blank:]][0-9]{3,})$/'),
44                _SBG_MO_PAY_TO_ACCOUNT_FOR      => array('pattern' => '/^(SBG[[:blank:]])(NAP)([[:blank:]][0-9]{3,})([[:blank:]][0-9]{10,11})$/'),
45                _SBG_MO_UNSUBSCRIBE_SERVICE => array('pattern' => '/^(SBG[[:blank:]])(HUY)$/')
46        );
47       
48        public function __construct(){
49                parent::__construct();
50        }
51       
52        private function messageAnalys ($message)
53        {
54                $aryMatched = array();
55                $result = false;
56                foreach ($this->_arySmsMO as $key => $pattern)
57                {
58                        $result = preg_match($pattern['pattern'], $message, $aryMatched);
59                        if ((bool)$result) break;
60                }
61               
62                $key = (bool)$result ? $key:_SBG_MO_SYNTAX_ERR;
63                $aryResult = (bool)$result ? array('case' => $key, 'content' => $aryMatched):array('case' => $key);
64                return $aryResult;
65        }
66       
67        public function processMORequest ($sentNumber,$message)
68        {
69                $debugMsg = $message."\n";
70               
71                $aryMatch = array();
72                if (preg_match('/^(84)([0-9]{9,10})$/', $sentNumber, $aryMatch) == 1) {
73                        $sentNumber = '0'. $aryMatch[2];
74                }
75               
76                $smsReturn = '';
77                $aryMOAnalys = $this->messageAnalys($message);         
78                $case = $aryMOAnalys['case'];
79                $aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
80                if ($aryContents != null){
81                        unset($aryContents[0]);
82                        $aryTmp = array();                     
83                        foreach ($aryContents as $key => $val)
84                        {
85                                $aryTmp[] = trim($val);
86                        }
87                        $aryContents = $aryTmp;
88                }
89               
90                $this->load->model('frontend/user_model', 'objUserModel');
91                $debugMsg .= $case;
92                write_file('mt_log', $debugMsg);
93                switch ($case) {
94                        case _SBG_MO_REGISTER:
95                        case _SBG_MO_REGISTER_FOR:
96                                $userName = isset($aryContents[2]) ? $aryContents[2] : $sentNumber; 
97                                $collaborator = isset($aryContents[2]) ? $sentNumber:null;
98                                $sms = $this->objUserModel->register($userName, $collaborator);
99                                if ($case == _SBG_MO_REGISTER_FOR) {
100                                        $smsReturn = '1|Ban da dang ky thanh cong dich vu Soan bai giang Online cho '. $userName;
101                                        $arySMS = explode ('|', $sms);
102                                        $sendMT = $this->sendMT($userName, $arySMS[1]);
103                                }
104                                else {
105                                        $smsReturn = $sms;
106                                }
107                                break;
108                        case _SBG_MO_PHONENUM_CHANGE:
109                                $oldPhoneNumber = $aryContents[2];
110                                $password = $aryContents[3];
111                                $smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
112                                $sendMT = $this->sendMT($oldPhoneNumber, $smsReturn);
113                                break;
114                        case _SBG_MO_PASSWORD_RECOVERY:
115                        case _SBG_MO_PASSWORD_CHANGE:
116                                $password = isset($aryContents[2]) ? $aryContents[2] : null;
117                                $smsReturn = $this->objUserModel->changePassword($sentNumber, $password);
118                                break;                                         
119                        case _SBG_MO_VIP_REGISTER:
120                                $p_code = $aryContents[2];
121                                $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code);
122                                break;
123                        case _SBG_MO_PAY_TO_ACCOUNT:
124                        case _SBG_MO_PAY_TO_ACCOUNT_FOR:
125                                $userName = isset($aryContents[3]) ? $aryContents[3] : $sentNumber;
126                                $amount = $aryContents[2];
127                                $result =$this->chargeRootAccountProcess($sentNumber, $amount);
128                                if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
129                                        $aryPatterns = array ('/:sendid:/', '/:amount:/', '/:username:/');
130                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber, $amount, $userName), _SBG_CHARGING_FAIL_NOT_ENOUGH_MSG);
131                                        $smsReturn = '0|'.$smsReturn;
132                                }
133                                else {
134                                        $smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
135                                        if (_SBG_MO_PAY_TO_ACCOUNT_FOR) {
136                                                $smsMT = $smsReturn .' tu thue bao '. $sentNumber;
137                                                $sendMT = $this->sendMT($userName, $smsMT);
138                                        }
139                                }
140                               
141                                //write to paid log
142                                //$this->objUserModel->insertPaidlog();
143                                break;
144                        case _SBG_MO_UNSUBSCRIBE_SERVICE:
145                        case _SBG_MO_SYNTAX_ERR:
146                        default:
147                                $smsReturn = '0|'._SBG_MO_SYNTAX_ERR_MSG;
148                                break;
149                }
150               
151                return $smsReturn;
152        }
153       
154        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
155        {
156                /*
157            ["BalanceAfterTransaction"]=>
158            string(1) "0"
159            ["ErrorCode"]=>
160            string(3) "199"
161            ["ErrorMessage"]=>
162            string(29) "L?i exception Payment service"
163            ["LastLockedOutDate"]=>
164            string(19) "0001-01-01T00:00:00"
165            ["RemainTimeToUnLock"]=>
166            string(1) "0"
167            ["Success"]=>
168            string(5) "false"
169            ["TransactionAmount"]=>
170            string(1) "0"
171            ["TransactionID"]=>
172            string(1) "0"
173            ["TransactionTime"]=>
174            string(19) "0001-01-01T00:00:00"
175        */
176                $url = $this->config->item('topup_service_url');
177                $action = 'http://tempuri.org/ITopupService/TopUp';
178                $xml = $this->load->view('topup', $aryCardInfo, true);
179               
180                $client = new nusoap_client($url);
181                $client->useHTTPPersistentConnection();
182                $client->soap_defencoding = 'UTF-8';
183                $client->operation = $action;
184               
185                $result = $client->send($xml, $action);
186               
187                if ($client->fault) {
188                        return _SBG_CARD_PAY_FAIL;
189                }
190               
191                $keys = array_keys($result);
192               
193                $aryTopupResult = $result[$keys[0]];
194               
195                if ($aryTopupResult['Success'] == 'false') {
196                        return _SBG_CARD_PAY_FAIL;
197                }
198               
199                return _SBG_CARD_PAY_SUCCESS;
200        }
201       
202        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
203        {
204                $url = $this->config->item('charging_service_url');
205                $username = $this->config->item('charging_username');
206                $password = $this->config->item('charging_password');
207                $action = 'http://tempuri.org/processCharging';         
208               
209                $aryParams = array(
210                                'user' => $username,
211                                'pass' => $password,
212                                'MSISDN' => $sentID,
213                                'Charging' => $amount,
214                                'Contents' => $contents
215                );
216               
217                $xml = $this->load->view('charging', $aryParams, true);
218                $client = new nusoap_client($url);
219                $client->useHTTPPersistentConnection();
220                $client->soap_defencoding = 'UTF-8';
221                $client->operation = $action;
222                $result = $client->send($xml, $action);
223               
224                if ($client->fault) {
225                        return _SBG_CHARGING_FAIL;
226                }
227               
228                if ((int)$result['processChargingResult'] == _SBG_CHARGING_FAIL_NOT_ENOUGH)
229                        return _SBG_CHARGING_FAIL_NOT_ENOUGH;
230               
231                return _SBG_CHARGING_SUCCESS;
232        }
233       
234        public function sendMT ($receiverID, $message)
235        {
236                $aryMatch = array();
237                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
238                        $receiverID = '84'. $aryMatch[2];
239                }
240               
241                $username = $this->config->item('mt_username');
242                $password = $this->config->item('mt_password');
243                $action = 'http://tempuri.org/InsertMT';
244               
245                $aryParams = array(
246                                'User' => $username,
247                                'Pass' => $password,
248                                'CPCode' => _SBG_MT_CP_CODE,
249                                'UserID' => _SBG_MT_SEND_NUMBER,
250                                'RequestID' => '4',
251                                'ReceiverID' => $receiverID,
252                                'ServiceID' => _SBG_SERVICE_SERVICEID,
253                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
254                                'ContentType' => '0',
255                                'Info' => $message
256                );
257               
258                $xml = $this->load->view('mt', $aryParams, true);
259                $url = $this->config->item('mt_service_url');
260                $client = new nusoap_client($url);
261                $client->useHTTPPersistentConnection();
262                $client->soap_defencoding = 'UTF-8';
263                $client->operation = $action;
264                $result = $client->send($xml, $action);
265               
266                //write_file('mt_log', var_export($client->fault, true)."\n".var_export($xml, true));
267               
268                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
269                        return _SBG_SEND_MT_FAIL;
270                }
271               
272                return _SBG_SEND_MT_SUCCESS;
273        }
274}
Note: See TracBrowser for help on using the repository browser.