Skip to content

Commit

Permalink
Merge branch 'master' into feat/deliveryform-override-price
Browse files Browse the repository at this point in the history
  • Loading branch information
Atala committed Feb 7, 2025
2 parents b3bb9f0 + 4a6bd24 commit a9d9468
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 99 deletions.
6 changes: 3 additions & 3 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ OPTIN_EXPORT_USERS_ENABLED=0
DV4CUL_ENABLED=0
WOOPIT_CLIENT_ID=
WOOPIT_CLIENT_SECRET=
WOOPIT_HOST=recette.gcp.last-mile.fr
WOOPIT_AUTHENTICATION_API_URL=https://connect.recette.gcp.last-mile.fr/api/oauth/token
WOOPIT_CARRIER_API_URL=https://car-api.recette.gcp.last-mile.fr
DABBA_ENABLED=0
DABBA_BASE_URL=https://api.dabba-consigne.fr
DABBA_CLIENT_ID=
Expand Down Expand Up @@ -131,6 +132,5 @@ CUBEJS_DUCKDB_S3_REGION=fr-fr
CUBEJS_DUCKDB_S3_ACCESS_KEY_ID=minio
CUBEJS_DUCKDB_S3_SECRET_ACCESS_KEY=coopcycle
CUBEJS_DUCKDB_S3_PATH=/images/exports/%type%
INSEE_API_KEY=
INSEE_API_SECRET=
INSEE_PORTAIL_API_KEY=
STANDTRACK_API_KEY=
6 changes: 4 additions & 2 deletions app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ framework:
cubejs.client:
base_uri: "%env(CUBEJS_API_URL)%/"
woopit.http_client:
base_uri: 'https://car-api.%env(WOOPIT_HOST)%/'
base_uri: '%env(WOOPIT_CARRIER_API_URL)%/'
dabba.client:
base_uri: "%env(DABBA_BASE_URL)%/"
umap.client:
Expand All @@ -225,7 +225,9 @@ framework:
query:
api_key: "%env(EDENRED_SYNCHRONIZER_API_KEY)%"
insee.client:
base_uri: "https://api.insee.fr/"
base_uri: "https://api.insee.fr/api-sirene/3.11/"
headers:
X-INSEE-Api-Key-Integration: '%env(INSEE_PORTAIL_API_KEY)%'

# Twig Configuration
twig:
Expand Down
2 changes: 1 addition & 1 deletion app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ services:
class: BenjaminFavre\OAuthHttpClient\GrantType\ClientCredentialsGrantType
arguments: [
'@woopit.http_client',
'https://connect.%env(WOOPIT_HOST)%/api/oauth/token',
'%env(WOOPIT_AUTHENTICATION_API_URL)%',
'%env(WOOPIT_CLIENT_ID)%',
'%env(WOOPIT_CLIENT_SECRET)%'
]
Expand Down
3 changes: 0 additions & 3 deletions app/config/services/validators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,5 @@ services:
- { name: validator.constraint_validator }

AppBundle\Validator\Constraints\SiretValidator:
arguments:
$apiKey: '%env(INSEE_API_KEY)%'
$apiSecret: '%env(INSEE_API_SECRET)%'
tags:
- { name: validator.constraint_validator }
2 changes: 1 addition & 1 deletion app/config/state_machine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ winzou_state_machine:
from: [cart]
to: new
cancel:
from: [new, accepted, started, ready]
from: [new, accepted, started, ready, refused]
to: cancelled
fulfill:
from: [new, accepted, started, ready]
Expand Down
9 changes: 9 additions & 0 deletions docs/WOOP.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
WOOP
====

[API docs](https://woop.stoplight.io/)

To setup Woop, you will need the `WOOPIT_CLIENT_ID` & `WOOPIT_CLIENT_SECRET` env vars provided by Woop.

1. [Create an API app](https://demo.coopcycle.org/admin/api/apps/new) with the API Key authentication mechanism.
2. Run the `woopit:subscriptions:create` console command, with the `client_id` of the API app, to [setup the Woop subscriptions](https://woop.stoplight.io/docs/carrier/1p9vn2atgetp4-subscriptions)
20 changes: 0 additions & 20 deletions src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,26 +534,6 @@ public function confirmAction($hashid,

// TODO Check if order is in expected state (new or superior)

$loopeatAccessTokenKey =
sprintf('loopeat.order.%d.access_token', $id);
$loopeatRefreshTokenKey =
sprintf('loopeat.order.%d.refresh_token', $id);

if ($session->has($loopeatAccessTokenKey) && $session->has($loopeatRefreshTokenKey)) {

$order->getCustomer()->setLoopeatAccessToken(
$session->get($loopeatAccessTokenKey)
);
$order->getCustomer()->setLoopeatRefreshToken(
$session->get($loopeatRefreshTokenKey)
);

$this->objectManager->flush();

$session->remove($loopeatAccessTokenKey);
$session->remove($loopeatRefreshTokenKey);
}

$dabbaAccessTokenKey =
sprintf('dabba.order.%d.access_token', $id);
$dabbaRefreshTokenKey =
Expand Down
34 changes: 14 additions & 20 deletions src/Entity/Sylius/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
use AppBundle\Entity\ReusablePackaging;
use AppBundle\Entity\Task\RecurrenceRule;
use AppBundle\Entity\Vendor;
use AppBundle\LoopEat\OAuthCredentialsInterface as LoopeatOAuthCredentialsInterface;
use AppBundle\Payment\MercadopagoPreferenceResponse;
use AppBundle\Sylius\Order\AdjustmentInterface;
use AppBundle\Sylius\Order\OrderInterface;
Expand Down Expand Up @@ -628,15 +627,6 @@ public function getCustomer(): ?CustomerInterface
public function setCustomer(?CustomerInterface $customer): void
{
$this->customer = $customer;

if (null !== $customer && $this->hasLoopEatCredentials()) {

WMAssert::isInstanceOf($this->customer, LoopeatOAuthCredentialsInterface::class);

$this->customer->setLoopeatAccessToken($this->loopeatCredentials->getLoopeatAccessToken());
$this->customer->setLoopeatRefreshToken($this->loopeatCredentials->getLoopeatRefreshToken());
$this->clearLoopEatCredentials();
}
}

public function getTaxTotal(): int
Expand Down Expand Up @@ -1751,16 +1741,18 @@ public function getLoopeatDeliver()

public function getLoopeatAccessToken()
{
if (null === $this->loopeatCredentials) {

return null;
}
$ownToken = $this->loopeatCredentials?->getLoopeatAccessToken();
$customerToken = $this->customer?->getLoopeatAccessToken();

return $this->loopeatCredentials->getLoopeatAccessToken();
return $customerToken ?? $ownToken;
}

public function setLoopeatAccessToken($accessToken)
{
if (null !== $this->customer) {
$this->customer->setLoopeatAccessToken($accessToken);
}

if (null === $this->loopeatCredentials) {

$this->loopeatCredentials = new OrderCredentials();
Expand All @@ -1772,16 +1764,18 @@ public function setLoopeatAccessToken($accessToken)

public function getLoopeatRefreshToken()
{
if (null === $this->loopeatCredentials) {

return null;
}
$ownToken = $this->loopeatCredentials?->getLoopeatRefreshToken();
$customerToken = $this->customer?->getLoopeatRefreshToken();

return $this->loopeatCredentials->getLoopeatRefreshToken();
return $customerToken ?? $ownToken;
}

public function setLoopeatRefreshToken($refreshToken)
{
if (null !== $this->customer) {
$this->customer->setLoopeatRefreshToken($refreshToken);
}

if (null === $this->loopeatCredentials) {

$this->loopeatCredentials = new OrderCredentials();
Expand Down
1 change: 0 additions & 1 deletion src/Form/Checkout/CheckoutAddressType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use AppBundle\Form\AddressType;
use AppBundle\LoopEat\Context as LoopEatContext;
use AppBundle\LoopEat\ContextInitializer as LoopEatContextInitializer;
use AppBundle\LoopEat\GuestCheckoutAwareAdapter as LoopEatAdapter;
use AppBundle\Dabba\Client as DabbaClient;
use AppBundle\Dabba\Context as DabbaContext;
use AppBundle\Dabba\GuestCheckoutAwareAdapter as DabbaAdapter;
Expand Down
7 changes: 3 additions & 4 deletions src/LoopEat/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,13 @@ public function createOrder(OrderInterface $order)

$currentRestaurant = $this->currentRestaurant($restaurant);

// Assert::isInstanceOf($order->getCustomer(), CustomerInterface::class);
Assert::isInstanceOf($order->getCustomer(), OAuthCredentialsInterface::class);
Assert::isInstanceOf($order, OAuthCredentialsInterface::class);

$response = $this->client->request('POST', sprintf('/api/v1/partners/restaurants/%s/orders', $currentRestaurant['id']), [
'headers' => [
'Authorization' => sprintf('Bearer %s', $order->getCustomer()->getLoopeatAccessToken())
'Authorization' => sprintf('Bearer %s', $order->getLoopeatAccessToken())
],
'oauth_credentials' => $order->getCustomer(),
'oauth_credentials' => $order,
'json' => [
'order' => [
'external_id' => $order->getId(),
Expand Down
1 change: 0 additions & 1 deletion src/LoopEat/GuestCheckoutAwareAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace AppBundle\LoopEat;

use AppBundle\Sylius\Order\OrderInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class GuestCheckoutAwareAdapter implements OAuthCredentialsInterface
{
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Geocoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private function getGeocoder()
// For France only, use https://adresse.data.gouv.fr/
if ('fr' === $this->country) {
// TODO Create own provider to get results with a high score
$providers[] = AddokProvider::withBANServer($httpClient);
$providers[] = new AddokProvider($httpClient, 'https://data.geopf.fr/geocodage');
}
}

Expand Down
1 change: 0 additions & 1 deletion src/Validator/Constraints/LoopeatStockValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace AppBundle\Validator\Constraints;

use AppBundle\LoopEat\Client as LoopEatClient;
use AppBundle\LoopEat\GuestCheckoutAwareAdapter as LoopEatAdapter;
use AppBundle\Sylius\Customer\CustomerInterface;
use AppBundle\Sylius\Order\OrderInterface;
use AppBundle\Sylius\Order\OrderItemInterface;
Expand Down
41 changes: 7 additions & 34 deletions src/Validator/Constraints/SiretValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace AppBundle\Validator\Constraints;

use AppBundle\Entity\Task;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintValidator;
Expand All @@ -13,8 +15,7 @@ class SiretValidator extends ConstraintValidator
{
public function __construct(
private HttpClientInterface $inseeClient,
private string $apiKey,
private string $apiSecret)
private LoggerInterface $logger)
{}

public function validate($value, Constraint $constraint)
Expand All @@ -25,51 +26,23 @@ public function validate($value, Constraint $constraint)
return;
}

if (empty($this->apiKey) && empty($this->apiSecret)) {
return;
}

// Remove spaces
$value = preg_replace('/\s+/', '', $value);

try {

$this->inseeClient->request('GET', sprintf('entreprises/sirene/V3.11/siret/%s', $value), [
'headers' => [
'Authorization' => sprintf('Bearer %s', $this->getToken()),
]
]);
$this->inseeClient->request('GET', sprintf('siret/%s', $value));

} catch (ClientException $e) {

$data = $e->getResponse()->toArray(false);

$this->context->buildViolation($data['header']['message'])
->addViolation();
}

}

/**
* @see https://api.insee.fr/catalogue/site/themes/wso2/subthemes/insee/pages/help.jag
*/
private function getToken(): string
{
$base64 = base64_encode(sprintf('%s:%s', $this->apiKey, $this->apiSecret));

$response = $this->inseeClient->request('POST', 'token', [
'body' => [
'grant_type' => 'client_credentials',
'validity_period' => '604800',
],
'headers' => [
'Authorization' => sprintf('Basic %s', $base64),
]
]);

$data = $response->toArray();

return $data['access_token'];
} catch (ServerException $e) {
$this->logger->error($e->getResponse()->getContent(throw: false));
}
}
}

26 changes: 26 additions & 0 deletions tests/AppBundle/Entity/Sylius/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use AppBundle\Entity\LocalBusiness;
use AppBundle\Entity\ReusablePackaging;
use AppBundle\Entity\ReusablePackagings;
use AppBundle\Entity\Sylius\Customer;
use AppBundle\Entity\Sylius\Order;
use AppBundle\Sylius\Order\OrderItemInterface;
use AppBundle\Sylius\Order\OrderInterface;
Expand Down Expand Up @@ -176,4 +177,29 @@ public function testGetFormatsToDeliverForLoopeatGroupsContainers()

$this->assertEquals($expectedFormats, $order->getFormatsToDeliverForLoopeat());
}

public function testLoopeatCredentialsAreResolvedFromCustomer()
{
$order = new Order();
$customer = new Customer();

$order->setCustomer($customer);

$customer->setLoopeatAccessToken('123456');
$customer->setLoopeatRefreshToken('654321');

$this->assertEquals('123456', $order->getLoopeatAccessToken());
$this->assertEquals('654321', $order->getLoopeatRefreshToken());
}

public function testLoopeatCredentialsAreResolvedFromOrder()
{
$order = new Order();

$order->setLoopeatAccessToken('123456');
$order->setLoopeatRefreshToken('654321');

$this->assertEquals('123456', $order->getLoopeatAccessToken());
$this->assertEquals('654321', $order->getLoopeatRefreshToken());
}
}
9 changes: 2 additions & 7 deletions tests/AppBundle/LoopEat/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public function testCreateOrder()
{
$restaurant = new Restaurant();

$customer = $this->prophesize(CustomerInterface::class);
$customer
->getLoopeatAccessToken()
->willReturn('123456abcdef');

$order = $this->prophesize(OrderInterface::class);

$order
Expand All @@ -76,8 +71,8 @@ public function testCreateOrder()
->willReturn($restaurant);

$order
->getCustomer()
->willReturn($customer->reveal());
->getLoopeatAccessToken()
->willReturn('123456abcdef');

$order
->getFormatsToDeliverForLoopeat()
Expand Down

0 comments on commit a9d9468

Please sign in to comment.