1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
---|
2 |
|
---|
3 | if ( ! defined('_SBG_MO_REGISTER')) define('_SBG_MO_REGISTER', 0);
|
---|
4 | if ( ! defined('_SBG_MO_REGISTER_FOR')) define('_SBG_MO_REGISTER_FOR', 1);
|
---|
5 | if ( ! defined('_SBG_MO_PHONENUM_CHANGE')) define('_SBG_MO_PHONENUM_CHANGE', 2);
|
---|
6 | if ( ! defined('_SBG_MO_PASSWORD_RECOVERY')) define('_SBG_MO_PASSWORD_RECOVERY', 3);
|
---|
7 | if ( ! defined('_SBG_MO_PASSWORD_CHANGE')) define('_SBG_MO_PASSWORD_CHANGE', 4);
|
---|
8 | if ( ! defined('_SBG_MO_VIP_REGISTER')) define('_SBG_MO_VIP_REGISTER', 5);
|
---|
9 | if ( ! defined('_SBG_MO_PAY_TO_ACCOUNT')) define('_SBG_MO_PAY_TO_ACCOUNT', 6);
|
---|
10 | if ( ! defined('_SBG_MO_PAY_TO_ACCOUNT_FOR')) define('_SBG_MO_PAY_TO_ACCOUNT_FOR', 7);
|
---|
11 |
|
---|
12 | if ( ! defined('_SBG_MO_SYNTAX_ERR')) define('_SBG_MO_SYNTAX_ERR', -1);
|
---|
13 | if ( ! defined('_SBG_MO_SYNTAX_ERR_MSG')) define('_SBG_MO_SYNTAX_ERR_MSG', 'soanbaigiang.smas.vn khong co dich vu nay!');
|
---|
14 |
|
---|
15 | if ( ! defined('_SBG_CARD_PAY_FAIL')) define('_SBG_CARD_PAY_FAIL', -1);
|
---|
16 | if ( ! defined('_SBG_CARD_PAY_SUCCESS')) define('_SBG_CARD_PAY_SUCCESS', 1);
|
---|
17 |
|
---|
18 | if ( ! defined('_SBG_CHARGING_FAIL')) define('_SBG_CHARGING_FAIL', -1);
|
---|
19 | if ( ! defined('_SBG_CHARGING_SUCCESS')) define('_SBG_CHARGING_SUCCESS', 1);
|
---|
20 |
|
---|
21 | if ( ! defined('_SBG_CHARGING_FAIL_NOT_ENOUGH')) define('_SBG_CHARGING_FAIL_NOT_ENOUGH', 401);
|
---|
22 | if ( ! 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 |
|
---|
24 | if ( ! defined('_SBG_SEND_MT_FAIL')) define('_SBG_SEND_MT_FAIL', -1);
|
---|
25 | if ( ! defined('_SBG_SEND_MT_SUCCESS')) define('_SBG_SEND_MT_SUCCESS', 1);
|
---|
26 |
|
---|
27 | if ( ! defined('_SBG_MT_SEND_NUMBER')) define('_SBG_MT_SEND_NUMBER', '841662860000');
|
---|
28 | if ( ! defined('_SBG_MT_CP_CODE')) define('_SBG_MT_CP_CODE', '601');
|
---|
29 |
|
---|
30 | if ( ! defined('_SBG_SERVICE_COMMAND_CODE')) define('_SBG_SERVICE_COMMAND_CODE', 'SBG');
|
---|
31 | if ( ! defined('_SBG_SERVICE_SERVICEID')) define('_SBG_SERVICE_SERVICEID', '8062');
|
---|
32 |
|
---|
33 | Class 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 | $debugMsg = $message."\n";
|
---|
68 |
|
---|
69 | $aryMatch = array();
|
---|
70 | if (preg_match('/^(84)([0-9]{9,10})$/', $sentNumber, $aryMatch) == 1) {
|
---|
71 | $sentNumber = '0'. $aryMatch[2];
|
---|
72 | }
|
---|
73 |
|
---|
74 | $smsReturn = '';
|
---|
75 | $aryMOAnalys = $this->messageAnalys($message);
|
---|
76 | $case = $aryMOAnalys['case'];
|
---|
77 | $aryContents = isset($aryMOAnalys['content']) ? $aryMOAnalys['content']: null;
|
---|
78 | if ($aryContents != null){
|
---|
79 | unset($aryContents[0]);
|
---|
80 | $aryTmp = array();
|
---|
81 | foreach ($aryContents as $key => $val)
|
---|
82 | {
|
---|
83 | $aryTmp[] = trim($val);
|
---|
84 | }
|
---|
85 | $aryContents = $aryTmp;
|
---|
86 | }
|
---|
87 |
|
---|
88 | $this->load->model('frontend/user_model', 'objUserModel');
|
---|
89 | $debugMsg .= $case;
|
---|
90 | write_file('mt_log', $debugMsg);
|
---|
91 | switch ($case) {
|
---|
92 | case _SBG_MO_REGISTER:
|
---|
93 | case _SBG_MO_REGISTER_FOR:
|
---|
94 | $userName = isset($aryContents[2]) ? $aryContents[2]:$sentNumber;
|
---|
95 | $collaborator = isset($aryContents[2]) ? $aryContents[0]:null;
|
---|
96 | $smsReturn = $this->objUserModel->register($userName, $collaborator);
|
---|
97 | break;
|
---|
98 | case _SBG_MO_PHONENUM_CHANGE:
|
---|
99 | $oldPhoneNumber = $aryContents[2];
|
---|
100 | $password = $aryContents[3];
|
---|
101 | $smsReturn = $this->objUserModel->changeProfile($sentNumber, $oldPhoneNumber, $password);
|
---|
102 | $sendMT = $this->sendMT($oldPhoneNumber, $smsReturn);
|
---|
103 | $smsReturn .= ' - SendMT: ';
|
---|
104 | break;
|
---|
105 | case _SBG_MO_PASSWORD_RECOVERY:
|
---|
106 | case _SBG_MO_PASSWORD_CHANGE:
|
---|
107 | $password = isset($aryContents[2]) ? $aryContents[2] : null;
|
---|
108 | $smsReturn = $this->objUserModel->changePassword($sentNumber, $password);
|
---|
109 | break;
|
---|
110 | case _SBG_MO_VIP_REGISTER:
|
---|
111 | $p_code = $aryContents[2];
|
---|
112 | $smsReturn = $this->objUserModel->registerVip($sentNumber, $p_code);
|
---|
113 | break;
|
---|
114 | case _SBG_MO_PAY_TO_ACCOUNT:
|
---|
115 | case _SBG_MO_PAY_TO_ACCOUNT_FOR:
|
---|
116 | $userName = isset($aryContents[3]) ? $aryContents[3] : $sentNumber;
|
---|
117 | $amount = $aryContents[2];
|
---|
118 | $result =$this->chargeRootAccountProcess($sentNumber, $amount);
|
---|
119 | if ($result == _SBG_CHARGING_FAIL_NOT_ENOUGH) {
|
---|
120 | $aryPatterns = array ('/:sendid:/', '/:amount:/', '/:username:/');
|
---|
121 | $smsReturn = preg_replace($aryPatterns, array($sentNumber, $amount, $userName), _SBG_CHARGING_FAIL_NOT_ENOUGH_MSG);
|
---|
122 | $smsReturn = '0|'.$smsReturn;
|
---|
123 | }
|
---|
124 | else {
|
---|
125 | $smsReturn = $this->objUserModel->rechargeAccount($userName, $amount);
|
---|
126 | }
|
---|
127 | //write to paid log
|
---|
128 | break;
|
---|
129 | case _SBG_MO_SYNTAX_ERR:
|
---|
130 | default:
|
---|
131 | $smsReturn = '0|'._SBG_MO_SYNTAX_ERR_MSG;
|
---|
132 | break;
|
---|
133 | }
|
---|
134 |
|
---|
135 | return $smsReturn;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public function cardPaidProcess ($aryCardInfo, &$aryTopupResult = array())
|
---|
139 | {
|
---|
140 | /*
|
---|
141 | ["BalanceAfterTransaction"]=>
|
---|
142 | string(1) "0"
|
---|
143 | ["ErrorCode"]=>
|
---|
144 | string(3) "199"
|
---|
145 | ["ErrorMessage"]=>
|
---|
146 | string(29) "L?i exception Payment service"
|
---|
147 | ["LastLockedOutDate"]=>
|
---|
148 | string(19) "0001-01-01T00:00:00"
|
---|
149 | ["RemainTimeToUnLock"]=>
|
---|
150 | string(1) "0"
|
---|
151 | ["Success"]=>
|
---|
152 | string(5) "false"
|
---|
153 | ["TransactionAmount"]=>
|
---|
154 | string(1) "0"
|
---|
155 | ["TransactionID"]=>
|
---|
156 | string(1) "0"
|
---|
157 | ["TransactionTime"]=>
|
---|
158 | string(19) "0001-01-01T00:00:00"
|
---|
159 | */
|
---|
160 | $url = $this->config->item('topup_service_url');
|
---|
161 | $action = 'http://tempuri.org/ITopupService/TopUp';
|
---|
162 | $xml = $this->load->view('topup', $aryCardInfo, true);
|
---|
163 |
|
---|
164 | $client = new nusoap_client($url);
|
---|
165 | $client->useHTTPPersistentConnection();
|
---|
166 | $client->soap_defencoding = 'UTF-8';
|
---|
167 | $client->operation = $action;
|
---|
168 |
|
---|
169 | $result = $client->send($xml, $operation);
|
---|
170 |
|
---|
171 | if ($client->fault) {
|
---|
172 | return _SBG_CARD_PAY_FAIL;
|
---|
173 | }
|
---|
174 |
|
---|
175 | $aryTopupResult = $result['TopUpResult'];
|
---|
176 |
|
---|
177 | if ($aryTopupResult['Success'] == 'false') {
|
---|
178 | return _SBG_CARD_PAY_FAIL;
|
---|
179 | }
|
---|
180 |
|
---|
181 | return _SBG_CARD_PAY_SUCCESS;
|
---|
182 | }
|
---|
183 |
|
---|
184 | public function chargeRootAccountProcess ($sentID, $amount = 0, $contents = '')
|
---|
185 | {
|
---|
186 | $url = $this->config->item('charging_service_url');
|
---|
187 | $username = $this->config->item('charging_username');
|
---|
188 | $password = $this->config->item('charging_password');
|
---|
189 | $action = 'http://tempuri.org/processCharging';
|
---|
190 |
|
---|
191 | $aryParams = array(
|
---|
192 | 'user' => $username,
|
---|
193 | 'pass' => $password,
|
---|
194 | 'MSISDN' => $sentID,
|
---|
195 | 'Charging' => $amount,
|
---|
196 | 'Contents' => $contents
|
---|
197 | );
|
---|
198 |
|
---|
199 | $xml = $this->load->view('charging', $aryParams, true);
|
---|
200 | $client = new nusoap_client($url);
|
---|
201 | $client->useHTTPPersistentConnection();
|
---|
202 | $client->soap_defencoding = 'UTF-8';
|
---|
203 | $client->operation = $action;
|
---|
204 | $result = $client->send($xml, $action);
|
---|
205 |
|
---|
206 | if ($client->fault) {
|
---|
207 | return _SBG_CHARGING_FAIL;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if ((int)$result['processChargingResult'] == _SBG_CHARGING_FAIL_NOT_ENOUGH)
|
---|
211 | return _SBG_CHARGING_FAIL_NOT_ENOUGH;
|
---|
212 |
|
---|
213 | return _SBG_CHARGING_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 | public function sendMT ($receiverID, $message)
|
---|
217 | {
|
---|
218 | $aryMatch = array();
|
---|
219 | if (preg_match('/^(0)([0-9]{9,10})$/', $receiverID, $aryMatch) == 1) {
|
---|
220 | $receiverID = '84'. $aryMatch[2];
|
---|
221 | }
|
---|
222 |
|
---|
223 | $username = $this->config->item('mt_username');
|
---|
224 | $password = $this->config->item('mt_password');
|
---|
225 | $action = 'http://tempuri.org/InsertMT';
|
---|
226 |
|
---|
227 | $aryParams = array(
|
---|
228 | 'User' => $username,
|
---|
229 | 'Pass' => $password,
|
---|
230 | 'CPCode' => _SBG_MT_CP_CODE,
|
---|
231 | 'UserID' => _SBG_MT_SEND_NUMBER,
|
---|
232 | 'RequestID' => '4',
|
---|
233 | 'ReceiverID' => $receiverID,
|
---|
234 | 'ServiceID' => _SBG_SERVICE_SERVICEID,
|
---|
235 | 'CommandCode' => _SBG_SERVICE_COMMAND_CODE,
|
---|
236 | 'ContentType' => '0',
|
---|
237 | 'Info' => $message
|
---|
238 | );
|
---|
239 |
|
---|
240 | $xml = $this->load->view('mt', $aryParams, true);
|
---|
241 | $url = $this->config->item('mt_service_url');
|
---|
242 | $client = new nusoap_client($url);
|
---|
243 | $client->useHTTPPersistentConnection();
|
---|
244 | $client->soap_defencoding = 'UTF-8';
|
---|
245 | $client->operation = $action;
|
---|
246 | $result = $client->send($xml, $action);
|
---|
247 |
|
---|
248 | //write_file('mt_log', var_export($client->fault, true)."\n".var_export($xml, true));
|
---|
249 |
|
---|
250 | if ($client->fault || (int)$result['InsertMTResult'] != 1) {
|
---|
251 | return _SBG_SEND_MT_FAIL;
|
---|
252 | }
|
---|
253 |
|
---|
254 | return _SBG_SEND_MT_SUCCESS;
|
---|
255 | }
|
---|
256 | } |
---|