Support-Integrationen

Neues Ticket erstellen

createTicket()

Diese Funktion wird aufgerufen, wenn ein Kunde im Storefront ein neues Ticket erstellt. Sie wird in der folgenden Datei aufgerufen:

app/Http/Controllers/Storefront/Account/SupportTicketController.php Line 125

Parameter

Typ

Name

Beschreibung

SalesChannel

salesChannel

Der salesChannel

Customer oder null

customer

Das Customer-Objekt. Wenn null, werden guestName und guestMail ausgefüllt. (Das Ticket wurde ohne Login über Kontaktformulare erstellt)

string

subject

Der Ticket-Betreff.

string

message

Die erste Nachricht.

ProductHosting oder Domain oder null

product

Das ausgewählte ProductHosting oder Domain oder null. Mit instanceof() prüfen

TicketDepartment oder null

department

Das ausgewählte Department

array

attachments

Array von Ticket-Anhängen

String oder null

guestName

Wenn customer null ist, enthält dies den Gastnamen

String oder null

guestMail

Wenn customer null ist, enthält dies die Gast-E-Mail

Antwort

TicketCreatedResponse()-Klasse.

Typ

Name

Beschreibung

mixed

$id

Die ID des Tickets. In der Regel Integer oder UUID.

mixed

$number

Die Nummer des Tickets, die bei der Erstellung generiert wurde.

mixed

$messageId

Die ID der ersten Nachricht.

array

$attachmentFilePaths

Array von Dateipfaden in derselben Reihenfolge wie die Dateien.

Beispielcode

public function createTicket(
	SalesChannel      $salesChannel,
	?Customer         $customer,
	string            $subject,
	string            $message,
	mixed             $product,
	?TicketDepartment $department = null,
	array             $attachments = [],
	?string           $guestName = null,
	?string           $guestMail = null,
): TicketCreatedResponse {
	$mailboxId = $department->provider_department_id["freescout"] ?? $this->provider->default_department_id;
	$customerId = $this->getCustomerId($customer, $guestName, $guestMail);
	$additionalTexts = [];


	if($customer !== null) {
		$customerViewUrl = $salesChannel->url . route('admin.customer.edit', $customer->id, false);
		$additionalTexts = [
			"Ticket aus hostware in Freescout erstellt",
			"Verkaufskanal: {$salesChannel->name}",
			"<a href='{$customerViewUrl}'>Kunde ansehen (#{$customer->customer_number})</a>"
		];

		if ($product !== null) {
			$productRoute = "N-A";
			$productText = "N/A";

			if ($product instanceof Domain) {
				$productRoute = route('admin.domain.view', $product->fqdn, false);
				$productText = "{$product->fqdn} (Domain)";
			}
			if ($product instanceof ProductHosting) {
				$productRoute = route('admin.hosting.hostings.view', $product->id, false);
				$productText = "{$product->product->name} #{$product->product_hosting_number} (Hosting)";
			}

			$completeUrl = $salesChannel->url . $productRoute;

			$additionalTexts[] = '<a href="' . $completeUrl . '">Verlinktes Produkt ansehen (' . $productText . ')</a>';
		}
	}

	$threadAttachments = [];
	foreach ($attachments as $attachment) {
		$threadAttachments[] = [
			'fileName' => $attachment->getClientOriginalName(),
			'mimeType' => $attachment->getMimeType(),
			'data' => base64_encode($attachment->getContent()),
		];
	}


	$response = $this->makeRequest("conversations", "POST", [
		"type" => "email",
		"mailboxId" => $mailboxId,
		"subject" => $subject,
		"customer" => [
			'id' => $customerId,
		],
		"threads" => [[
			'text' => $message,
			'type' => 'customer',
			'customer' => [
				'id' => $customerId,
			],
			"attachments" => $threadAttachments
		], [
			'text' => implode("<br>", $additionalTexts),
			'type' => 'note',
			'user' => $this->_getFreescoutUserId()
		]],
		"createdAt" => now()->setTimezone("UTC")->toISOString(),
		"status" => "active"
	]);

	/**
	 * Get the attachment IDs
	 */
	$attachmentResponse = [];
	foreach ($response->_embedded->threads[0]->_embedded->attachments as $file) {
		$attachmentResponse[] = $file->id;
	}


	Log::debug("Freescout ticket creation response", ["res" => $response]);

	return new TicketCreatedResponse(
		$response->id,
		$response->number,
		$response->_embedded->threads[0]->id,
		$attachmentResponse
	);
}

/**
The function below is not required by the interface, but it gets used in the createTicket() of Freescout.

This method searches or creates a customer / contact at the ticket api using the given customer.
*/
private function getCustomerId(?Customer $customer, ?string $guestName = null, ?string $guestMail = null,) {
	if($customer === null) {
		/**
		 * Create a guest in freescout
		 */
		$allCustomers = $this->makeRequest("customers", "GET", [
			"email" => $guestMail
		]);

		if (count($allCustomers->_embedded->customers) === 1) {
			return $allCustomers->_embedded->customers[0]->id;
		}

		$response = $this->makeRequest("customers", "POST", [
			"firstName" => $guestName,
			"lastName" => "-",
			"notes" => "Aus hostware als Gast erstellt",
			"emails" => [[
				'value' => $guestMail,
				'type' => 'home',
			]]
		]);

		return $response->id;
	}

	/**
	If the customer already has a contact saved in the hostware database, use that.
	**/
	if (isset($customer->support_ticket_providers['freescout'])) {
		return $customer->support_ticket_providers['freescout'];
	}


	/**
	 * Search existing customers for the email
	 */
	$allCustomers = $this->makeRequest("customers", "GET", [
		"email" => $customer->email
	]);

	if (count($allCustomers->_embedded->customers) === 1) {
		$customer->addTicketProviderId("freescout", $allCustomers->_embedded->customers[0]->id);

		return $allCustomers->_embedded->customers[0]->id;
	}


	/**
	If no existing contact was found, create a new one.
	*/
	$firstAddress = $customer->firstAddress();
	$customerViewUrl = $customer->salesChannel->url . route('admin.customer.edit', $customer->id, false);

	$response = $this->makeRequest("customers", "POST", [
		"firstName" => $customer->first_name,
		"lastName" => $customer->last_name,
		"phone" => $customer->phone,
		"photoUrl" => null,
		"jobTitle" => null,
		"photoType" => null,
		"address" => array(
			'city' => $firstAddress->city,
			'state' => '-',
			'zip' => $firstAddress->zipcode,
			'country' => $firstAddress->country,
			'address' => $firstAddress->street_full,
		),
		"notes" => "Aus hostware importiert\n\n{$customerViewUrl}",
		"company" => $customer->company,
		"emails" => [[
			'value' => $customer->email,
			'type' => 'home',
		]],
		"phones" => [[
			'value' => $customer->phone,
			'type' => 'home',
		]]
	]);

	// Save the contact id to customer in hostware.
	$customer->addTicketProviderId("freescout", $response->id);
	return $response->id;
}

Testen

Du kannst diese Funktion testen, indem Du im Storefront ein Ticket erstellst.

War das hilfreich?