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

Last change on this file since 506 was 506, checked in by dungnv, 11 years ago
File size: 8.5 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);
11
12if ( ! defined('_SBG_MO_SYNTAX_ERR')) define('_SBG_MO_SYNTAX_ERR', -1);
13if ( ! defined('_SBG_MO_SYNTAX_ERR_MSG')) define('_SBG_MO_SYNTAX_ERR_MSG', 'soanbaigiang.smas.vn khong co dich vu nay!');
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_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');
23
24if ( ! defined('_SBG_SEND_MT_FAIL')) define('_SBG_SEND_MT_FAIL', -1);
25if ( ! defined('_SBG_SEND_MT_SUCCESS')) define('_SBG_SEND_MT_SUCCESS', 1);
26
27if ( ! defined('_SBG_MT_SEND_NUMBER')) define('_SBG_MT_SEND_NUMBER', '841662860000');
28if ( ! defined('_SBG_MT_CP_CODE')) define('_SBG_MT_CP_CODE', '601');
29
30if ( ! defined('_SBG_SERVICE_COMMAND_CODE')) define('_SBG_SERVICE_COMMAND_CODE', 'SBG');
31if ( ! defined('_SBG_SERVICE_SERVICEID')) define('_SBG_SERVICE_SERVICEID', '8062');
32
33Class Services_model extends CI_Model
34{
35        private $_arySmsMO = array(
36                _SBG_MO_REGISTER                        => array('pattern' => '/^(SBG[[:blank:]])(DK)$/'),
37                _SBG_MO_REGISTER_FOR            => array('pattern' => '/^(SBG[[:blank:]])(DK)([[:blank:]][0-9]{10,11})$/'),
38                _SBG_MO_PHONENUM_CHANGE         => array('pattern' => '/^(SBG[[:blank:]])(DDT)([[:blank:]][0-9]{10,11})([[:blank:]][a-zA-Z0-9]{4,16})$/'),
39                _SBG_MO_PASSWORD_RECOVERY       => array('pattern' => '/^(SBG[[:blank:]])(MK)$/'),
40                _SBG_MO_PASSWORD_CHANGE         => array('pattern' => '/^(SBG[[:blank:]])(MK)([[:blank:]][a-zA-Z0-9]{4,16})$/'),
41                _SBG_MO_VIP_REGISTER            => array('pattern' => '/^(SBG[[:blank:]])(VIP)([[:blank:]][0-9]{1,2})$/'),
42                _SBG_MO_PAY_TO_ACCOUNT          => array('pattern' => '/^(SBG[[:blank:]])(NAP)([[:blank:]][0-9]{3,})$/'),
43                _SBG_MO_PAY_TO_ACCOUNT_FOR      => array('pattern' => '/^(SBG[[:blank:]])(NAP)([[:blank:]][0-9]{3,})([[:blank:]][0-9]{10,11})$/'),
44        );
45       
46        public function __construct(){
47                parent::__construct();
48        }
49       
50        private function messageAnalys ($message)
51        {
52                $aryMatched = array();
53                $result = false;
54                foreach ($this->_arySmsMO as $key => $pattern)
55                {
56                        $result = preg_match($pattern['pattern'], $message, $aryMatched);
57                        if ((bool)$result) break;
58                }
59               
60                $key = (bool)$result ? $key:_SBG_MO_SYNTAX_ERR;
61                $aryResult = (bool)$result ? array('case' => $key, 'content' => $aryMatched):array('case' => $key);
62                return $aryResult;
63        }
64       
65        public function processMORequest ($sentNumber,$message)
66        {
67                $aryMatch = array();
68                if (preg_match('/^(84)([0-9]{9,10})$/', $sentNumber, $aryMatch) == 1) {
69                        $sentNumber = '0'. $aryMatch[2];
70                }
71               
72                $smsReturn = '';
73                $aryMOAnalys = $this->messageAnalys($message);         
74                $case = $aryMOAnalys['case'];
75                $aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
76                if ($aryContents != null){
77                        unset($aryContents[0]);
78                        $aryTmp = array();                     
79                        foreach ($aryContents as $key => $val)
80                        {
81                                $aryTmp[] = trim($val);
82                        }
83                        $aryContents = $aryTmp;
84                }
85               
86                $this->load->model('frontend/user_model', 'objUserModel');
87               
88                switch ($case) {
89                        case _SBG_MO_REGISTER:
90                        case _SBG_MO_REGISTER_FOR:
91                                $userName = isset($aryContents[2]) ? $aryContents[2]:$sentNumber; 
92                                $collaborator = isset($aryContents[2]) ? $aryContents[0]:null;
93                                $smsReturn = $this->objUserModel->register($userName, $collaborator);
94                                break;
95                        case _SBG_MO_PHONENUM_CHANGE:
96                                $oldPhoneNumber = $aryContents[2];
97                                $password = $aryContents[3];
98                                $smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
99                                $this->sendMT($oldPhoneNumber, $smsReturn);
100                                break;
101                        case _SBG_MO_PASSWORD_RECOVERY:
102                        case _SBG_MO_PASSWORD_CHANGE:
103                                $password = isset($aryContents[2]) ? $aryContents[2] : null;
104                                $smsReturn = $this->objUserModel->changePassword($sentNumber, $password);
105                                break;                                         
106                        case _SBG_MO_VIP_REGISTER:
107                                $p_code = $aryContents[2];
108                                $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code);
109                                break;
110                        case _SBG_MO_PAY_TO_ACCOUNT:
111                        case _SBG_MO_PAY_TO_ACCOUNT_FOR:
112                                $userName = isset($aryContents[3]) ? $aryContents[3] : $sentNumber;
113                                $amount = $aryContents[2];
114                                $result =$this->chargeRootAccountProcess($sentNumber, $amount);
115                                if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
116                                        $aryPatterns = array ('/:sendid:/', '/:amount:/', '/:username:/');
117                                        $smsReturn = preg_replace($aryPatterns, array($sentNumber, $amount, $userName), _SBG_CHARGING_FAIL_NOT_ENOUGH_MSG);
118                                        $smsReturn = '0|'.$smsReturn;
119                                }
120                                else {
121                                        $smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
122                                }
123                                //write to paid log
124                                break;
125                        case _SBG_MO_SYNTAX_ERR:
126                        default:
127                                $smsReturn = '0|'._SBG_MO_SYNTAX_ERR_MSG;
128                                break;
129                }
130               
131                return $smsReturn;
132        }
133       
134        public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
135        {
136                $url = $this->config->item('topup_service_url');
137                $action = 'http://tempuri.org/ITopupService/TopUp';
138                $xml = $this->load->view('topup', $aryCardInfo, true);
139               
140                $client = new nusoap_client($url);
141                $client->useHTTPPersistentConnection();
142                $client->soap_defencoding = 'UTF-8';
143                $client->operation = $action;
144               
145                $result = $client->send($this->_xmlHeader.$xml, $operation);
146               
147                if ($client->fault) {
148                        return _SBG_CARD_PAY_FAIL;
149                }
150               
151                $aryTopupResult = $result['TopUpResult'];
152               
153                if ($aryTopupResult['Success'] == 'false') {
154                        return _SBG_CARD_PAY_FAIL;
155                }
156               
157                return _SBG_CARD_PAY_SUCCESS;
158        }
159       
160        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
161        {
162                $url = $this->config->item('charging_service_url');
163                $username = $this->config->item('charging_username');
164                $password = $this->config->item('charging_password');
165                $action = 'http://tempuri.org/processCharging';         
166               
167                $aryParams = array(
168                                'user' => $username,
169                                'pass' => $password,
170                                'MSISDN' => $sentID,
171                                'Charging' => $amount,
172                                'Contents' => $contents
173                );
174               
175                $xml = $this->load->view('charging', $aryParams, true);
176                $client = new nusoap_client($url);
177                $client->useHTTPPersistentConnection();
178                $client->soap_defencoding = 'UTF-8';
179                $client->operation = $action;
180                $result = $client->send($xml, $action);
181               
182                if ($client->fault) {
183                        return _SBG_CHARGING_FAIL;
184                }
185               
186                if ((int)$result['processChargingResult'] == _SBG_CHARGING_FAIL_NOT_ENOUGH)
187                        return _SBG_CHARGING_FAIL_NOT_ENOUGH;
188               
189                return _SBG_CHARGING_SUCCESS;
190        }
191       
192        public function sendMT ($receiverID, $message)
193        {
194                $aryMatch = array();
195                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
196                        $receiverID = '84'. $aryMatch[2];
197                }
198               
199                $username = $this->config->item('mt_username');
200                $password = $this->config->item('mt_password');
201                $action = 'http://tempuri.org/InsertMT';
202               
203                $aryParams = array(
204                                'User' => $username,
205                                'Pass' => $password,
206                                'CPCode' => _SBG_MT_CP_CODE,
207                                'UserID' => _SBG_MT_SEND_NUMBER,
208                                'RequestID' => '4',
209                                'ReceiverID' => $receiverID,
210                                'ServiceID' => _SBG_SERVICE_SERVICEID,
211                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
212                                'ContentType' => '0',
213                                'Info' => $message
214                );
215               
216                $xml = $this->load->view('mt', $aryParams, true);
217                $client = new nusoap_client($url);
218                $client->useHTTPPersistentConnection();
219                $client->soap_defencoding = 'UTF-8';
220                $client->operation = $action;
221                $result = $client->send($xml, $action);
222               
223                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
224                        return _SBG_SEND_MT_FAIL;
225                }
226               
227                return _SBG_SEND_MT_SUCCESS;
228        }
229}
Note: See TracBrowser for help on using the repository browser.