WT CDEK library
- Category: Libraries
- Version: 1.0.0
- Date:
A small native PHP Joomla library for working with the CDEK delivery service API v.2. The package consists of a plugin for storing settings, a PHP library and map widgets for selecting order pick-up points. The library is a client for authorization in the CDEK API via OAuth, working with some API methods: obtaining a number of data and calculating the cost of delivery. Joomla 4.2.7 and higher is supported.
Description
Attention
This extension is intended for Joomla developers. They can use it as a dependency when developing their extensions for Joomla.
Pull Requests welcome from Joomla developers!
As part of the package
- WT CDEK library
- settings plugin for connecting to the CDEK API System - WT Cdek
- official widget for choosing the type of delivery from CDEK on Yandex map
The library is a client for connecting to the CDEK API and several methods for working with it with preliminary verification of the data structure, as well as error handling during the request. To work with the library, you must have the official CDEK API documentation at hand.
Connecting the library to your Joomla extension
<?php
use Webtolk\Cdekapi\Cdek;
defined('_JEXEC') or die('Restricted access');
$cdek = new Cdek();
// Zip code
$index_to = 410012;
// City name
$city = 'Saratov';
// Array of request parameters to the CDEK API
$cdek_city_options = ['size' => 1];
if (!empty($index_to))
{
$cdek_city_options['postal_code'] = trim((string) $index_to);
}
if (!empty($city))
{
$cdek_city_options['city'] = trim((string) $city);
}
$cdek_city = $cdek->getLocationCities($cdek_city_options);
List of library methods
- getDeliveryPoints
- The method is intended to obtain a list of existing SDEC offices. If the city_code, postal_code, and fias_guid parameters are specified at the same time, then city_code is given priority to determine the city of all countries of the SDEC presence, followed by fias_guid.
- getLocationRegions
- List of regions. The method is designed to obtain detailed information about the regions. The list of regions may be limited by user-defined characteristics. The parameters marked as obsolete have not been added to the list of request parameters.
- getLocationCities
- List of localities. The method is designed to obtain detailed information about localities. The list of localities may be limited by user-defined characteristics. The parameters marked as obsolete have not been added to the list of request parameters.
- getCalculatorTariff
- Calculator. Calculation based on the tariff code. The method is used to calculate the cost and delivery time according to the tariff code.
- getCalculatorTarifflist
- Calculator. Calculation for all available tariffs. The method is used by customers to calculate the cost and delivery time for all available tariffs.
- subscribeToWebhook
- Subscribe to Webhooks. The methods are designed to manage the subscription to receive webhooks at the client's URL. Since the SDEC test account is shared by all clients, only the SDEC combat URL must be used to test webhooks. In the subscription request, specify your test URL where the webhooks will be sent. After testing is complete, change it to your battle URL. If the client already has a subscription with the specified type, the old url is overwritten with the new one.
- CreateOrder
- Request for order registration.
- getTariffListShop
- An array with CDEK tariffs for the "online store" type, current at the time of the release of the library version.
- getTariffListDostavka
- An array with CDEK tariffs for the "delivery" type, current at the time of the release of the library version.
$request_options
, the structure of which must match the parameters of the CDEK API documentation request.Official widget for choosing the type of delivery from CDEK (selection of pick-up points on the map)
The library includes a widget of the latest version at the moment: 3.10.2.
Connecting the CDEK JavaScript widget
The javascript of the widget is designed as a Joomla Web Asset. In our code, we connect it using WebAssetManager as follows:
<?php
use Joomla\CMS\Factory;
defined('_JEXEC') or die('Restricted access');
$doc = Factory::getApplication()->getDocument();
/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = $doc->getWebAssetManager();
$wa->useScript('cdek-widget-umd');
Otherwise, when setting up, follow the widget documentation.
Widget Service.php
The widget is a Yandex.map that receives a list of order pick-up points using ajax. To work with this library, when initializing the widget, specify the servicePath
parameter - the url for the ajax request. By default, the file is included with the widget service.php, which is the entry point for the ajax request. In this library, the functionality of this file (getting a list of PVZ and calculating tariffs) has been transferred to the Joomla system plugin.
<?php
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Session\Session;
defined('_JEXEC') or die('Restricted access');
$service_url = new Uri(Uri::base());
$service_url->setPath('/index.php');
$service_url->setQuery([
'option' => 'com_ajax',
'plugin' => 'wtcdek',
'group' => 'system',
'format' => 'raw',
'city_code' => $city_code, // CDEK area code to get a list of pick-up points only for the selected city
Session::getFormToken() => 1
]);
// URL string
$service_url->toString();
For javascript, we use either Joomla Script options or php echo
, depending on the structure of your extension.
Joomla
- Extension type:
- Package
- Package composition:
- Library, Plugin
- Joomla version:
- 4.2.7