· 6 years ago · Aug 02, 2019, 01:12 PM
1<?php
2/**
3 * TrueWallet Class
4 *
5 * @category Payment Gateway
6 * @package php-truewallet-api
7 * @author Likecyber <cyber2friends@gmail.com>
8 * @copyright Copyright (c) 2018-2019
9 * @license https://creativecommons.org/licenses/by/4.0/ Attribution 4.0 International (CC BY 4.0)
10 * @link https://github.com/likecyber/php-truewallet-api
11 * @version Custom by Phumin Studio
12 **/
13class TrueWallet
14{
15 public $credentials = array();
16 public $access_token = null;
17 public $reference_token = null;
18
19 public $curl_options = array();
20
21 public $mobile_number = null;
22 public $otp_reference = null;
23
24 public $response = null;
25 public $http_code = null;
26
27 public $mobile_api_gateway = "https://srv1-ssl.demza.info/mobile-api-gateway/";
28 public $secret_key = "9LXAVCxcITaABNK48pAVgc4muuTNJ4enIKS5YzKyGZ";
29
30 public $device_id = ""; // Set device_id here
31 public $mobile_tracking = ""; // Set mobile_tracking here
32
33 public static function generate_identity($that = null)
34 {
35 $mobile_tracking = base64_encode(openssl_random_pseudo_bytes(40));
36 $device_id = substr(md5($mobile_tracking), 16);
37 if ($that !== null) {
38 $that->mobile_tracking = $mobile_tracking;
39 $that->device_id = $device_id;
40 }
41 return array($device_id, $mobile_tracking);
42 }
43
44 public function __construct($username = null, $password = null, $options = array())
45 {
46 $this->device_id = isset($options['device_id']) ? $options['device_id'] : null;
47 $this->mobile_tracking = isset($options['mobile_tracking']) ? $options['mobile_tracking'] : null;
48 $reference_token = isset($options['reference_token']) ? $options['reference_token'] : null;
49
50 if (!is_null($username) && !is_null($password)) {
51 $this->setCredentials($username, $password, $reference_token);
52 } elseif (!is_null($username)) {
53 $this->setAccessToken($username);
54 }
55 }
56
57 public function setCredentials($username, $password, $reference_token = null, $type = null)
58 {
59 if (is_null($type)) $type = filter_var($username, FILTER_VALIDATE_EMAIL) ? "email" : "mobile";
60 $this->credentials["username"] = strval($username);
61 $this->credentials["password"] = strval($password);
62 $this->credentials["type"] = strval($type);
63 $this->setAccessToken(null);
64 if(!is_null($reference_token)) $this->setReferenceToken($reference_token);
65 }
66 public function setAccessToken($access_token)
67 {
68 $this->access_token = is_null($access_token) ? null : strval($access_token);
69 }
70 public function setReferenceToken($reference_token)
71 {
72 $this->reference_token = is_null($reference_token) ? null : strval($reference_token);
73 }
74 public function request($api_path, $headers = array(), $data = null)
75 {
76 $handle = curl_init($this->mobile_api_gateway . ltrim($api_path, "/"));
77 if (!is_null($data)) {
78 curl_setopt_array($handle, array(
79 CURLOPT_POST => true,
80 CURLOPT_POSTFIELDS => is_array($data) ? json_encode($data) : $data
81 ));
82 if (is_array($data)) $headers = array_merge(array("Content-Type" => "application/json"), $headers);
83 }
84 curl_setopt_array($handle, array(
85 CURLOPT_RETURNTRANSFER => true,
86 CURLOPT_USERAGENT => "okhttp/3.8.0",
87 CURLOPT_HTTPHEADER => $this->buildHeaders($headers)
88 ));
89 if (is_array($this->curl_options)) curl_setopt_array($handle, $this->curl_options);
90 $this->response = curl_exec($handle);
91 $this->http_code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
92 if ($result = json_decode($this->response, true)) return $result;
93 return $this->response;
94 }
95 public function buildHeaders($array)
96 {
97 $headers = array();
98 foreach ($array as $key => $value) {
99 $headers[] = $key . ": " . $value;
100 }
101 return $headers;
102 }
103 public function getTimestamp()
104 {
105 return strval(floor(microtime(true) * 1000));
106 }
107 public function RequestLoginOTP()
108 {
109 if (!isset($this->credentials["username"]) || !isset($this->credentials["password"]) || !isset($this->credentials["type"])) return false;
110 $timestamp = $this->getTimestamp();
111 $result = $this->request("/api/v1/login/otp/", array(
112 "username" => $this->credentials["username"],
113 "password" => sha1($this->credentials["username"] . $this->credentials["password"])
114 ), array(
115 "type" => $this->credentials["type"],
116 "device_id" => $this->device_id,
117 "timestamp" => $timestamp,
118 "signature" => hash_hmac("sha1", implode("|", array($this->credentials["type"], $this->device_id, $timestamp)), $this->secret_key)
119 ));
120 if (isset($result["data"]["mobile_number"]) && isset($result["data"]["otp_reference"])) {
121 $this->mobile_number = $result["data"]["mobile_number"];
122 $this->otp_reference = $result["data"]["otp_reference"];
123 }
124 return $result;
125 }
126 public function SubmitLoginOTP($otp_code, $mobile_number = null, $otp_reference = null)
127 {
128 if (is_null($mobile_number) && !is_null($this->mobile_number)) $mobile_number = $this->mobile_number;
129 if (is_null($otp_reference) && !is_null($this->otp_reference)) $otp_reference = $this->otp_reference;
130 if (is_null($mobile_number) || is_null($otp_reference)) return false;
131 $timestamp = $this->getTimestamp();
132 $result = $this->request("/api/v1/login/otp/verification/", array(
133 "username" => $this->credentials["username"],
134 "password" => sha1($this->credentials["username"] . $this->credentials["password"])
135 ), array(
136 "type" => $this->credentials["type"],
137 "otp_code" => strval($otp_code),
138 "mobile_number" => strval($mobile_number),
139 "otp_reference" => strval($otp_reference),
140 "device_id" => $this->device_id,
141 "mobile_tracking" => $this->mobile_tracking,
142 "timestamp" => $timestamp,
143 "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)
144 ));
145 if (isset($result["data"]["access_token"])) $this->setAccessToken($result["data"]["access_token"]);
146 if (isset($result["data"]["reference_token"])) $this->setReferenceToken($result["data"]["reference_token"]);
147 return $result;
148 }
149 public function Login()
150 {
151 if (!isset($this->credentials["username"]) || !isset($this->credentials["password"]) || !isset($this->credentials["type"]) || is_null($this->reference_token)) return false;
152 $timestamp = $this->getTimestamp();
153 $result = $this->request("/api/v1/login/", array(
154 "username" => $this->credentials["username"],
155 "password" => sha1($this->credentials["username"] . $this->credentials["password"])
156 ), array(
157 "type" => $this->credentials["type"],
158 "reference_token" => $this->reference_token,
159 "device_id" => $this->device_id,
160 "mobile_tracking" => $this->mobile_tracking,
161 "timestamp" => $timestamp,
162 "signature" => hash_hmac("sha1", implode("|", array($this->credentials["type"], $this->reference_token, $this->device_id, $this->mobile_tracking, $timestamp)), $this->secret_key)
163 ));
164 if (isset($result["data"]["access_token"])) $this->setAccessToken($result["data"]["access_token"]);
165 return $result;
166 }
167 public function Logout()
168 {
169 if (is_null($this->access_token)) return false;
170 return $this->request("/api/v1/signout/" . $this->access_token, array(), "");
171 }
172 public function GetProfile()
173 {
174 if (is_null($this->access_token)) return false;
175 return $this->request("/api/v1/profile/" . $this->access_token);
176 }
177 public function GetBalance()
178 {
179 if (is_null($this->access_token)) return false;
180 return $this->request("/api/v1/profile/balance/" . $this->access_token);
181 }
182 public function GetTransaction($limit = 50, $start_date = null, $end_date = null)
183 {
184 if (is_null($this->access_token)) return false;
185 if (is_null($start_date) && is_null($end_date)) $start_date = date("Y-m-d", strtotime("-30 days") - date("Z") + 25200);
186 if (is_null($end_date)) $end_date = date("Y-m-d", strtotime("+1 day") - date("Z") + 25200);
187 if (is_null($start_date) || is_null($end_date)) return false;
188 return $this->request("/user-profile-composite/v1/users/transactions/history?start_date=" . strval($start_date) . "&end_date=" . strval($end_date) . "&limit=" . intval($limit), array(
189 "Authorization" => $this->access_token
190 ));
191 }
192 public function GetTransactionReport($report_id)
193 {
194 if (is_null($this->access_token)) return false;
195 return $this->request("/user-profile-composite/v1/users/transactions/history/detail/" . intval($report_id), array(
196 "Authorization" => $this->access_token
197 ));
198 }
199 public function TopupCashcard($cashcard)
200 {
201 if (is_null($this->access_token)) return false;
202 return $this->request("/api/v1/topup/mobile/" . time() . "/" . $this->access_token . "/cashcard/" . strval($cashcard), array(), "");
203 }
204 public function KycProfile() {
205 if (is_null($this->access_token)) return false;
206 return $this->request("/user-profile-composite/v1/users/kyc-profiles", array(
207 "Authorization" => $this->access_token,
208 ));
209 }
210}