Advertisement
Guest User

Untitled

a guest
Nov 17th, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.93 KB | None | 0 0
  1. <?php
  2.  
  3. class CloverApi
  4. {
  5. var $sandbox_url;
  6. var $production_url;
  7. var $isSandbox;
  8.  
  9. function __construct(){
  10. $this->sandbox_url = "https://apisandbox.dev.clover.com";
  11. $this->production_url = "https://api.clover.com";
  12. $this->isSandbox = true;
  13. }
  14.  
  15. public function getURL(){
  16. if($this->isSandbox){
  17. return $this->sandbox_url;
  18. }else{
  19. return $this->production_url;
  20. }
  21. }
  22.  
  23. /* Exchanging an auth code for an access token */
  24. public function GetAccessToken($client_id, $client_secret, $code) {
  25. //sandbox.dev.clover.com
  26. $url = 'https://sandbox.dev.clover.com/oauth/token';
  27. if(!$this->isSandbox){
  28. $url = 'https://www.clover.com/oauth/token';
  29. }
  30.  
  31. $curlPost = 'client_id=' . $client_id . '&client_secret=' . $client_secret . '&code='. $code;
  32. $ch = curl_init();
  33. curl_setopt($ch, CURLOPT_URL, $url);
  34. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  35. curl_setopt($ch, CURLOPT_POST, 1);
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  37. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  38. $data = json_decode(curl_exec($ch), true); //echo "<pre>";var_dump($data);
  39. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  40. if($http_code != 200)
  41. throw new Exception('Error : Failed to receive access token');
  42. return $data;
  43. }
  44.  
  45. public function GetUser($merchant_id, $access_token) {
  46. $url = $this->getURL().'/v3/merchants/current/employees/current';
  47. $ch = curl_init();
  48. curl_setopt($ch, CURLOPT_URL, $url);
  49. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  50. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  51. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  52. $data = json_decode(curl_exec($ch), true);//echo '<pre>';print_r($data);echo '</pre>';
  53. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  54. if($http_code != 200)
  55. throw new Exception('Error : Failed to get user info');
  56. return $data;
  57. }
  58.  
  59.  
  60. /* Get the list of orders */
  61. public function GetOrders($merchant_id, $access_token, $filter, $execution_count) {
  62.  
  63. $limit = 1000;
  64. $offset = ($execution_count - 1)*$limit;
  65.  
  66. $url = $this->getURL().'/v3/merchants/' . $merchant_id . '/orders?limit='.$limit;
  67.  
  68. for($i=0; $i<sizeof($filter); $i++)
  69. $url .= '&filter=' . urlencode($filter[$i]);
  70.  
  71. $ch = curl_init();
  72. curl_setopt($ch, CURLOPT_URL, $url);
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  74. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  75. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  76. $data = json_decode(curl_exec($ch), true);//echo '<pre>';print_r($_SESSION);print_r($data);echo '</pre>';exit;
  77. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  78.  
  79. if($http_code != 200)
  80. throw new Exception('Error : Failed to get orders');
  81.  
  82. $myData = array();
  83. for($i=0; $i<sizeof($data['elements']); $i++) {
  84. $d = $data['elements'][$i];
  85. $temp['title'] = $d['title'];
  86. $temp['id'] = $d['id'];
  87. $temp['total'] = $d['total'];
  88. $temp['date'] = date("d-M-y",$d['clientCreatedTime']/1000);
  89. $temp['note'] = array();
  90.  
  91. if(isset($d['note']))
  92. $temp['note'] = explode('&',$d['note']);
  93. $myData[] = $temp;
  94. }
  95. return $myData;
  96. }
  97.  
  98. public function GetOrdersAll($merchant_id, $access_token, $filter, $execution_count) {
  99.  
  100. $limit = 1000;
  101. $offset = ($execution_count - 1)*$limit;
  102.  
  103. $url = $this->getURL().'/v3/merchants/' . $merchant_id . '/orders?';
  104.  
  105. for($i=0; $i<sizeof($filter); $i++)
  106. $url .= '&filter=' . urlencode($filter[$i]);
  107.  
  108. $ch = curl_init();
  109. curl_setopt($ch, CURLOPT_URL, $url);
  110. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  111. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  112. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  113. $data = json_decode(curl_exec($ch));//echo '<pre>';print_r($_SESSION);print_r($data);echo '</pre>';exit;
  114.  
  115. return $data;
  116. }
  117.  
  118. /* Get the list of employees */
  119. public function GetEmployees($merchant_id, $access_token) {
  120. $url = $this->getURL().'/v3/merchants/' . $merchant_id . '/employees?limit=1000';
  121.  
  122. $ch = curl_init();
  123. curl_setopt($ch, CURLOPT_URL, $url);
  124. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  125. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  126. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  127. $data = json_decode(curl_exec($ch), true);//echo '<pre>';print_r($data);echo '</pre>';
  128. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  129. if($http_code != 200)
  130. throw new Exception('Error : Failed to get employees');
  131.  
  132. /*$employees = [];
  133. if(sizeof($data['elements']) > 0) {
  134. for($i=0; $i<sizeof($data['elements']); $i++)
  135. $employees[$data['elements'][$i]['id']] = $data['elements'][$i]['name'];
  136. }*/
  137.  
  138. return $data['elements'];
  139. }
  140.  
  141. /* Get the list of Inventory Category */
  142. public function GetCategory($merchant_id, $access_token) {
  143. $url = $this->getURL().'/v3/merchants/' . $merchant_id . '/categories?limit=1000';
  144.  
  145. $ch = curl_init();
  146. curl_setopt($ch, CURLOPT_URL, $url);
  147. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  148. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  149. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  150. $data = json_decode(curl_exec($ch), true);//echo '<pre>';print_r($data);echo '</pre>';exit;
  151. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  152. if($http_code != 200)
  153. throw new Exception('Error : Failed to get employees');
  154.  
  155. $categories = [];
  156. if(sizeof($data['elements']) > 0) {
  157. for($i=0; $i<sizeof($data['elements']); $i++)
  158. $categories[$data['elements'][$i]['id']] = $data['elements'][$i]['name'];
  159. }
  160.  
  161. return $categories;
  162. }
  163.  
  164. public function GetOrderTypesCategories($merchant_id,$access_token){
  165. $url = $this->getURL().'/v3/merchants/' . $merchant_id . '/order_type_categories?limit=1000';
  166.  
  167. $ch = curl_init();
  168. curl_setopt($ch, CURLOPT_URL, $url);
  169. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  170. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  171. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  172. $data = json_decode(curl_exec($ch), true);echo '<pre>';print_r($data);echo '</pre>';
  173. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  174. if($http_code != 200)
  175. throw new Exception('Error : Failed to get order types');
  176.  
  177. $order_types = [];
  178. if(sizeof($data['elements']) > 0) {
  179. for($i=0; $i<sizeof($data['elements']); $i++)
  180. $order_types[$data['elements'][$i]['id']] = $data['elements'][$i]['label'];
  181. }
  182.  
  183. return $order_types;
  184. }
  185.  
  186. /* Get the list of order types */
  187. public function GetOrderTypes($merchant_id, $access_token) {
  188. $url = $this->getURL().'/v3/merchants/' . $merchant_id . '/order_types?limit=1000';
  189.  
  190. $ch = curl_init();
  191. curl_setopt($ch, CURLOPT_URL, $url);
  192. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  193. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  194. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  195. $data = json_decode(curl_exec($ch), true);//echo '<pre>';print_r($data);echo '</pre>';
  196. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  197. if($http_code != 200)
  198. throw new Exception('Error : Failed to get order types');
  199.  
  200. $order_types = [];
  201. if(sizeof($data['elements']) > 0) {
  202. for($i=0; $i<sizeof($data['elements']); $i++)
  203. $order_types[$data['elements'][$i]['id']] = $data['elements'][$i]['label'];
  204. }
  205.  
  206. return $order_types;
  207. }
  208.  
  209. /* Get merchant name */
  210. public function GetMerchantName($merchant_id, $access_token) {
  211.  
  212. $url = $this->getURL().'/v3/merchants/' . $merchant_id;
  213.  
  214. $ch = curl_init();
  215. curl_setopt($ch, CURLOPT_URL, $url);
  216. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  217. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  218. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  219. $data = json_decode(curl_exec($ch), true);//echo '<pre>';print_r($data);echo '</pre>';exit;
  220.  
  221. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  222. if($http_code != 200)
  223. throw new Exception('Error : Failed to get merchant name');
  224.  
  225. return $data['name'];
  226. }
  227.  
  228. /* Get merchant timezone */
  229. public function GetMerchantTimezone($merchant_id, $access_token) {
  230. $url = $this->getURL().'/v3/merchants/' . $merchant_id . '/properties';
  231.  
  232. $ch = curl_init();
  233. curl_setopt($ch, CURLOPT_URL, $url);
  234. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  235. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  236. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  237. $data = json_decode(curl_exec($ch), true);//echo '<pre>';print_r($data);echo '</pre>';
  238. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  239. if($http_code != 200)
  240. throw new Exception('Error : Failed to get merchant timezone');
  241.  
  242. return $data['timezone'];
  243. }
  244.  
  245. public function GetPaymentTypes($merchant_id, $access_token) {
  246. $url = $this->getURL().'/v3/merchants/' . $merchant_id . '/tenders';
  247.  
  248. $ch = curl_init();
  249. curl_setopt($ch, CURLOPT_URL, $url);
  250. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  251. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  252. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  253. $data = json_decode(curl_exec($ch), true);//echo '<pre>';print_r($data);echo '</pre>';
  254. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  255. if($http_code != 200)
  256. throw new Exception('Error : Failed to get merchant timezone');
  257. if(sizeof($data['elements']) > 0) {
  258. for($i=0; $i<sizeof($data['elements']); $i++)
  259. $payment_types[$data['elements'][$i]['id']] = $data['elements'][$i]['label'];
  260. }
  261. return $payment_types;
  262. }
  263.  
  264. private function GetAllPayment($merchant_id, $access_token, $filterPayment) {
  265. $url = $this->getURL().'/v3/merchants/' . $merchant_id . '/payments';
  266. for($i=0; $i<sizeof($filter); $i++)
  267. $url .= '&filter=' . urlencode($filter[$i]);
  268.  
  269. $ch = curl_init();
  270. curl_setopt($ch, CURLOPT_URL, $url);
  271. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  272. curl_setopt($ch,CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
  273. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  274. $data = json_decode(curl_exec($ch), true);//echo '<pre>';print_r($data);echo '</pre>';
  275. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  276. if($http_code != 200)
  277. throw new Exception('Error : Failed to get merchant timezone');
  278.  
  279. $Payments = [];
  280.  
  281. if(sizeof($data['elements']) > 0) {
  282. for($i=0; $i<sizeof($data['elements']); $i++) {
  283. $payment = new stdClass();
  284. $payment->orderid = $data['elements'][$i]['order']['id'];
  285. $payment->tenderid = $data['elements'][$i]['tender']['id'];
  286. array_push($Payments, $payment);
  287. }
  288. }
  289.  
  290. return $Payments;
  291. }
  292. }
  293. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement