<?= $form->field($model, 'tiller_restaurant_token'); ?> | <?= $form->field($model, 'tiller_restaurant_token'); ?> | ||||
<h4>API V3</h4> | <h4>API V3</h4> | ||||
<?= $form->field($model, 'tiller_store_id'); ?> | |||||
<?= $form->field($model, 'tiller_redirect_uri'); ?> | <?= $form->field($model, 'tiller_redirect_uri'); ?> | ||||
<?= $form->field($model, 'tiller_client_id'); ?> | <?= $form->field($model, 'tiller_client_id'); ?> | ||||
<?= $form->field($model, 'tiller_client_secret'); ?> | <?= $form->field($model, 'tiller_client_secret'); ?> |
{ | { | ||||
name: 'logiciels-caisse', | name: 'logiciels-caisse', | ||||
nameDisplay: 'Logiciels de caisse', | nameDisplay: 'Logiciels de caisse', | ||||
isAdminSection: 0 | |||||
isAdminSection: 1 | |||||
}, | }, | ||||
{ | { | ||||
name: 'software', | name: 'software', |
$this->restaurantToken = $restaurantToken; | $this->restaurantToken = $restaurantToken; | ||||
} | } | ||||
public function getUrlAuthorizeCode(string $clientId, string $redirectUri): string | |||||
{ | |||||
return "#"; | |||||
} | |||||
public function isAuthenticated(): bool | public function isAuthenticated(): bool | ||||
{ | { | ||||
return true; | return true; |
class TillerClientV3 implements TillerClientInterface | class TillerClientV3 implements TillerClientInterface | ||||
{ | { | ||||
var $curl; | var $curl; | ||||
var $clientId; | |||||
var $clientSecret; | |||||
var $urlApi = 'https://app.tillersystems.com/api/'; | |||||
var $storeId; | |||||
var $headers = []; | |||||
var $urlApi = 'https://api.tiller.systems/orders/v3/'; | |||||
public function __construct(string $clientId = null, string $clientSecret = null) | |||||
public function __construct(string $storeId = null, string $accessToken = null) | |||||
{ | { | ||||
$this->storeId = $storeId; | |||||
$this->headers = [ | |||||
'authorization' => 'Bearer '.$accessToken, | |||||
'accept' => 'application/json', | |||||
'content-type' => 'application/json', | |||||
]; | |||||
$this->curl = new curl\Curl(); | $this->curl = new curl\Curl(); | ||||
$this->clientId = $clientId; | |||||
$this->clientSecret = $clientSecret; | |||||
$this->curl->setHeader('Authorization', 'Bearer '.$accessToken); | |||||
$this->curl->setHeader('accept', 'application/json'); | |||||
} | } | ||||
public function getUrlAuthorizeCode(string $clientId, string $redirectUri): string | public function getUrlAuthorizeCode(string $clientId, string $redirectUri): string | ||||
public function isAuthenticated(): bool | public function isAuthenticated(): bool | ||||
{ | { | ||||
/*$resultOrders = $this->getOrders('2024-02-01'); | |||||
print_r($resultOrders); | |||||
die();*/ | |||||
return false; | return false; | ||||
} | } | ||||
public function getOrders($date) | public function getOrders($date) | ||||
{ | { | ||||
$orders = $this->curl->setGetParams([ | |||||
'client_id' => $this->clientId, | |||||
'client_secret' => $this->clientSecret, | |||||
'dateFrom' => date('Y-m-d H-i-s', strtotime($date)), | |||||
'dateTo' => date( | |||||
'Y-m-d H-i-s', | |||||
strtotime($date) + 24 * 60 * 60 - 1 | |||||
), | |||||
'status' => 'IN_PROGRESS', | |||||
])->get($this->urlApi . 'orders'); | |||||
return json_decode($orders); | |||||
$client = new \GuzzleHttp\Client(); | |||||
$response = $client->request('POST', 'https://api.tiller.systems/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(); | |||||
/*$orders = $this->curl | |||||
->setRequestBody(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), | |||||
] | |||||
])) | |||||
->setPostParams([ | |||||
// 'statuses' => ['IN_PROGRESS'], | |||||
])->post($this->urlApi . 'orders/search?count=20'); | |||||
return json_decode($orders);*/ | |||||
} | } | ||||
public function postOrder($params) | public function postOrder($params) | ||||
{ | { | ||||
return $this->curl->setPostParams( | |||||
array_merge([ | |||||
'client_id' => $this->clientId, | |||||
'client_secret' => $this->clientSecret, | |||||
], $params) | |||||
) | |||||
return $this->curl | |||||
->setPostParams($params) | |||||
->post($this->urlApi . 'orders'); | ->post($this->urlApi . 'orders'); | ||||
} | } | ||||
} | } |
} | } | ||||
elseif($apiVersion == 'v3') { | elseif($apiVersion == 'v3') { | ||||
return new TillerClientV3( | return new TillerClientV3( | ||||
$this->producerSolver->getConfig('tiller_client_id'), | |||||
$this->producerSolver->getConfig('tiller_client_secret') | |||||
$this->producerSolver->getConfig('tiller_store_id'), | |||||
$this->producerSolver->getConfig('tiller_access_token') | |||||
); | ); | ||||
} | } | ||||
public function synchronizeDistribution(string $date): array | public function synchronizeDistribution(string $date): array | ||||
{ | { | ||||
$apiVersion = $this->producerSolver->getConfig('tiller_api_version'); | |||||
$return = []; | $return = []; | ||||
if($this->tillerActivated) { | if($this->tillerActivated) { | ||||
$this->orderBuilder->initOrder($order); | $this->orderBuilder->initOrder($order); | ||||
$lines = []; | $lines = []; | ||||
foreach ($order->productOrder as $productOrder) { | foreach ($order->productOrder as $productOrder) { | ||||
$lines[] = [ | |||||
'name' => $productOrder->product->name, | |||||
'price' => $this->productOrderSolver->getPriceWithTax($productOrder) * 100 * $productOrder->quantity, | |||||
'tax' => $productOrder->taxRate->value * 100, | |||||
'date' => $strDate, | |||||
'quantity' => $productOrder->quantity | |||||
]; | |||||
// v3 | |||||
if($apiVersion == 'v3') { | |||||
$lines[] = [ | |||||
'name' => $productOrder->product->name, | |||||
'unitPrice' => $this->productOrderSolver->getPriceWithTax($productOrder) * 100, | |||||
'taxRate' => $productOrder->taxRate->value * 100, | |||||
'date' => $strDate, | |||||
'quantity' => $productOrder->quantity | |||||
]; | |||||
} | |||||
// v2 | |||||
else { | |||||
$lines[] = [ | |||||
'name' => $productOrder->product->name, | |||||
'price' => $this->productOrderSolver->getPriceWithTax($productOrder) * 100 * $productOrder->quantity, | |||||
'tax' => $productOrder->taxRate->value * 100, | |||||
'date' => $strDate, | |||||
'quantity' => $productOrder->quantity | |||||
]; | |||||
} | |||||
} | } | ||||
$typePaymentTiller = ''; | $typePaymentTiller = ''; | ||||
} | } | ||||
if (!$this->isSynchronized($date, $order->id)) { | if (!$this->isSynchronized($date, $order->id)) { | ||||
$returnTiller = $this->tillerClient->postOrder([ | |||||
'externalId' => $order->id, | |||||
'type' => 1, | |||||
'status' => 'IN_PROGRESS', | |||||
'openDate' => $strDate, | |||||
'closeDate' => $strDate, | |||||
'lines' => $lines, | |||||
'payments' => [ | |||||
[ | |||||
'type' => $typePaymentTiller, | |||||
'amount' => $this->orderSolver->getOrderAmountWithTax( | |||||
$order, | |||||
Order::AMOUNT_PAID | |||||
) * 100, | |||||
'status' => 'ACCEPTED', | |||||
'date' => $strDate | |||||
// v3 | |||||
if($apiVersion == 'v3') { | |||||
$returnTiller = $this->tillerClient->postOrder([ | |||||
'externalId' => $order->id, | |||||
'type' => 1, | |||||
'status' => 'IN_PROGRESS', | |||||
'date' => $strDate, | |||||
'itemLines' => $lines, | |||||
'payments' => [ | |||||
[ | |||||
'type' => $typePaymentTiller, | |||||
'amount' => $this->orderSolver->getOrderAmountWithTax( | |||||
$order, | |||||
Order::AMOUNT_PAID | |||||
) * 100, | |||||
'status' => 'ACCEPTED', | |||||
'date' => $strDate | |||||
] | |||||
] | ] | ||||
] | |||||
]); | |||||
]); | |||||
} | |||||
// v2 | |||||
else { | |||||
$returnTiller = $this->tillerClient->postOrder([ | |||||
'externalId' => $order->id, | |||||
'type' => 1, | |||||
'status' => 'IN_PROGRESS', | |||||
'openDate' => $strDate, | |||||
'closeDate' => $strDate, | |||||
'lines' => $lines, | |||||
'payments' => [ | |||||
[ | |||||
'type' => $typePaymentTiller, | |||||
'amount' => $this->orderSolver->getOrderAmountWithTax( | |||||
$order, | |||||
Order::AMOUNT_PAID | |||||
) * 100, | |||||
'status' => 'ACCEPTED', | |||||
'date' => $strDate | |||||
] | |||||
] | |||||
]); | |||||
} | |||||
$returnTillerObject = json_decode($returnTiller); | $returnTillerObject = json_decode($returnTiller); | ||||
$order->tiller_external_id = '' . $returnTillerObject->id; | $order->tiller_external_id = '' . $returnTillerObject->id; |
'tiller_client_secret', | 'tiller_client_secret', | ||||
'tiller_access_token', | 'tiller_access_token', | ||||
'tiller_refresh_token', | 'tiller_refresh_token', | ||||
'tiller_redirect_uri' | |||||
'tiller_redirect_uri', | |||||
'tiller_store_id' | |||||
], | ], | ||||
'string' | 'string' | ||||
], | ], | ||||
'tiller_access_token' => 'Tiller : access token', | 'tiller_access_token' => 'Tiller : access token', | ||||
'tiller_refresh_token' => 'Tiller : refresh token', | 'tiller_refresh_token' => 'Tiller : refresh token', | ||||
'tiller_redirect_uri' => 'Tiller : callback URL', | 'tiller_redirect_uri' => 'Tiller : callback URL', | ||||
'tiller_store_id' => 'Tiller : store ID' | |||||
]; | ]; | ||||
} | } | ||||
$this->addColumn('producer', 'tiller_access_token', Schema::TYPE_TEXT); | $this->addColumn('producer', 'tiller_access_token', Schema::TYPE_TEXT); | ||||
$this->addColumn('producer', 'tiller_refresh_token', Schema::TYPE_TEXT); | $this->addColumn('producer', 'tiller_refresh_token', Schema::TYPE_TEXT); | ||||
$this->addColumn('producer', 'tiller_redirect_uri', Schema::TYPE_STRING); | $this->addColumn('producer', 'tiller_redirect_uri', Schema::TYPE_STRING); | ||||
$this->addColumn('producer', 'tiller_store_id', Schema::TYPE_STRING); | |||||
} | } | ||||
/** | /** | ||||
$this->dropColumn('producer', 'tiller_access_token'); | $this->dropColumn('producer', 'tiller_access_token'); | ||||
$this->dropColumn('producer', 'tiller_refresh_token'); | $this->dropColumn('producer', 'tiller_refresh_token'); | ||||
$this->dropColumn('producer', 'tiller_redirect_uri'); | $this->dropColumn('producer', 'tiller_redirect_uri'); | ||||
$this->dropColumn('producer', 'tiller_store_id'); | |||||
} | } | ||||
} | } |