2026-01-16 15:10:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Service\Signature;
|
|
|
|
|
|
2026-01-16 15:34:41 +01:00
|
|
|
use App\Entity\CustomerOrder;
|
|
|
|
|
use App\Entity\Devis;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
|
|
use Symfony\Component\HttpKernel\KernelInterface;
|
|
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface;
|
|
|
|
|
|
2026-01-16 15:10:27 +01:00
|
|
|
class Client
|
|
|
|
|
{
|
2026-01-16 15:34:41 +01:00
|
|
|
private \Docuseal\Api $docuseal;
|
|
|
|
|
private string $baseUrl;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
private readonly RequestStack $requestStack,
|
|
|
|
|
private readonly UrlGeneratorInterface $urlGenerator,
|
|
|
|
|
private readonly EntityManagerInterface $entityManager,
|
|
|
|
|
private readonly KernelInterface $kernel,
|
|
|
|
|
private readonly StorageInterface $storage,
|
|
|
|
|
) {
|
|
|
|
|
// Configuration via les variables d'environnement
|
|
|
|
|
$key = $_ENV['ESYSIGN_APIEY'] ?? '';
|
2026-01-19 18:22:53 +01:00
|
|
|
$this->baseUrl = $_ENV['SIGN_URL'];
|
2026-01-16 15:34:41 +01:00
|
|
|
|
|
|
|
|
// L'URL API est le point d'entrée pour le SDK Docuseal
|
2026-01-19 18:22:53 +01:00
|
|
|
$apiUrl = rtrim("https://signature.esy-web.dev", '/') . '/api';
|
2026-01-16 15:34:41 +01:00
|
|
|
$this->docuseal = new \Docuseal\Api($key, $apiUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Crée une soumission pour un Devis
|
|
|
|
|
*/
|
|
|
|
|
public function createSubmissionDevis(Devis $devis): string
|
|
|
|
|
{
|
|
|
|
|
// Si aucune signature n'est lancée, on initialise la soumission
|
|
|
|
|
if ($devis->getSignatureId() === null) {
|
|
|
|
|
|
|
|
|
|
// URL où le client sera redirigé après signature
|
|
|
|
|
$completedRedirectUrl = $this->baseUrl . $this->urlGenerator->generate(
|
|
|
|
|
'app_sign_complete',
|
|
|
|
|
['type' => 'devis', 'id' => $devis->getId()]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Récupération du fichier via VichUploader (champ devisDocuSealFile)
|
|
|
|
|
$relativeFileUrl = $this->storage->resolveUri($devis, 'devisDocuSealFile');
|
|
|
|
|
$fileUrl = $this->baseUrl . $relativeFileUrl;
|
|
|
|
|
|
|
|
|
|
$submission = $this->docuseal->createSubmissionFromPdf([
|
|
|
|
|
'name' => 'Devis N°' . $devis->getNum(), // Correction : getNum()
|
|
|
|
|
'completed_redirect_url' => $completedRedirectUrl,
|
|
|
|
|
'send_email' => true,
|
|
|
|
|
'documents' => [
|
|
|
|
|
[
|
|
|
|
|
'name' => 'devis_' . $devis->getNum() . '.pdf', // Correction : getNum()
|
|
|
|
|
'file' => $fileUrl,
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'submitters' => [
|
2026-01-19 18:22:53 +01:00
|
|
|
[
|
|
|
|
|
'role' => 'Ludikevent',
|
|
|
|
|
'email' => 'contact@ludikevent.fr',
|
|
|
|
|
'completed' => true,
|
|
|
|
|
],
|
2026-01-16 15:34:41 +01:00
|
|
|
[
|
|
|
|
|
'role' => 'Client',
|
|
|
|
|
'email' => $devis->getCustomer()->getEmail(),
|
|
|
|
|
'name' => $devis->getCustomer()->getSurname() . ' ' . $devis->getCustomer()->getName(),
|
|
|
|
|
'metadata' => [
|
|
|
|
|
'id' => $devis->getId(),
|
|
|
|
|
'type' => 'devis'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Stockage de l'ID submitter de Docuseal dans ton entité
|
2026-01-19 18:22:53 +01:00
|
|
|
$devis->setSignatureId($submission['submitters'][1]['id']);
|
2026-01-16 15:34:41 +01:00
|
|
|
$this->entityManager->flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->getLinkSign($devis->getSignatureId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retourne l'URL de signature publique pour le client
|
|
|
|
|
*/
|
|
|
|
|
public function getLinkSign(?string $submitterId): string
|
|
|
|
|
{
|
|
|
|
|
if (!$submitterId) {
|
|
|
|
|
throw new \InvalidArgumentException("ID Submitter absent.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$submissionData = $this->docuseal->getSubmitter($submitterId);
|
|
|
|
|
|
|
|
|
|
return rtrim($this->baseUrl, '/') . "/s/" . $submissionData['slug'];
|
|
|
|
|
}
|
2026-01-16 15:10:27 +01:00
|
|
|
|
2026-01-16 15:34:41 +01:00
|
|
|
/**
|
|
|
|
|
* Récupère l'état d'un signataire (pour vérifier si c'est signé)
|
|
|
|
|
*/
|
|
|
|
|
public function getSubmiter(?string $submitterId): array
|
2026-01-16 15:10:27 +01:00
|
|
|
{
|
2026-01-16 15:34:41 +01:00
|
|
|
return $this->docuseal->getSubmitter($submitterId);
|
2026-01-16 15:10:27 +01:00
|
|
|
}
|
2026-01-19 11:20:16 +01:00
|
|
|
|
|
|
|
|
public function status(): bool
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->docuseal->listTemplates();
|
|
|
|
|
return true;
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-16 15:10:27 +01:00
|
|
|
}
|