PHP code example of elgibor-solution / laravel-delivery-gosend

1. Go to this page and download the library: Download elgibor-solution/laravel-delivery-gosend library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

elgibor-solution / laravel-delivery-gosend example snippets


'environment' => env('GOSEND_ENV', 'staging'),

'base_urls' => [
    'staging' => env('GOSEND_STAGING_URL', 'https://integration-kilat-api.gojekapi.com'),
    'production' => env('GOSEND_PRODUCTION_URL', 'https://kilat-api.gojekapi.com'),
],

'http' => [
    'timeout' => env('GOSEND_HTTP_TIMEOUT', 10),
    'retries' => env('GOSEND_HTTP_RETRIES', 2),
    'retry_sleep_ms' => env('GOSEND_HTTP_RETRY_SLEEP_MS', 250),
],

'routes' => [
    'enabled' => env('GOSEND_ROUTES_ENABLED', true),
    'prefix' => env('GOSEND_ROUTE_PREFIX', 'gosend'),
    'middleware' => ['api'],
],

'webhook' => [
    'token_header' => env('GOSEND_WEBHOOK_TOKEN_HEADER', 'X-Callback-Token'),
    'token' => env('GOSEND_WEBHOOK_TOKEN'),
    'route_name' => 'gosend.webhook',
],

use GoSend;
use ESolution\GoSend\Contracts\GoSendClientInterface;

// Load from DB (pseudo code)
$tenantConfig = [
    'environment' => $store->gosend_environment, // "staging" or "production"
    'credentials' => [
        $store->gosend_environment => [
            'client_id' => $store->gosend_client_id,
            'pass_key'  => $store->gosend_pass_key,
        ],
    ],
];

// Either via facade:
$client = GoSend::withConfig($tenantConfig);

// Or via dependency injection:
$baseClient = app(GoSendClientInterface::class);
$client = $baseClient->withConfig($tenantConfig);

// Now use $client for this tenant.

use GoSend;

$payload = [
    'paymentType' => 3,
    'shipment_method' => 'Instant',
    'routes' => [[
        'originName'             => 'Pak Andri',
        'originNote'             => 'Tunggu di lobby',
        'originContactName'      => 'The Kingdom Shop',
        'originContactPhone'     => '6285201311802',
        'originLatLong'          => '-6.1263348,106.7890888',
        'originAddress'          => 'Alamat lengkap asal',
        'destinationName'        => 'Pak Nando',
        'destinationNote'        => 'Tolong hati-hati',
        'destinationContactName' => 'Toko Jaya Agung',
        'destinationContactPhone'=> '6281254564161',
        'destinationLatLong'     => '-6.284508001748839,106.8295789',
        'destinationAddress'     => 'Alamat lengkap tujuan',
        'item'                   => 'Sepatu, Sendal, Kaos Kaki',
        'storeOrderId'           => 'AWB-123456',
        'insuranceDetails'       => [
            'applied'             => 'false',
            'fee'                 => '0',
            'product_description' => 'Sepatu, Sendal, Kaos Kaki',
            'product_price'       => '500.000',
        ],
    ]],
];

$delivery = GoSend::createBooking($payload);

// $delivery is ESolution\GoSend\Models\GoSendDelivery

use ESolution\GoSend\Contracts\GoSendClientInterface;

public function createForTenant(GoSendClientInterface $baseClient)
{
    $store = // ... load store/tenant model

    $client = $baseClient->withConfig([
        'environment' => $store->gosend_environment,
        'credentials' => [
            $store->gosend_environment => [
                'client_id' => $store->gosend_client_id,
                'pass_key'  => $store->gosend_pass_key,
            ],
        ],
    ]);

    $delivery = $client->createBooking($payload);
}

$status = GoSend::getStatusByOrderNo('GK-11-170563');

$delivery = \ESolution\GoSend\Models\GoSendDelivery::where('order_no', 'GK-11-170563')->first();

$status = GoSend::getStatusByStoreOrderId('AWB-123456');

GoSend::cancelBooking('GK-11-170563');

$estimate = GoSend::estimatePrice(
    originLatLong: '-6.1263348,106.7890888',
    destinationLatLong: '-6.284508001748839,106.8295789',
    paymentType: 3
);

// $estimate['Instant']['price']['total_price'] ...

namespace App\Listeners;

use ESolution\GoSend\Events\GoSendStatusUpdated;
use Illuminate\Support\Facades\Log;

class LogGoSendStatus
{
    public function handle(GoSendStatusUpdated $event): void
    {
        Log::info('GoSend status updated', [
            'order_no' => $event->delivery->order_no,
            'status'   => $event->delivery->status,
            'event'    => $event->payload['type'] ?? null,
        ]);
    }
}

protected $listen = [
    \ESolution\GoSend\Events\GoSendStatusUpdated::class => [
        \App\Listeners\LogGoSendStatus::class,
    ],
];

namespace App\Http\Controllers;

use ESolution\GoSend\Contracts\GoSendClientInterface;
use Illuminate\Http\Request;

class OrderDeliveryController extends Controller
{
    public function createForOrder(Request $request, GoSendClientInterface $baseClient)
    {
        $order = /* find your order */;

        $store = $order->store; // example relation

        $client = $baseClient->withConfig([
            'environment' => $store->gosend_environment,
            'credentials' => [
                $store->gosend_environment => [
                    'client_id' => $store->gosend_client_id,
                    'pass_key'  => $store->gosend_pass_key,
                ],
            ],
        ]);

        $payload = [
            'paymentType' => 3,
            'shipment_method' => 'Instant',
            'routes' => [[
                'originName'             => $order->seller_name,
                'originContactName'      => $order->seller_contact_name,
                'originContactPhone'     => $order->seller_phone,
                'originLatLong'          => $order->seller_latlong,
                'originAddress'          => $order->seller_address,
                'destinationName'        => $order->buyer_name,
                'destinationContactName' => $order->buyer_contact_name,
                'destinationContactPhone'=> $order->buyer_phone,
                'destinationLatLong'     => $order->buyer_latlong,
                'destinationAddress'     => $order->buyer_address,
                'item'                   => $order->item_summary,
                'storeOrderId'           => $order->awb_code,
            ]],
        ];

        $delivery = $client->createBooking($payload);

        $order->gosend_delivery_id = $delivery->id;
        $order->save();

        return response()->json([
            'message' => 'GoSend delivery created',
            'order_no' => $delivery->order_no,
        ]);
    }
}

namespace App\Jobs;

use ESolution\GoSend\Contracts\GoSendClientInterface;
use ESolution\GoSend\Models\GoSendDelivery;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class RefreshGoSendStatus implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public GoSendDelivery $delivery;

    public function __construct(GoSendDelivery $delivery)
    {
        $this->delivery = $delivery;
    }

    public function handle(GoSendClientInterface $client): void
    {
        if (! $this->delivery->order_no) {
            return;
        }

        $client->getStatusByOrderNo($this->delivery->order_no);
    }
}
bash
php artisan vendor:publish --provider="ESolution\GoSend\Providers\GoSendServiceProvider" --tag=gosend-config
php artisan vendor:publish --provider="ESolution\GoSend\Providers\GoSendServiceProvider" --tag=gosend-migrations
bash
php artisan migrate