vanthao03596/ghtk-sdk

A simple PHP GHTK API client, Object Oriented, tested and documented.

0.0.3 2022-10-14 03:31 UTC

This package is auto-updated.

Last update: 2024-03-21 11:06:26 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f5048502532304748544b25323053444b2e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d76616e7468616f30333539362532466768746b2d73646b267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d412b73696d706c652b5048502b4748544b2b4150492b636c69656e742532432b4f626a6563742b4f7269656e7465642532432b7465737465642b616e642b646f63756d656e7465642e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d68747470732533412532462532467777772e7068702e6e6574253246696d616765732532466c6f676f732532466e65772d7068702d6c6f676f2e737667

Requirements

Quick install

Via Composer.

This command will get you up and running quickly with a Guzzle HTTP client.

composer require vanthao03596/ghtk-sdk guzzlehttp/guzzle:^7.0.1 http-interop/http-factory-guzzle:^1.0

Advanced install

We are decoupled from any HTTP messaging client with help by HTTPlug.

Using a different http client

composer require vanthao03596/ghtk-sdk symfony/http-client nyholm/psr7

To set up the GHTK client with this HTTP client

use Symfony\Component\HttpClient\HttplugClient;

$client = Client::createWithHttpClient(new HttplugClient(), true);

Basic usage of php-ghtk-api client

<?php

// This file is generated by Composer
require_once __DIR__ . '/vendor/autoload.php';

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$fee = $client->api('shipment')
    ->calculateFee([
        'pick_province' => 'Hà Nội',
        'pick_district' => 'Quận Hai Bà Trưng',
        'province' => 'Hà nội',
        'district' => 'Quận Cầu Giấy',
        'address' => 'P.503 tòa nhà Auu Việt, số 1 Lê Đức Thọ',
        'weight' => 1000,
        'value' => 3000000,
        'transport' => 'fly',
        'deliver_option' => 'xteam',
        'tags'  => [1]
    ]);

From $client object, you have access to all available GHTK api endpoints.

Create Order

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('order')->createOrder([
    'products' => [
        [
            'name' => 'bút',
            'weight' => 0.1,
            'quantity' => 1,
            'product_code' => '23304A3MHLMVMXX625'
        ],
        [
            'name' => 'tẩy',
            'weight' => 0.2,
            'quantity' => 1,
            'product_code' => ''
        ]
    ],
    'order' => [
        'id' => 'a9',
        'pick_name' => 'HCM-nội thành',
        'pick_address' => '590 CMT8 P.11',
        'pick_province' => 'TP. Hồ Chí Minh',
        'pick_district' => 'Quận 3',
        'pick_ward' => 'Phường 1',
        'pick_tel' => '0911222333',
        'tel' => '0911222333',
        'name' => 'GHTK - HCM - Noi Thanh',
        'address' => '123 nguyễn chí thanh',
        'province' => 'TP. Hồ Chí Minh',
        'district' => 'Quận 1',
        'ward' => 'Phường Bến Nghé',
        'hamlet' => 'Khác',
        'is_freeship' => 1,
        'pick_date' => '2016-09-30',
        'pick_money' => 47000,
        'note' => 'Khối lượng tính cước tối đa: 1.00 kg',
        'value' => 3000000,
        'transport' => 'fly',
        'pick_option' =>'cod',
        'deliver_option' => 'xteam',
        'pick_session' => '2',
        'tags' => [ 1],
        'email' => 'thao@123.com',
        'use_return_address' => 1
    ]
]);

Calculate shipment fee

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('shipment')->calculateFee([
    'pick_province' => 'Hà Nội',
    'pick_district' => 'Quận Hai Bà Trưng',
    'province' => 'Hà nội',
    'district' => 'Quận Cầu Giấy',
    'address' => 'P.503 tòa nhà Auu Việt, số 1 Lê Đức Thọ',
    'weight' => 1000,
    'value' => 3000000,
    'transport' => 'fly',
    'deliver_option' => 'xteam',
    'tags'  => [1]
]));

Check order status

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('order')->checkStatus('S1.A1.17373471');

Cancel order

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('order')->cancelOrder('S1.A1.17373471');

Print label

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$response = $client->api('order')->printLabel('S1.A1.17373471');

header('Content-type: application/pdf');
header('Content-disposition: attachment; filename=label.pdf');
header('Content-Transfer-Encoding: Binary');
echo $response;

Get list pick address

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('shipment')->listPickAddress();

Get address level 4

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('address')->getAddressLevel4([
    'province' => 'Hà nội',
    'district' => 'Quận Thanh Xuân',
    'ward_street' => 'Định Công',
    'address' => 'đường Đội Cấn, Ba Đình, Hà Nội',
]));

Get product info

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('inventory')->getProductInfo([
    'term' => 'laptop'
]));

Check XFAST service

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('shipment')->checkXteam([
    "customer_district" => "Quận Ba Đình",
    "customer_province" => "Hà Nội",
    "customer_ward" => "Phường Đội Cấn",
    "customer_first_address" => "12",
    "pick_province" => "Hà Nội",
    "pick_district" => "Quận Ba Đình",
    "pick_ward"     => "Phường Cống Vị"
]));

B2C - Create account

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('shop')->createShop([
    "name" => "shop test",
    "first_address" => "ngõ 2, Phan Bá Vành, Cầu Diễn",
    "province" => "Hà Nội",
    "district" => "Bắc Từ Liêm",
    "tel" => "01234555666",
    "email" => "shoptest@email.com"
]);

B2C - Get shop token

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

$client->api('shop')->getToken([
    "email" => "shoptest@email.com",
    "password" => "1S@fF#K2",
]));

B2C - Use Shop Token

$plugin = new Http\Client\Common\Plugin\HeaderSetPlugin([
    'X-Refer-Token' => 'B2CToken-hlsheiwquhrksadlfkjahsdfjaaljh',
]);

$builder = new Vanthao03596\GhtkSdk\HttpClient\Builder(new Http\Adapter\Guzzle7\Client);
$builder->addPlugin($plugin);

$client = new \Vanthao03596\GhtkSdk\Client(
    $builder, 
    true
);

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');

// Or use ReferToken plugin

$client = Vanthao03596\GhtkSdk\Client::createWithHttpClient(new Http\Adapter\Guzzle7\Client, true); // true is production mode

$client->authenticate('APITokenSample-ca441e70288cB0515F310742');
$client->setReferToken('B2CToken-hlsheiwquhrksadlfkjahsdfjaaljh');

....

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.