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

Last change on this file since 508 was 507, checked in by dungnv, 11 years ago
File size: 9.0 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                /*
137            ["BalanceAfterTransaction"]=>
138            string(1) "0"
139            ["ErrorCode"]=>
140            string(3) "199"
141            ["ErrorMessage"]=>
142            string(29) "L?i exception Payment service"
143            ["LastLockedOutDate"]=>
144            string(19) "0001-01-01T00:00:00"
145            ["RemainTimeToUnLock"]=>
146            string(1) "0"
147            ["Success"]=>
148            string(5) "false"
149            ["TransactionAmount"]=>
150            string(1) "0"
151            ["TransactionID"]=>
152            string(1) "0"
153            ["TransactionTime"]=>
154            string(19) "0001-01-01T00:00:00"
155        */
156                $url = $this->config->item('topup_service_url');
157                $action = 'http://tempuri.org/ITopupService/TopUp';
158                $xml = $this->load->view('topup', $aryCardInfo, true);
159               
160                $client = new nusoap_client($url);
161                $client->useHTTPPersistentConnection();
162                $client->soap_defencoding = 'UTF-8';
163                $client->operation = $action;
164               
165                $result = $client->send($this->_xmlHeader.$xml, $operation);
166               
167                if ($client->fault) {
168                        return _SBG_CARD_PAY_FAIL;
169                }
170               
171                $aryTopupResult = $result['TopUpResult'];
172               
173                if ($aryTopupResult['Success'] == 'false') {
174                        return _SBG_CARD_PAY_FAIL;
175                }
176               
177                return _SBG_CARD_PAY_SUCCESS;
178        }
179       
180        public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
181        {
182                $url = $this->config->item('charging_service_url');
183                $username = $this->config->item('charging_username');
184                $password = $this->config->item('charging_password');
185                $action = 'http://tempuri.org/processCharging';         
186               
187                $aryParams = array(
188                                'user' => $username,
189                                'pass' => $password,
190                                'MSISDN' => $sentID,
191                                'Charging' => $amount,
192                                'Contents' => $contents
193                );
194               
195                $xml = $this->load->view('charging', $aryParams, true);
196                $client = new nusoap_client($url);
197                $client->useHTTPPersistentConnection();
198                $client->soap_defencoding = 'UTF-8';
199                $client->operation = $action;
200                $result = $client->send($xml, $action);
201               
202                if ($client->fault) {
203                        return _SBG_CHARGING_FAIL;
204                }
205               
206                if ((int)$result['processChargingResult'] == _SBG_CHARGING_FAIL_NOT_ENOUGH)
207                        return _SBG_CHARGING_FAIL_NOT_ENOUGH;
208               
209                return _SBG_CHARGING_SUCCESS;
210        }
211       
212        public function sendMT ($receiverID, $message)
213        {
214                $aryMatch = array();
215                if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
216                        $receiverID = '84'. $aryMatch[2];
217                }
218               
219                $username = $this->config->item('mt_username');
220                $password = $this->config->item('mt_password');
221                $action = 'http://tempuri.org/InsertMT';
222               
223                $aryParams = array(
224                                'User' => $username,
225                                'Pass' => $password,
226                                'CPCode' => _SBG_MT_CP_CODE,
227                                'UserID' => _SBG_MT_SEND_NUMBER,
228                                'RequestID' => '4',
229                                'ReceiverID' => $receiverID,
230                                'ServiceID' => _SBG_SERVICE_SERVICEID,
231                                'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
232                                'ContentType' => '0',
233                                'Info' => $message
234                );
235               
236                $xml = $this->load->view('mt', $aryParams, true);
237                $client = new nusoap_client($url);
238                $client->useHTTPPersistentConnection();
239                $client->soap_defencoding = 'UTF-8';
240                $client->operation = $action;
241                $result = $client->send($xml, $action);
242               
243                if ($client->fault || (int)$result['InsertMTResult'] != 1) {
244                        return _SBG_SEND_MT_FAIL;
245                }
246               
247                return _SBG_SEND_MT_SUCCESS;
248        }
249}
Note: See TracBrowser for help on using the repository browser.