Browse Source

[Administration] Mise à jour API Sumpup

feature/souke
Guillaume Bourgeois 10 months ago
parent
commit
b72f12215e
2 changed files with 53 additions and 61 deletions
  1. +31
    -43
      common/components/Tiller/TillerClientV3.php
  2. +22
    -18
      common/logic/Order/Order/Service/TillerManager.php

+ 31
- 43
common/components/Tiller/TillerClientV3.php View File

@@ -39,28 +39,25 @@
namespace common\components\Tiller;

use common\components\Tiller\TillerClientInterface;
use GuzzleHttp\Exception\RequestException;
use linslin\yii2\curl;

class TillerClientV3 implements TillerClientInterface
{
var $curl;
var $client;
var $storeId;
var $headers = [];
var $urlApi = 'https://api.tiller.systems/orders/v3/';
var $urlApi = 'https://api.tiller.systems/';

public function __construct(string $storeId = null, string $accessToken = null)
{
$this->storeId = $storeId;
$this->client = new \GuzzleHttp\Client();
$this->storeId = (int) $storeId;
$this->headers = [
'authorization' => 'Bearer '.$accessToken,
'accept' => 'application/json',
'content-type' => 'application/json',
];

$this->curl = new curl\Curl();
$this->curl->setHeader('Authorization', 'Bearer '.$accessToken);
$this->curl->setHeader('accept', 'application/json');
}

public function getUrlAuthorizeCode(string $clientId, string $redirectUri): string
@@ -70,49 +67,40 @@ class TillerClientV3 implements TillerClientInterface

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

return false;
return $this->getOrders(date('Y-m-d')) !== false;
}

public function getOrders($date)
{
$client = new \GuzzleHttp\Client();
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;
}
}

$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)
]
]),
public function postOrder($params)
{
$response = $this->client->request('POST', $this->urlApi.'purchase-requests/v1/requests', [
'query' => [
'storeId' => $this->storeId
],
'body' => json_encode($params),
'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($params)
->post($this->urlApi . 'orders');
}
}

+ 22
- 18
common/logic/Order/Order/Service/TillerManager.php View File

@@ -10,6 +10,7 @@ use common\logic\Order\ProductOrder\Service\ProductOrderSolver;
use common\logic\Producer\Producer\Service\ProducerSolver;
use common\components\Tiller\TillerClientInterface;
use common\components\Tiller\TillerClientV3;
use GuzzleHttp\Exception\RequestException;

class TillerManager extends AbstractManager
{
@@ -81,20 +82,21 @@ class TillerManager extends AbstractManager
]);

$strDate = date('Y-m-d\T12:i:s+0000', strtotime($date) + 1);
$datetime = new \DateTime(date('Y-m-d 12:i:s', strtotime($date)));

if ($orders && count($orders)) {
foreach ($orders as $order) {
$this->orderBuilder->initOrder($order);
$lines = [];
foreach ($order->productOrder as $productOrder) {

// v3
if($apiVersion == 'v3') {
$lines[] = [
'name' => $productOrder->product->name,
'unitPrice' => $this->productOrderSolver->getPriceWithTax($productOrder) * 100,
'unitPrice' => [
'amount' => $this->productOrderSolver->getPriceWithTax($productOrder) * 100,
],
'taxRate' => $productOrder->taxRate->value * 100,
'date' => $strDate,
'quantity' => $productOrder->quantity
];
}
@@ -131,20 +133,17 @@ class TillerManager extends AbstractManager
// 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
'externalId' => ''.$order->id,
'purchaseRequestType' => 'clickAndCollect',
'date' => $datetime->format(\DateTime::ATOM),
'order' => [
'currencyCode' => 'EUR',
'itemLines' => $lines,
'payments' => [
[
'externalId' => ''.$order->id,
'amount' => $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID) * 100,
]
]
]
]);
@@ -173,7 +172,12 @@ class TillerManager extends AbstractManager
}

$returnTillerObject = json_decode($returnTiller);
$order->tiller_external_id = '' . $returnTillerObject->id;
if($apiVersion == 'v3') {
$order->tiller_external_id = '' . $returnTillerObject->referenceId;
}
else {
$order->tiller_external_id = '' . $returnTillerObject->id;
}
$order->save();

$return[] = $returnTiller;

Loading…
Cancel
Save