|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Payid19; |
| 4 | + |
| 5 | +class ClientAPI |
| 6 | +{ |
| 7 | + protected $public_key =''; |
| 8 | + protected $private_key = ''; |
| 9 | + |
| 10 | + public $apiEndPoint = 'https://payid19.com/api/v1'; |
| 11 | + |
| 12 | + |
| 13 | + public function __construct($public_key,$private_key) |
| 14 | + { |
| 15 | + $this->public_key = $public_key; |
| 16 | + $this->private_key = $private_key; |
| 17 | + } |
| 18 | + |
| 19 | + protected function getApiUrl($commandUrl) |
| 20 | + { |
| 21 | + return trim($this->apiEndPoint, '/') . '/' . $commandUrl; |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + public function create_invoice($req) |
| 26 | + { |
| 27 | + return $this->apiCall('create_invoice', $req); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + private function isSetup() |
| 32 | + { |
| 33 | + if($this->public_key=='' or $this->private_key==''){ |
| 34 | + return false; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + private function apiCall($cmd, $req = array()) |
| 40 | + { |
| 41 | + if ($this->isSetup()==false) { |
| 42 | + return array('error' => 'You have not called the Setup function with your private and public keys!'); |
| 43 | + } |
| 44 | + return $this->guestApiCall($cmd, $req); |
| 45 | + } |
| 46 | + |
| 47 | + private function guestApiCall($cmd, $req = array()) |
| 48 | + { |
| 49 | + try { |
| 50 | + $apiUrl = $this->getApiUrl($cmd); |
| 51 | + |
| 52 | + $ch = curl_init(); |
| 53 | + curl_setopt($ch, CURLOPT_URL, $apiUrl); |
| 54 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
| 55 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 56 | + curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($req)); |
| 57 | + $result = curl_exec($ch); |
| 58 | + curl_close($ch); |
| 59 | + |
| 60 | + if($this->json_decode($result)->status=='error'){ |
| 61 | + //error |
| 62 | + return $this->json_decode($result)->message[0]; |
| 63 | + }else{ |
| 64 | + //success |
| 65 | + return $this->json_decode($result)->message; |
| 66 | + } |
| 67 | + } catch (\Exception $e) { |
| 68 | + return array('status' => 'error', 'message' => 'Could not send request to API : ' . $apiUrl); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private function jsonDecode($data) |
| 73 | + { |
| 74 | + if (PHP_INT_SIZE < 8 && version_compare(PHP_VERSION, '5.4.0') >= 0) { |
| 75 | + // We are on 32-bit PHP, so use the bigint as string option. If you are using any API calls with Satoshis it is highly NOT recommended to use 32-bit PHP |
| 76 | + $dec = json_decode($data, TRUE, 512, JSON_BIGINT_AS_STRING); |
| 77 | + } else { |
| 78 | + $dec = json_decode($data, TRUE); |
| 79 | + } |
| 80 | + return $dec; |
| 81 | + } |
| 82 | + |
| 83 | + public function verifyCallbackData($data, $secretKey) |
| 84 | + { |
| 85 | + |
| 86 | + } |
| 87 | +} |
0 commit comments