From 5092f78eb796f5af27516c60b7b531db607d8bfb Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 21 Jan 2022 20:31:57 +0300 Subject: [PATCH] external source id support --- examples/leads_actions.php | 38 +++++++++++++++++++++++++++++++ src/AmoCRM/Models/LeadModel.php | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/examples/leads_actions.php b/examples/leads_actions.php index db4d0dc5..065b8f40 100644 --- a/examples/leads_actions.php +++ b/examples/leads_actions.php @@ -233,3 +233,41 @@ function (AccessTokenInterface $accessToken, string $baseDomain) { var_dump($syncedElement); } + +// Рассмотрим кейс создания источника и создания сделки с этим источником +// ID источника на вашей стороне +$sourceExternalId = 'my-integration-super-id'; +// Создадим источник +$source = new \AmoCRM\Models\SourceModel(); +$source->setName('My super source') + ->setExternalId($sourceExternalId) + ->setPipelineId(4913583); + +$sourcesService = $apiClient->sources(); + +try { + $source = $sourcesService->addOne($source); +} catch (AmoCRMApiException $e) { + printError($e); + die; +} +echo 'Added source: '; +var_dump($source->toArray()); +echo PHP_EOL; + + +$lead = new LeadModel(); +$lead->setName('Название сделки') + ->setPrice(54321) + ->setSourceExternalId($sourceExternalId); + +try { + $lead = $leadsService->addOne($lead); +} catch (AmoCRMApiException $e) { + printError($e); + die; +} + +echo 'Added lead: '; +var_dump($lead->toArray()); +echo PHP_EOL; diff --git a/src/AmoCRM/Models/LeadModel.php b/src/AmoCRM/Models/LeadModel.php index 1f6fb9e6..3ccccaf7 100644 --- a/src/AmoCRM/Models/LeadModel.php +++ b/src/AmoCRM/Models/LeadModel.php @@ -135,6 +135,11 @@ class LeadModel extends BaseApiModel implements */ protected $sourceId; + /** + * @var string|null + */ + protected $sourceExternalId = null; + /** * @var CustomFieldsValuesCollection|null */ @@ -955,6 +960,14 @@ public function toApi(?string $requestId = "0"): array ]; } + // Источник можно передать только при создании + if (is_null($this->getId()) && !is_null($this->getSourceExternalId())) { + $result[AmoCRMApiRequest::EMBEDDED]['source'] = [ + 'type' => 'widget', + 'external_id' => $this->getSourceExternalId(), + ]; + } + $result['request_id'] = $this->getRequestId(); return $result; @@ -983,6 +996,13 @@ public function toComplexApi(?string $requestId = "0"): array $result[AmoCRMApiRequest::EMBEDDED]['metadata'] = $this->getMetadata()->toComplexApi(); } + if (is_null($this->getId()) && !is_null($this->getSourceExternalId())) { + $result[AmoCRMApiRequest::EMBEDDED]['source'] = [ + 'type' => 'widget', + 'external_id' => $this->getSourceExternalId(), + ]; + } + $result['request_id'] = $this->getRequestId(); return $result; @@ -1115,4 +1135,24 @@ public function setIsMerged(bool $isMerged): LeadModel return $this; } + + /** + * @return string|null + */ + public function getSourceExternalId(): ?string + { + return $this->sourceExternalId; + } + + /** + * @param string|null $sourceExternalId + * + * @return LeadModel + */ + public function setSourceExternalId(?string $sourceExternalId): LeadModel + { + $this->sourceExternalId = $sourceExternalId; + + return $this; + } }