How to create a new license key for one or more projects. Sample code for developers to use in their software solutions.
License keys are most often provided to users on a paid basis. The SW JProjects component currently does not provide functionality for accepting payments and generating keys after successful payment. However, you can easily automate this process by arranging payment acceptance using any other e-commerce component.
As a rule, in all such components, after the payment event, an event is called for the Joomla plugin system (Event Dispatching pattern). When an event is triggered, all plugins of the system
group and plugins of a specific group subscribed to this event are triggered. Thus, it is enough for you to write a plugin for your component, which will automatically create a license key at the right time and under the right conditions.
Sample code for creating a license key
It is assumed that this code is in the plugin. However, it can be used in other places of Joomla with minor modifications.
<?php
use Joomla\CMS\Router\Route;
use Joomla\Component\SWJProjects\Site\Helper\RouteHelper;
/**
* Generate download key for SW JProejects project
*
* @param int $order_number Order number
* @param int $project_id Project id. Or it can be an array of projects ids
* @param string $email Customer email
* @param string $note Your admin note for this key
* @param string $domain WWW-domain for key if you need
* @param string $date_start Key expiration date start
* @param string $date_end Key expiration date end
* @param int $user_id Joomla user id
*
*/
$modelProject = $this->getApplication()
->bootComponent('com_swjprojects')
->getMVCFactory()
->createModel('Project', 'Site', ['ignore_request' => false]);
/** @var array $keyData Data we will return for customer after successful payment */
$keyData = [];
if (!$project = $modelProject->getItem($project_id)) {
// We have tried to create a key for non-existent project
return;
}
/** @var \Joomla\Component\SWJProjects\Administrator\Model\KeyModel $modelKey */
$modelKey = $this->getApplication()
->bootComponent('com_swjprojects')
->getMVCFactory()
->createModel('Key', 'Administrator', ['ignore_request' => true]);
$data = [
// string empty for new key
'key' => '',
// int 0 for new key
'id' => 0,
// array Array of project ids
'projects' => [$project_id],
'order' => $order_number,
'email' => $email,
'date_start' => $date_start,
'date_end' => $date_end,
// int Published or not.
// If not, you will receive an error
// when you try to download a file using this key.
'state' => 1,
// Your admin note for this key
'note' => $note,
// WWW-domain for key if you need
'domain' => $domain,
];
// Bind the Joomla user id
if ($user_id > 0) {
$data['user'] = $user_id;
}
// Save data
if ($modelKey->save($data)) {
$key = $modelKey->getItem();
// Get the download link with download key for this project.
// If you generate a key for multiple projects -
// create a download link for each project in a loop
$downloadLink = Route::_(
RouteHelper::getDownloadRoute(
null,
$project_id,
$project->element,
$key->key
)
);
$keyData['key'] = $key;
$keyData['download_link'] = $downloadLink;
}