WT CDEK library
- Category: Libraries
- Version: 1.3.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 tasks plugin of the standard Joomla task scheduler for copying and updating CDEK data to a local database
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
Install the pkg_lib_wtcdek package as a regular Joomla extension. After installation, enable the System - WT Cdek plugin, fill in client_id and client_secret, and configure scheduler:run through CRON if you use the Joomla Scheduler.
Quick start
<?php
use Webtolk\Cdekapi\Cdek;
\defined('_JEXEC') or die;
$cdek = new Cdek();
// or
$cdek = new Cdek(test_mode: true, client_id: 'your_client_id', client_secret: 'your_client_secret');
Authorization is handled automatically, and the access token is cached through Joomla.
Main changes in 1.3.0
- the library was moved to an entity-based API with the
Cdekfacade - API logic was split into dedicated entity classes
- new entities were added:
webhooks,prealert,photoDocument,reverse,passport,payment,print,check,intakes,oauth - installer script,
TarifflistField,TariffinfoField, and updated assets were added location()->suggestCities()now supports thecountry_codeparameter
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, 5.2.1, 5.2.2, 6.0.3