diff --git a/Payme/Account.php b/Payme/Account.php new file mode 100644 index 0000000..505d594 --- /dev/null +++ b/Payme/Account.php @@ -0,0 +1,71 @@ +name = $name; + $this->title = $title; + $this->value = $value; + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @param string $name + */ + public function setName(string $name): void + { + $this->name = $name; + } + + /** + * @return string + */ + public function getTitle(): string + { + return $this->title; + } + + /** + * @param string $title + */ + public function setTitle(string $title): void + { + $this->title = $title; + } + + /** + * @return string + */ + public function getValue(): string + { + return $this->value; + } + + /** + * @param string $value + */ + public function setValue(string $value): void + { + $this->value = $value; + } +} \ No newline at end of file diff --git a/Payme/Api/Api.php b/Payme/Api/Api.php index d9d6e73..6c034b7 100644 --- a/Payme/Api/Api.php +++ b/Payme/Api/Api.php @@ -4,6 +4,8 @@ use App\Api\Api as BaseApi; use App\Payme\Card; +use App\Payme\Cheque; +use App\Payme\DebugException; use App\Session\Session; use DateTime; use DateTimeZone; @@ -83,119 +85,168 @@ public function __construct() * @param array|string $postfields * @param array $headers * @return BaseApi + * @throws DebugException */ protected function post(string $url, $postfields, array $headers = []): BaseApi { - $headers = array_merge([ - 'Content-Type: text/plain', - 'Accept: */*', - 'Connection: keep-alive' - ], $headers); - $params['method'] = $url; - $params['params'] = $postfields; - $response = parent::post(self::ENDPOINT_URL . $url, json_encode($params), $headers); - $this->api_session = $this->getApiSession($response); - return $response; + try { + $headers = array_merge([ + 'Content-Type: text/plain', + 'Accept: */*', + 'Connection: keep-alive' + ], $headers); + $params['method'] = $url; + $params['params'] = $postfields; + $response = parent::post(self::ENDPOINT_URL . $url, json_encode($params), $headers); + $this->api_session = $this->getApiSession($response); + return $response; + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * @param array $headers * @return Api + * @throws DebugException */ public function login(array $headers = []): Api { - $this->post(self::API_LOGIN_URL, $this->credentials, $headers); - $this->is_active_session = (bool)$this->device ?? false; - return $this; + try { + $this->post(self::API_LOGIN_URL, $this->credentials, $headers); + $this->is_active_session = (bool)$this->device ?? false; + return $this; + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * @param string $code * @return Api + * @throws DebugException */ public function activate(string $code): Api { - $this->post(self::API_SESSION_ACTIVATE_URL, ['code' => $code, 'device' => true], ["API-SESSION: $this->api_session"]); - return $this; + try { + $this->post(self::API_SESSION_ACTIVATE_URL, ['code' => $code, 'device' => true], ["API-SESSION: $this->api_session"]); + return $this; + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * @return Api + * @throws DebugException */ public function sendActivationCode(): Api { - $this->post(self::API_SEND_ACTIVATION_CODE, [], ["API-SESSION: $this->api_session"]); - return $this; + try { + $this->post(self::API_SEND_ACTIVATION_CODE, [], ["API-SESSION: $this->api_session"]); + return $this; + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * @return Api + * @throws DebugException */ public function registerDevice(): Api { - $this->post(self::API_REGISTER_DEVICE_URL, [ - 'display' => self::DEVICE_NAME, - 'type' => 2 - ], ["API-SESSION: $this->api_session"]); - $this->device = "{$this->getContent()['result']['_id']}; {$this->getContent()['result']['key']};"; - return $this; + try { + $this->post(self::API_REGISTER_DEVICE_URL, [ + 'display' => self::DEVICE_NAME, + 'type' => 2 + ], ["API-SESSION: $this->api_session"]); + $this->device = "{$this->getContent()['result']['_id']}; {$this->getContent()['result']['key']};"; + return $this; + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * @param array $sort - * @return false|int|mixed|Api + * @return Cheque[] + * @throws DebugException */ - public function getAllCheques(array $sort = []) + public function getAllCheques(array $sort = []): array { - $sort = $sort ?: [ - 'count' => 20, - 'group' => 'time' - ]; - $this->login(["Device: $this->device"]); - $this->post(self::API_CHEQUE_URL, $sort, ["API-SESSION: $this->api_session", "Device: $this->device"]); - return $this->getContent()['result']['cheques']; + try { + $sort = $sort ?: [ + 'count' => 90, + 'group' => 'time' + ]; + $this->login(["Device: $this->device"]); + $this->post(self::API_CHEQUE_URL, $sort, ["API-SESSION: $this->api_session", "Device: $this->device"]); + + return array_map(function ($cheque) { + return new Cheque(...array_values(array_slice($cheque, 0, 17))); + }, $this->getContent()['result']['cheques']); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** - * @return false|int|mixed|null + * @return Cheque[] + * @throws DebugException */ - public function getCheques() + public function getCheques(): array { - return $this->cheques; + try { + return $this->cheques; + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * Chainable cheques * @param array $sort * @return $this + * @throws DebugException */ public function cheques(array $sort = []): Api { - $this->cheques = $this->getAllCheques($sort); - return $this; + try { + $this->cheques = $this->getAllCheques($sort); + return $this; + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * @return Card[] + * @throws DebugException */ public function getMyCards(): array { - $this->login(["Device: $this->device"]); - $this->post(self::API_GET_CARDS_URL, [], ["API-SESSION: $this->api_session", "Device: $this->device"]); + try { + $this->login(["Device: $this->device"]); + $this->post(self::API_GET_CARDS_URL, [], ["API-SESSION: $this->api_session", "Device: $this->device"]); - return array_map(function ($card) { - return new Card( - $card['_id'], $card['name'], $card['number'], - $card['expire'], $card['active'], $card['owner'], - $card['balance'], $card['main'], $card['date'] - ); - }, $this->getContent()['result']['cards']); + return array_map(function ($card) { + return new Card( + $card['_id'], $card['name'], $card['number'], + $card['expire'], $card['active'], $card['owner'], + $card['balance'], $card['main'], $card['date'] + ); + }, $this->getContent()['result']['cards']); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * @param string $card_id * @param array $sort * @return $this + * @throws DebugException */ public function selectCard(string $card_id, array $sort = []): Api { @@ -214,44 +265,26 @@ public function selectCard(string $card_id, array $sort = []): Api ] ]); return $this; - } catch (Exception $exception) { - echo $this->getExceptionMessage($exception); - die(); - } catch (Throwable $throwable) { - echo $this->getExceptionMessage($throwable); - die(); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); } } - /** - * @param $exception - * @return string - */ - private function getExceptionMessage($exception): string - { - $message = "Xatolik matni: "; - $message .= $exception->getMessage(); - $message .= "
Xatolik kodi:: "; - $message .= $exception->getCode(); - $message .= "
Xatolik kelib chiqqan qator:: "; - $message .= $exception->getLine(); - $message .= "
Xatolik kelib chiqqan fayl:: "; - $message .= $exception->getFile(); - $message .= "
Xatolik izi:: "; - $message .= $exception->getTraceAsString(); - return $message; - } - /** * @param string $comment * @param int $amount * @return array + * @throws DebugException */ public function findByComment(string $comment, int $amount): array { - return array_filter($this->cheques, function ($cheque) use ($amount, $comment) { - return $cheque['description'] == $comment && $cheque['amount'] == $amount; - }); + try { + return array_filter($this->cheques, function ($cheque) use ($amount, $comment) { + return $cheque->hasPaymentWithComment($comment, $amount); + }); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** @@ -259,7 +292,11 @@ public function findByComment(string $comment, int $amount): array */ public function date(): DateTime { - return new DateTime('now', new DateTimeZone(self::API_TIMEZONE)); + try { + return new DateTime('now', new DateTimeZone(self::API_TIMEZONE)); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** @@ -293,27 +330,42 @@ public function setCredentials(array $credentials): Api /** * @param BaseApi $api * @return mixed + * @throws DebugException */ public function getApiSession(BaseApi $api) { - return $api->getHeader($api->getContent(true))['api-session']; + try { + return $api->getHeader($api->getContent(true))['api-session']; + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * @param $property * @param $value + * @throws DebugException */ public function __set($property, $value) { - $this->session->store($property, $value); + try { + $this->session->store($property, $value); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } /** * @param $property * @return mixed|null + * @throws DebugException */ public function __get($property) { - return $this->session->get($property); + try { + return $this->session->get($property); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } } } diff --git a/Payme/Card.php b/Payme/Card.php index 89da4cc..5bf0eb7 100644 --- a/Payme/Card.php +++ b/Payme/Card.php @@ -2,7 +2,7 @@ namespace App\Payme; -class Card +class Card extends Formatter { /** * Defines card id @@ -139,7 +139,7 @@ public function getOwner(): string */ public function getBalance(): string { - return number_format($this->balance / 100, 2, '.', ','); + return $this->formatMoney($this->balance); } /** @@ -151,12 +151,11 @@ public function isMain(): bool } /** - * @param string $format * @return string */ - public function getDate(string $format = "Y/m/d H:i:s"): string + public function getDate(): string { - return date($format, round($this->date / 1000)); + return $this->formatDate($this->date); } /** diff --git a/Payme/Cheque.php b/Payme/Cheque.php new file mode 100644 index 0000000..ebe3bc7 --- /dev/null +++ b/Payme/Cheque.php @@ -0,0 +1,510 @@ +id = $id; + $this->createTime = $createTime; + $this->payTime = $payTime; + $this->cancelTime = $cancelTime; + $this->state = $state; + $this->type = $type; + $this->external = $external; + $this->operation = $operation; + $this->category = $category; + $this->error = $error; + $this->description = $description; + $this->detail = $detail; + $this->amount = $amount; + $this->currency = $currency; + $this->commission = $commission; + $this->account = $account; + $this->card = $card; + } + + /** + * @param string $comment + * @param int $amount + * @return bool + */ + public function hasPaymentWithComment(string $comment, int $amount): bool + { + return $this->description == $comment && $this->amount == $amount; + } + + /** + * @return string + */ + public function getId(): string + { + return $this->id; + } + + /** + * @return string + */ + public function getCreateTime(): string + { + return $this->formatDate($this->createTime); + } + + /** + * @return string + */ + public function getPayTime(): string + { + return $this->formatDate($this->payTime); + } + + /** + * @return string + */ + public function getCancelTime(): string + { + return $this->formatDate($this->cancelTime); + } + + /** + * @return int + */ + public function getState(): int + { + return $this->state; + } + + /** + * @return int + */ + public function getType(): int + { + return $this->type; + } + + /** + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * @return string + */ + public function getAmount(): string + { + return $this->formatMoney($this->amount); + } + + /** + * @return string + */ + public function getCommission(): string + { + return $this->formatMoney($this->commission); + } + + /** + * @return array + */ + public function getAccount(): array + { + return $this->account; + } + + /** + * @return Account + * @throws DebugException + */ + public function getEcardTrx(): Account + { + try { + $account = array_filter($this->account, function ($account) { + return $account['name'] === self::ECARD_TRX_ID; + }); + return new Account(...array_values(array_shift($account))); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } + } + + /** + * @return Account + * @throws DebugException + */ + public function getEcardEmitter(): Account + { + try { + $account = array_filter($this->account, function ($account) { + return $account['name'] === self::ECARD_EMITTER; + }); + return new Account(...array_values(array_shift($account))); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } + } + + /** + * @return Account + * @throws DebugException + */ + public function getCard(): Account + { + try { + $account = array_filter($this->account, function ($account) { + return $account['name'] === self::CARD; + }); + return new Account(...array_values(array_shift($account))); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } + } + + /** + * @return Account + * @throws DebugException + */ + public function getOwner(): Account + { + try { + $account = array_filter($this->account, function ($account) { + return $account['name'] === self::OWNER; + }); + return new Account(...array_values(array_shift($account))); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } + } + + /** + * @return Card + * @throws DebugException + */ + public function getCardData(): Card + { + try { + return new Card( + $this->card['_id'], $this->card['name'], $this->card['number'], + $this->card['expire'], true, $this->card['name'], + 0, $this->card['main'], time() + ); + } catch (Exception | Throwable $exception) { + throw new DebugException($exception); + } + } + + /** + * @param string $id + */ + public function setId(string $id): void + { + $this->id = $id; + } + + /** + * @param int $createTime + */ + public function setCreateTime(int $createTime): void + { + $this->createTime = $createTime; + } + + /** + * @param int $payTime + */ + public function setPayTime(int $payTime): void + { + $this->payTime = $payTime; + } + + /** + * @param int $cancelTime + */ + public function setCancelTime(int $cancelTime): void + { + $this->cancelTime = $cancelTime; + } + + /** + * @param int $state + */ + public function setState(int $state): void + { + $this->state = $state; + } + + /** + * @param int $type + */ + public function setType(int $type): void + { + $this->type = $type; + } + + /** + * @param string $description + */ + public function setDescription(string $description): void + { + $this->description = $description; + } + + /** + * @param int $amount + */ + public function setAmount(int $amount): void + { + $this->amount = $amount; + } + + /** + * @param int $commission + */ + public function setCommission(int $commission): void + { + $this->commission = $commission; + } + + /** + * @param array $account + */ + public function setAccount(array $account): void + { + $this->account = $account; + } + + /** + * @return bool + */ + public function isExternal(): bool + { + return $this->external; + } + + /** + * @param bool $external + */ + public function setExternal(bool $external): void + { + $this->external = $external; + } + + /** + * @return int + */ + public function getOperation(): int + { + return $this->operation; + } + + /** + * @param int $operation + */ + public function setOperation(int $operation): void + { + $this->operation = $operation; + } + + /** + * @return array + */ + public function getCategory(): array + { + return $this->category; + } + + /** + * @param array $category + */ + public function setCategory(array $category): void + { + $this->category = $category; + } + + /** + * @return string|null + */ + public function getError(): ?string + { + return $this->error; + } + + /** + * @param string|null $error + */ + public function setError(?string $error): void + { + $this->error = $error; + } + + /** + * @return string|null + */ + public function getDetail(): ?string + { + return $this->detail; + } + + /** + * @param string|null $detail + */ + public function setDetail(?string $detail): void + { + $this->detail = $detail; + } + + /** + * @return int + */ + public function getCurrency(): int + { + return $this->currency; + } + + /** + * @param int $currency + */ + public function setCurrency(int $currency): void + { + $this->currency = $currency; + } +} \ No newline at end of file diff --git a/Payme/DebugException.php b/Payme/DebugException.php new file mode 100644 index 0000000..f40d2f0 --- /dev/null +++ b/Payme/DebugException.php @@ -0,0 +1,42 @@ +Xatolik matni: "; + $message .= $this->getMessage(); + $message .= "
Xatolik kodi:: "; + $message .= $this->getCode(); + $message .= "
Xatolik kelib chiqqan qator:: "; + $message .= $this->getLine(); + $message .= "
Xatolik kelib chiqqan fayl:: "; + $message .= $this->getFile(); + $message .= "
Xatolik izi:: "; + $message .= $this->getTraceAsString(); + return $message; + } +} \ No newline at end of file diff --git a/Payme/Formatter.php b/Payme/Formatter.php new file mode 100644 index 0000000..abd3f02 --- /dev/null +++ b/Payme/Formatter.php @@ -0,0 +1,26 @@ +