|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
-
-
-
- namespace common\components\Tiller;
-
- use common\components\Tiller\TillerClientInterface;
- use GuzzleHttp\Exception\RequestException;
- use linslin\yii2\curl;
-
- class TillerClientV3 implements TillerClientInterface
- {
- var $client;
- var $storeId;
- var $headers = [];
- var $urlApi = 'https://api.tiller.systems/';
-
- public function __construct(string $storeId = null, string $accessToken = null)
- {
- $this->client = new \GuzzleHttp\Client();
- $this->storeId = (int) $storeId;
- $this->headers = [
- 'authorization' => 'Bearer '.$accessToken,
- 'accept' => 'application/json',
- 'content-type' => 'application/json',
- ];
- }
-
- public function getUrlAuthorizeCode(string $clientId = null, string $redirectUri = null): string
- {
- if($clientId && $redirectUri) {
- return "https://oauth.api.tiller.systems/oauth2/authorize?client_id=$clientId&response_type=code&scope=&redirect_uri=$redirectUri";
- }
-
- return '#';
- }
-
- public function isAuthenticated(): bool
- {
- return $this->getOrders(date('Y-m-d')) !== false;
- }
-
- public function getOrders($date)
- {
- try {
- $response = $this->client->request('POST', $this->urlApi.'orders/v3/orders/search', [
- 'body' => json_encode([
- 'storeIds' => [$this->storeId],
- 'openDate' => [
- 'from' => date('Y-m-d H:i:s', strtotime($date)),
- 'to' => date('Y-m-d H:i:s', strtotime($date) + 24 * 60 * 60 - 1)
- ]
- ]),
- 'headers' => $this->headers
- ]);
-
- return $response->getBody()->getContents();
- }
- catch (RequestException $exception) {
- return false;
- }
- }
-
- public function postOrder($params)
- {
- try {
- $response = $this->client->request('POST', $this->urlApi.'purchase-requests/v1/requests?storeId='.$this->storeId, [
- 'body' => json_encode($params),
- 'headers' => $this->headers
- ]);
- }
- catch (RequestException $exception) {
- print_r($exception);
- die();
- }
-
- return $response->getBody()->getContents();
- }
- }
|