vldmr-k / yii2-novaposhta

Yii2 extension for Nova Poshta API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Yii2 extension for Nova Poshta API

IMPORTANT! Extension is under development

Extension to work with Nova Poshta API

Latest Stable Version Scrutinizer Code Quality Total Downloads Build Status License Code Coverage

Installation via Composer

Since this is a fork repository, and original repo is not available on Packagist, you need to add this to your composer.json

  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/Webkadabra/yii2-novaposhta"
    }
  ],

After that, you can install package with a command:

composer require webkadabra/yii2-novaposhta

Setup

All models methods and properties has the same as in official documentation.

  1. Setup configuration:
'components' => [
    'novaposhta' => [
        'class' => 'jones\novaposhta\Api',
        'api_key' => 'specify your api key',
        'format' => 'json' \\supported json and xml formats
    ]
]

The json format more appreciated, because response formatting faster.

  1. Set alias for extension in bootstrap.php file:
Yii::setAlias('novaposhta', '@vendor/webkadabra/yii2-novaposhta');

Or, if you use Yii2 Advanced template, add this to your common/config/bootstrap.php

Yii::setAlias('novaposhta', dirname(dirname(__DIR__)) . '/vendor/webkadabra/yii2-novaposhta');

  1. Configure extension translations:
'components' => [
    'i18n' => [
        'translations' => [
            'api' => [
                'class' => 'yii\i18n\PhpMessageSource',
                'basePath' => '@novaposhta/messages',
                'sourceLanguage' => 'en',
            ],
        ],
    ],
]
  1. Create new api model:
$model = new \jones\novaposhta\Address(new \jones\novaposhta\request\RequestFactory());

or

$model = Yii::createObject(\jones\novaposhta\Address::class);

in the second case request factory will be created by Yii DI manager.

  1. Process request:
$areas = $model->getAreas();

Each model has list of rules where described attributes per each scenario. If some api call will be processed without required attributes you will get false instead normal response. To get list of errors just call:

$model->getErrors();

Use more then one api token

$model = new \jones\novaposhta\Address(new \jones\novaposhta\request\RequestFactory());

$token1 = 'token1';
$factory1 = new RequestFactory($token);
$model->setRequestFactory($factory1);

$token2 = 'token2';
$factory2 = new RequestFactory($token);
$model->setRequestFactory($factory2);

List of available models

  • Address
  • InternetDocument (under development)
  • Common (under development)
  • ContactPerson (under development)
  • Counterparty (under development)
  • ScanSheet (under development)
  • AdditionalService

List of implemented methods

Address model

  • getAreas

    $areas = $addressModel->getAreas();
  • getCities

    $cities = $addressModel->getCities('Бровари'); // filter not empty add it to `FindByString` request param
  • delete

    $addressModel->Ref = 'fs1d2vbv12'; // if Ref is not specified you will get validation error
    $addressModel->delete();
  • getWarehouses

    $addressModel->CityRef = 'df1j2cmf5d';  // CityRef is required parameter
    $warehouses = $addressModel->getWarehouses('Броварський'); // Street name is additional parameter
  • getStreet

    $streets = $addressModel->getStreet('city ref', 'street name');
  • getWarehouseTypes

    $types = $addressModel->getWarehouseTypes();
  • save

    $addressModel->StreetRef = 'd8364179-4149-11dd-9198-001d60451983';
    $addressModel->CounterpartyRef = '56300fb9-cbd3-11e4-bdb5-005056801329';
    $response = $addressModel->save('10', 12, 'Comment');   // building should be in string type
  • update

    $addressModel->Ref = '503702df-cd4c-11e4-bdb5-005056801329';
    $addressModel->CounterpartyRef = '56300fb9-cbd3-11e4-bdb5-005056801329';
    $response = $addressModel->save('10', 12, 'Comment');   // building should be in string type

Counterparty model

  • getAreas

    $counterparties = $counterpartyModel->getCounterparties();
  • getCounterpartyAddresses

    $addresses = $counterpartyModel->getCounterpartyAddresses('6e9acced-d072-11e3-95eb-0050568046cd', 'Sender');
  • getCounterpartyContactPersons

    $persons = $counterpartyModel->getCounterpartyContactPersons('6e9acced-d072-11e3-95eb-0050568046cd');
  • save

    // set model attributes
    $counterpartyModel->cityRef = '0050568046cd';
    .
    .
    .
    $counterpartyModel->Phone = '0567894512';
    $response = $counterpartyModel->save();
  • update

    // set model attributes
    $counterpartyModel->Ref = '005056801329';
    .
    .
    .
    $counterpartyModel->Email = 'contractor@test.com';
    $response = $counterpartyModel->update();
  • delete

    $counterparty->Ref = '0050568002cf'; // if Ref is not specified you will get validation error
    $counterparty->delete();

About

Yii2 extension for Nova Poshta API

License:MIT License


Languages

Language:PHP 100.0%