com-company/yousign-bundle
Bundle Symfony pour intégrer Yousign (API v3, compat v2) : configuration via variables d’environnement, envoi de demandes de signature et gestion des webhooks (routes, listeners/handlers, payloads) avec binding d’événements.
## Getting Started
### Minimal Setup
1. **Installation**
```bash
composer require com-company/yousign-bundle
Add the bundle to config/bundles.php:
return [
// ...
ComCompany\YousignBundle\YousignBundle::class => ['all' => true],
];
Environment Configuration
Set .env variables for V3 (focused on this package):
YOUSIGN_V3_URI=https://api-sandbox.yousign.app/v3/ # or prod
YOUSIGN_V3_TOKEN=your_v3_api_token
YOUSIGN_V3_ACCESS_KEY=your_webhook_key_if_needed
First Use Case: Sending a Signature Request with Custom Signing Date
Inject the YousignClient service and use it to create a signature workflow with a predefined signing date:
use ComCompany\YousignBundle\Client\YousignClient;
use ComCompany\YousignBundle\Model\SignatureRequest;
use ComCompany\YousignBundle\Model\Signer;
public function createSignatureWithDate(YousignClient $client)
{
$signer = new Signer();
$signer->setEmail('user@example.com');
$signer->setSigningDate('2024-12-31'); // New feature: Set a specific signing date
$request = new SignatureRequest();
$request->setSubject('Contract Approval');
$request->setSigners([$signer]);
$request->setDocuments([['url' => 'https://example.com/contract.pdf']]);
$response = $client->sendSignatureRequest($request);
return $response->getUrl(); // Redirect URL for signer
}
Signature Requests with Signing Dates
setSigningDate() method on the Signer object to specify when the signer must sign.
$signer->setSigningDate('YYYY-MM-DD'); // ISO 8601 format
Signature Requests
YousignClient::sendSignatureRequest() with a SignatureRequest object.YousignClient::getSignatureRequestStatus() or use webhooks (see below).YousignClient::createTemplate() and reuse them.Webhooks (Event-Driven)
config/routes/yousign.yaml (as per README).EventHandlerInterface for each event (e.g., signature_request_created, signature_request_signed).
// Example: src/EventHandler/SignatureSignedHandler.php
use ComCompany\YousignBundle\Event\WebhookPayload;
use ComCompany\YousignBundle\Event\EventHandlerInterface;
class SignatureSignedHandler implements EventHandlerInterface
{
public function handle(WebhookPayload $payload): void
{
$event = $payload->getEvent();
if ($event === 'signature_request_signed') {
$data = $payload->getData();
if (isset($data['signing_date'])) {
// Handle custom signing date logic
}
}
}
}
Document Management
YousignClient::uploadDocument() and attach them to requests.Error Handling
YousignException:
try {
$response = $client->sendSignatureRequest($request);
} catch (YousignException $e) {
// Log or notify (e.g., $e->getMessage())
if (str_contains($e->getMessage(), 'signing_date')) {
// Handle date-specific validation errors
}
}
YOUSIGN_V3_URI) and mock the YousignClient in PHPUnit:
$client = $this->createMock(YousignClient::class);
$client->method('sendSignatureRequest')->willReturn(new SignatureResponse());
$this->assertEquals('2024-12-31', $client->getLastSigningDate());
Environment Variables
YOUSIGN_V3_* variables, not V2_* ones.YOUSIGN_V3_URI will throw RuntimeException on client initialization.Signing Date Validation
$signingDate = new \DateTime('2024-12-31');
if ($signingDate < new \DateTime()) {
throw new \InvalidArgumentException('Signing date must be in the future.');
}
Webhook Quirks
YOUSIGN_V3_ACCESS_KEY is set and matches the key in the Yousign dashboard.yousign.yaml prefix to avoid conflicts with existing routes.signing_date for signed events. Use $payload->getData() and cast to WebhookPayload for type safety.Rate Limiting
use Symfony\Component\HttpClient\RetryableHttpClient;
$client = new RetryableHttpClient(
$httpClient,
[
'max_retries' => 3,
'delay' => 1000, // ms
'multiplier' => 2,
]
);
Document URLs
SignatureRequest are publicly accessible (Yousign fetches them via URL).Logging
Enable debug mode in config/packages/yousign.yaml:
yousign:
debug: true # Logs API requests/responses to monolog
Check logs for raw payloads during webhook testing, especially for signing_date fields.
Webhook Testing
signing_date field.Common Errors
401 Unauthorized: Verify YOUSIGN_V3_TOKEN is correct and not expired.400 Bad Request: Validate SignatureRequest data (e.g., required fields like subject, signers, or signing_date format).500 Internal Server Error: Check Yousign’s status page for outages.Custom Events for Signing Dates
Extend the bundle’s event system to handle signing_date in webhooks or custom events:
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
$dispatcher->dispatch(new SigningDateEvent($payload->getData()['signing_date']));
Service Decorators for Date Logic
Decorate YousignClient to add pre/post-processing logic for signing dates:
class CustomYousignClientDecorator implements YousignClientInterface
{
public function __construct(private YousignClientInterface $decorated)
{}
public function sendSignatureRequest(SignatureRequest $request): SignatureResponse
{
// Pre-process: Validate or modify signing dates
$this->validateSigningDates($request->getSigners());
$response = $this->decorated->sendSignatureRequest($request);
// Post
How can I help you explore Laravel packages today?