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-10-21 12:23:15 UTC


README

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

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.