· 5 years ago · Dec 10, 2019, 09:04 AM
1<?php
2
3
4class TrueWallet {
5 public $credentials = array();
6 public $access_token = null;
7 public $reference_token = null;
8
9 public $curl_options = null;
10
11 public $data = null;
12
13 public $response = null;
14 public $http_code = null;
15
16 public $mobile_api_gateway = "https://mobile-api-gateway.truemoney.com/mobile-api-gateway/";
17 public $secret_key = "9LXAVCxcITaABNK48pAVgc4muuTNJ4enIKS5YzKyGZ";
18
19 public $device_id = ""; // Set device_id here
20 public $mobile_tracking = ""; // Set mobile_tracking here
21
22 public function generate_identity () {
23 $this->mobile_tracking = base64_encode(openssl_random_pseudo_bytes(40));
24 $this->device_id = substr(md5($this->mobile_tracking), 16);
25 return implode("|", array($this->device_id, $this->mobile_tracking));
26 }
27
28 public function __construct ($username = null, $password = null, $reference_token = null) {
29 if (empty($this->device_id) || empty($this->mobile_tracking)) {
30 $identity_file = dirname(__FILE__)."/".basename(__FILE__, ".php").".identity";
31 if (file_exists($identity_file)) {
32 list($this->device_id, $this->mobile_tracking) = explode("|", file_get_contents($identity_file));
33 } else {
34 file_put_contents($identity_file, $this->generate_identity());
35 }
36 }
37 if (!is_null($username) && !is_null($password)) {
38 $this->setCredentials($username, $password, $reference_token);
39 } elseif (!is_null($username)) {
40 $this->setAccessToken($username);
41 }
42 }
43
44 public function setCredentials ($username, $password, $reference_token = null, $type = null) {
45 if (is_null($type)) $type = filter_var($username, FILTER_VALIDATE_EMAIL) ? "email" : "mobile";
46 $this->credentials["username"] = strval($username);
47 $this->credentials["password"] = strval($password);
48 $this->credentials["type"] = strval($type);
49 $this->setAccessToken(null);
50 $this->setReferenceToken($reference_token);
51 }
52
53 public function setAccessToken ($access_token) {
54 $this->access_token = is_null($access_token) ? null : strval($access_token);
55 }
56
57 public function setReferenceToken ($reference_token) {
58 $this->reference_token = is_null($reference_token) ? null : strval($reference_token);
59 }
60
61 public function request ($method, $endpoint, $headers = array(), $data = null) {
62 $this->data = null;
63 $handle = curl_init();
64 if (!is_null($data)) {
65 curl_setopt($handle, CURLOPT_POSTFIELDS, is_array($data) ? json_encode($data) : $data);
66 if (is_array($data)) $headers = array_merge(array("Content-Type" => "application/json"), $headers);
67 }
68 curl_setopt_array($handle, array(
69 CURLOPT_URL => $this->mobile_api_gateway.ltrim($endpoint, "/"),
70 CURLOPT_CUSTOMREQUEST => $method,
71 CURLOPT_RETURNTRANSFER => true,
72 CURLOPT_USERAGENT => "okhttp/3.8.0",
73 CURLOPT_HTTPHEADER => $this->buildHeaders($headers)
74 ));
75 if (is_array($this->curl_options)) curl_setopt_array($handle, $this->curl_options);
76 $this->response = curl_exec($handle);
77 $this->http_code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
78 if ($result = json_decode($this->response, true)) {
79 if (isset($result["data"])) $this->data = $result["data"];
80 return $result;
81 }
82 return $this->response;
83 }
84
85 public function buildHeaders ($array) {
86 $headers = array();
87 foreach ($array as $key => $value) {
88 $headers[] = $key.": ".$value;
89 }
90 return $headers;
91 }
92
93 public function getTimestamp() {
94 return strval(floor(microtime(true)*1000));
95 }
96
97 public function RequestLoginOTP () {
98 if (!isset($this->credentials["username"]) || !isset($this->credentials["password"]) || !isset($this->credentials["type"])) return false;
99 $timestamp = $this->getTimestamp();
100 $result = $this->request("POST", "/api/v1/login/otp/", array(
101 "username" => $this->credentials["username"],
102 "password" => sha1($this->credentials["username"].$this->credentials["password"])
103 ), array(
104 "type" => $this->credentials["type"],
105 "device_id" => $this->device_id,
106 "timestamp" => $timestamp,
107 "signature" => hash_hmac("sha1", implode("|", array($this->credentials["type"], $this->device_id, $timestamp)), $this->secret_key)
108 ));
109 return $result;
110 }
111
112 public function SubmitLoginOTP ($otp_code, $mobile_number = null, $otp_reference = null) {
113 if (is_null($mobile_number) && isset($this->data["mobile_number"])) $mobile_number = $this->data["mobile_number"];
114 if (is_null($otp_reference) && isset($this->data["otp_reference"])) $otp_reference = $this->data["otp_reference"];
115 if (is_null($mobile_number) || is_null($otp_reference)) return false;
116 $timestamp = $this->getTimestamp();
117 $result = $this->request("POST", "/api/v1/login/otp/verification/", array(
118 "username" => $this->credentials["username"],
119 "password" => sha1($this->credentials["username"].$this->credentials["password"])
120 ), array(
121 "type" => $this->credentials["type"],
122 "otp_code" => strval($otp_code),
123 "mobile_number" => strval($mobile_number),
124 "otp_reference" => strval($otp_reference),
125 "device_id" => $this->device_id,
126 "mobile_tracking" => $this->mobile_tracking,
127 "timestamp" => $timestamp,
128 "signature" => hash_hmac("sha1", implode("|", array($this->credentials["type"], strval($otp_code), strval($mobile_number), strval($otp_reference), $this->device_id, $this->mobile_tracking, $timestamp)), $this->secret_key)
129 ));
130 if (isset($result["data"]["access_token"])) $this->setAccessToken($result["data"]["access_token"]);
131 if (isset($result["data"]["reference_token"])) $this->setReferenceToken($result["data"]["reference_token"]);
132 return $result;
133 }
134
135 public function Login () {
136 if (!isset($this->credentials["username"]) || !isset($this->credentials["password"]) || !isset($this->credentials["type"]) || is_null($this->reference_token)) return false;
137 $timestamp = $this->getTimestamp();
138 $result = $this->request("POST", "/api/v1/login/", array(
139 "username" => $this->credentials["username"],
140 "password" => sha1($this->credentials["username"].$this->credentials["password"])
141 ), array(
142 "type" => $this->credentials["type"],
143 "reference_token" => $this->reference_token,
144 "device_id" => $this->device_id,
145 "mobile_tracking" => $this->mobile_tracking,
146 "timestamp" => $timestamp,
147 "signature" => hash_hmac("sha1", implode("|", array($this->credentials["type"], $this->reference_token, $this->device_id, $this->mobile_tracking, $timestamp)), $this->secret_key)
148 ));
149 if (isset($result["data"]["access_token"])) $this->setAccessToken($result["data"]["access_token"]);
150 return $result;
151 }
152
153 public function Logout () {
154 if (is_null($this->access_token)) return false;
155 return $this->request("POST", "/api/v1/signout/".$this->access_token);
156 }
157
158 public function GetProfile () {
159 if (is_null($this->access_token)) return false;
160 return $this->request("GET", "/user-profile-composite/v1/users/", array(
161 "Authorization" => $this->access_token
162 ));
163 }
164
165 public function GetBalance () {
166 if (is_null($this->access_token)) return false;
167 return $this->request("GET", "/user-profile-composite/v1/users/balance/", array(
168 "Authorization" => $this->access_token
169 ));
170 }
171
172 public function GetTransaction ($limit = 50, $start_date = null, $end_date = null) {
173 if (is_null($this->access_token)) return false;
174 if (is_null($start_date) && is_null($end_date)) $start_date = date("Y-m-d", strtotime("-30 days") - date("Z") + 25200);
175 if (is_null($end_date)) $end_date = date("Y-m-d", strtotime("+1 day") - date("Z") + 25200);
176 if (is_null($start_date) || is_null($end_date)) return false;
177 return $this->request("GET", "/user-profile-composite/v1/users/transactions/history/?".http_build_query(array(
178 "start_date" => strval($start_date),
179 "end_date" => strval($end_date),
180 "limit" => intval($limit)
181 )), array(
182 "Authorization" => $this->access_token
183 ));
184 }
185
186 public function GetTransactionReport ($report_id) {
187 if (is_null($this->access_token)) return false;
188 return $this->request("GET", "/user-profile-composite/v1/users/transactions/history/detail/".intval($report_id), array(
189 "Authorization" => $this->access_token
190 ));
191 }
192
193 public function TopupCashcard ($cashcard) {
194 if (is_null($this->access_token)) return false;
195 return $this->request("POST", "/api/v1/topup/mobile/".time()."/".$this->access_token."/cashcard/".strval($cashcard));
196 }
197
198 public function DraftTransferP2P ($mobile_number, $amount) {
199 if (is_null($this->access_token)) return false;
200 $timestamp = $this->getTimestamp();
201 return $this->request("POST", "/transfer-composite/v1/p2p-transfer/draft-transactions/", array(
202 "Authorization" => $this->access_token
203 ), array(
204 "amount" => number_format(str_replace(",", "", strval($amount)), 2, ".", ""),
205 "mobileNumber" => str_replace(array("-", " "), "", strval($mobile_number)),
206 "timestamp" => $timestamp,
207 "signature" => hash_hmac("sha1", implode("|", array(number_format(str_replace(",", "", strval($amount)), 2, ".", ""), str_replace(array("-", " "), "", strval($mobile_number)), $timestamp)), $this->secret_key)
208 ));
209 }
210
211 public function ConfirmTransferP2P ($personal_message = "", $wait_processing = true, $draft_transaction_id = null, $reference_key = null) {
212 if (is_null($this->access_token)) return false;
213 if (is_null($draft_transaction_id) && isset($this->data["draft_transaction_id"])) $draft_transaction_id = $this->data["draft_transaction_id"];
214 if (is_null($reference_key) && isset($this->data["reference_key"])) $reference_key = $this->data["reference_key"];
215 if (is_null($draft_transaction_id) || is_null($reference_key)) return false;
216 $timestamp = $this->getTimestamp();
217 $result = $this->request("PUT", "/transfer-composite/v1/p2p-transfer/draft-transactions/".$draft_transaction_id, array(
218 "Authorization" => $this->access_token
219 ), array(
220 "personal_message" => strval($personal_message),
221 "timestamp" => $timestamp,
222 "signature" => hash_hmac("sha1", implode("|", array(strval($personal_message), $timestamp)), $this->secret_key)
223 ));
224 if (isset($result["data"]["transaction_id"])) {
225 $transaction_id = $result["data"]["transaction_id"];
226 $timestamp = $this->getTimestamp();
227 $result = $this->request("POST", "/transfer-composite/v1/p2p-transfer/transactions/".$transaction_id."/", array(
228 "Authorization" => $this->access_token
229 ), array(
230 "reference_key" => strval($reference_key),
231 "timestamp" => $timestamp,
232 "signature" => hash_hmac("sha1", implode("|", array(strval($reference_key), $timestamp)), $this->secret_key)
233 ));
234 if ($wait_processing) {
235 for ($i = 0; $i < 10; $i++) {
236 if (isset($result["data"]["transfer_status"])) {
237 if ($result["data"]["transfer_status"] === "PROCESSING") {
238 if ($i > 0) sleep(1);
239 $result = $this->request("GET", "/transfer-composite/v1/p2p-transfer/transactions/".$transaction_id."/status/", array(
240 "Authorization" => $this->access_token
241 ));
242 } else {
243 break;
244 }
245 } else {
246 break;
247 }
248 }
249 }
250 if (isset($result["data"]["transfer_status"])) {
251 $this->data["transaction_id"] = $transaction_id;
252 }
253 }
254 return $result;
255 }
256
257 public function GetDetailTransferP2P ($transaction_id = null) {
258 if (is_null($this->access_token)) return false;
259 if (is_null($transaction_id) && isset($this->data["transaction_id"])) $transaction_id = $this->data["transaction_id"];
260 if (is_null($transaction_id)) return false;
261 $timestamp = $this->getTimestamp();
262 return $this->request("GET", "/transfer-composite/v1/p2p-transfer/transactions/".$transaction_id."/detail/", array(
263 "Authorization" => $this->access_token
264 ));
265 }
266
267 public function DraftBuyCashcard ($amount, $mobile_number) {
268 if (is_null($this->access_token)) return false;
269 $timestamp = $this->getTimestamp();
270 return $this->request("POST", "/api/v1/buy/e-pin/draft/verifyAndCreate/".$this->access_token, array(), array(
271 "amount" => str_replace(",", "", strval($amount)),
272 "recipientMobileNumber" => str_replace(array("-", " "), "", strval($mobile_number)),
273 "timestamp" => $timestamp,
274 "signature" => hash_hmac("sha1", implode("|", array(str_replace(",", "", strval($amount)), str_replace(array("-", " "), "", strval($mobile_number)), $timestamp)), $this->secret_key)
275 ));
276 }
277
278 public function ConfirmBuyCashcard ($otp_code, $wait_processing = true, $draft_transaction_id = null, $mobile_number = null, $otp_reference = null) {
279 if (is_null($this->access_token)) return false;
280 if (is_null($draft_transaction_id) && isset($this->data["draftTransactionID"])) $draft_transaction_id = $this->data["draftTransactionID"];
281 if (is_null($mobile_number) && isset($this->data["mobileNumber"])) $mobile_number = $this->data["mobileNumber"];
282 if (is_null($otp_reference) && isset($this->data["otpRefCode"])) $otp_reference = $this->data["otpRefCode"];
283 if (is_null($draft_transaction_id) || is_null($mobile_number) || is_null($otp_reference)) return false;
284 $timestamp = $this->getTimestamp();
285 $result = $this->request("PUT", "/api/v1/buy/e-pin/confirm/".$draft_transaction_id."/".$this->access_token, array(), array(
286 "mobileNumber" => str_replace(array("-", " "), "", strval($mobile_number)),
287 "otpRefCode" => strval($otp_reference),
288 "otpString" => strval($otp_code),
289 "timestamp" => $timestamp,
290 "signature" => hash_hmac("sha1", implode("|", array(str_replace(array("-", " "), "", strval($mobile_number)), strval($otp_reference), strval($otp_code), $timestamp)), $this->secret_key)
291 ));
292 if (isset($result["data"]["status"]) && $result["data"]["status"] === "VERIFIED") {
293 $transaction_id = $draft_transaction_id;
294 if ($wait_processing) {
295 for ($i = 0; $i < 10; $i++) {
296 if (isset($result["data"]["status"])) {
297 if ($result["data"]["status"] === "VERIFIED" || $result["data"]["status"] === "PROCESSING") {
298 if ($i > 0) sleep(1);
299 $result = $this->request("GET", "/api/v1/buy/e-pin/".$transaction_id."/status/".$this->access_token);
300 } else {
301 break;
302 }
303 } else {
304 break;
305 }
306 }
307 }
308 if (isset($result["data"]["status"])) {
309 $this->data["transaction_id"] = $transaction_id;
310 }
311 }
312 return $result;
313 }
314
315 public function GetDetailBuyCashcard ($transaction_id = null) {
316 if (is_null($this->access_token)) return false;
317 if (is_null($transaction_id) && isset($this->data["transaction_id"])) $transaction_id = $this->data["transaction_id"];
318 if (is_null($transaction_id)) return false;
319 $timestamp = $this->getTimestamp();
320 return $this->request("GET", "/api/v1/buy/e-pin/".$transaction_id."/details/".$this->access_token);
321 }
322}
323
324?>