Browse Source

[Administration] Mise à jour API Sumpup

feature/souke
Guillaume Bourgeois 10 months ago
parent
commit
74b278e31b
7 changed files with 126 additions and 53 deletions
  1. +1
    -0
      backend/views/producer/update.php
  2. +1
    -1
      backend/web/js/vuejs/producer-update.js
  3. +5
    -0
      common/components/Tiller/TillerClientV2.php
  4. +48
    -24
      common/components/Tiller/TillerClientV3.php
  5. +66
    -27
      common/logic/Order/Order/Service/TillerManager.php
  6. +3
    -1
      common/logic/Producer/Producer/Model/Producer.php
  7. +2
    -0
      console/migrations/m240206_135558_add_columns_producer_tiller_api_v3.php

+ 1
- 0
backend/views/producer/update.php View File

@@ -433,6 +433,7 @@ $this->addBreadcrumb($this->getTitle());
<?= $form->field($model, 'tiller_restaurant_token'); ?>

<h4>API V3</h4>
<?= $form->field($model, 'tiller_store_id'); ?>
<?= $form->field($model, 'tiller_redirect_uri'); ?>
<?= $form->field($model, 'tiller_client_id'); ?>
<?= $form->field($model, 'tiller_client_secret'); ?>

+ 1
- 1
backend/web/js/vuejs/producer-update.js View File

@@ -84,7 +84,7 @@ var app = new Vue({
{
name: 'logiciels-caisse',
nameDisplay: 'Logiciels de caisse',
isAdminSection: 0
isAdminSection: 1
},
{
name: 'software',

+ 5
- 0
common/components/Tiller/TillerClientV2.php View File

@@ -54,6 +54,11 @@ class TillerClientV2 implements TillerClientInterface
$this->restaurantToken = $restaurantToken;
}

public function getUrlAuthorizeCode(string $clientId, string $redirectUri): string
{
return "#";
}

public function isAuthenticated(): bool
{
return true;

+ 48
- 24
common/components/Tiller/TillerClientV3.php View File

@@ -44,15 +44,23 @@ use linslin\yii2\curl;
class TillerClientV3 implements TillerClientInterface
{
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->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
@@ -62,33 +70,49 @@ class TillerClientV3 implements TillerClientInterface

public function isAuthenticated(): bool
{
/*$resultOrders = $this->getOrders('2024-02-01');
print_r($resultOrders);
die();*/

return false;
}

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)
{
return $this->curl->setPostParams(
array_merge([
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
], $params)
)
return $this->curl
->setPostParams($params)
->post($this->urlApi . 'orders');
}
}

+ 66
- 27
common/logic/Order/Order/Service/TillerManager.php View File

@@ -43,8 +43,8 @@ class TillerManager extends AbstractManager
}
elseif($apiVersion == 'v3') {
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')
);
}

@@ -66,6 +66,7 @@ class TillerManager extends AbstractManager

public function synchronizeDistribution(string $date): array
{
$apiVersion = $this->producerSolver->getConfig('tiller_api_version');
$return = [];

if($this->tillerActivated) {
@@ -86,13 +87,27 @@ class TillerManager extends AbstractManager
$this->orderBuilder->initOrder($order);
$lines = [];
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 = '';
@@ -113,25 +128,49 @@ class TillerManager extends AbstractManager
}

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);
$order->tiller_external_id = '' . $returnTillerObject->id;

+ 3
- 1
common/logic/Producer/Producer/Model/Producer.php View File

@@ -254,7 +254,8 @@ class Producer extends ActiveRecordCommon
'tiller_client_secret',
'tiller_access_token',
'tiller_refresh_token',
'tiller_redirect_uri'
'tiller_redirect_uri',
'tiller_store_id'
],
'string'
],
@@ -518,6 +519,7 @@ class Producer extends ActiveRecordCommon
'tiller_access_token' => 'Tiller : access token',
'tiller_refresh_token' => 'Tiller : refresh token',
'tiller_redirect_uri' => 'Tiller : callback URL',
'tiller_store_id' => 'Tiller : store ID'
];
}


+ 2
- 0
console/migrations/m240206_135558_add_columns_producer_tiller_api_v3.php View File

@@ -19,6 +19,7 @@ class m240206_135558_add_columns_producer_tiller_api_v3 extends Migration
$this->addColumn('producer', 'tiller_access_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_store_id', Schema::TYPE_STRING);
}

/**
@@ -32,5 +33,6 @@ class m240206_135558_add_columns_producer_tiller_api_v3 extends Migration
$this->dropColumn('producer', 'tiller_access_token');
$this->dropColumn('producer', 'tiller_refresh_token');
$this->dropColumn('producer', 'tiller_redirect_uri');
$this->dropColumn('producer', 'tiller_store_id');
}
}

Loading…
Cancel
Save