Browse Source

[Administration] Mise à jour API Sumpup

feature/souke
Guillaume Bourgeois 9 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

namespace common\components\Tiller; namespace common\components\Tiller;


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


class TillerClientV3 implements TillerClientInterface class TillerClientV3 implements TillerClientInterface
{ {
var $curl;
var $client;
var $storeId; var $storeId;
var $headers = []; 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) public function __construct(string $storeId = null, string $accessToken = null)
{ {
$this->storeId = $storeId;
$this->client = new \GuzzleHttp\Client();
$this->storeId = (int) $storeId;
$this->headers = [ $this->headers = [
'authorization' => 'Bearer '.$accessToken, 'authorization' => 'Bearer '.$accessToken,
'accept' => 'application/json', 'accept' => 'application/json',
'content-type' => '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 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 $this->getOrders(date('Y-m-d')) !== false;
} }


public function getOrders($date) 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 'headers' => $this->headers
]); ]);


return $response->getBody()->getContents(); 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

use common\logic\Producer\Producer\Service\ProducerSolver; use common\logic\Producer\Producer\Service\ProducerSolver;
use common\components\Tiller\TillerClientInterface; use common\components\Tiller\TillerClientInterface;
use common\components\Tiller\TillerClientV3; use common\components\Tiller\TillerClientV3;
use GuzzleHttp\Exception\RequestException;


class TillerManager extends AbstractManager class TillerManager extends AbstractManager
{ {
]); ]);


$strDate = date('Y-m-d\T12:i:s+0000', strtotime($date) + 1); $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)) { if ($orders && count($orders)) {
foreach ($orders as $order) { foreach ($orders as $order) {
$this->orderBuilder->initOrder($order); $this->orderBuilder->initOrder($order);
$lines = []; $lines = [];
foreach ($order->productOrder as $productOrder) { foreach ($order->productOrder as $productOrder) {

// v3 // v3
if($apiVersion == 'v3') { if($apiVersion == 'v3') {
$lines[] = [ $lines[] = [
'name' => $productOrder->product->name, 'name' => $productOrder->product->name,
'unitPrice' => $this->productOrderSolver->getPriceWithTax($productOrder) * 100,
'unitPrice' => [
'amount' => $this->productOrderSolver->getPriceWithTax($productOrder) * 100,
],
'taxRate' => $productOrder->taxRate->value * 100, 'taxRate' => $productOrder->taxRate->value * 100,
'date' => $strDate,
'quantity' => $productOrder->quantity 'quantity' => $productOrder->quantity
]; ];
} }
// v3 // v3
if($apiVersion == 'v3') { if($apiVersion == 'v3') {
$returnTiller = $this->tillerClient->postOrder([ $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,
]
] ]
] ]
]); ]);
} }


$returnTillerObject = json_decode($returnTiller); $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(); $order->save();


$return[] = $returnTiller; $return[] = $returnTiller;

Loading…
Cancel
Save