dzangocart/client
PHP client library for dzangocart, providing a simple way to connect to the dzangocart API from your application. Suitable for integrating dzangocart services into Laravel or other PHP projects.
Installation
composer require dzangocart/client
Ensure your Laravel project meets the PHP version requirement (>=5.3.3).
First Use Case: Authentication Initialize the client with your API credentials:
use DzangoCart\Client;
$client = new Client([
'api_key' => env('DZANGOCART_API_KEY'),
'api_secret' => env('DZANGOCART_API_SECRET'),
'base_uri' => 'https://api.dzangocart.com/v1',
]);
First Request: Fetching Products
$products = $client->get('/products')->json();
src/Client.php for core methods (e.g., get(), post()).API Integration Layer Create a Laravel service class to abstract API calls:
namespace App\Services;
use DzangoCart\Client;
class DzangoCartService {
protected $client;
public function __construct() {
$this->client = new Client(config('dzangocart'));
}
public function fetchProducts() {
return $this->client->get('/products')->json();
}
}
Request/Response Handling Use Laravel’s HTTP clients for consistency:
$response = $this->client->post('/orders', [
'json' => ['items' => $orderData]
]);
$response->raiseForStatus(); // Throw on 4xx/5xx
Configuration
Store credentials in .env and bind to Laravel config:
// config/dzangocart.php
return [
'api_key' => env('DZANGOCART_API_KEY'),
'api_secret' => env('DZANGOCART_API_SECRET'),
];
$client->getStack()->push(
\GuzzleHttp\Middleware::tap(function ($request) {
logger()->debug('DzangoCart Request:', ['url' => $request->getUri()]);
})
);
DzangoCartOrderCreated).Cache::remember():
Cache::remember('dzangocart_products', now()->addHours(1), function () {
return $this->client->get('/products')->json();
});
Deprecated Guzzle Version
~3.0, which is outdated. Upgrade to Guzzle 6/7+ and fork the package if needed.composer.json.No Rate Limiting
throttle middleware.Lack of Type Safety
collect() or json_decode with strict typing:
$products = collect($response->json())->mapInto(Product::class);
$client->getStack()->push(
\GuzzleHttp\Middleware::log(\Psr\Log\LogLevel::DEBUG, new \Monolog\Logger('guzzle'))
);
Authorization headers are set correctly (e.g., Bearer token).Custom Endpoints Extend the client class to add domain-specific methods:
class ExtendedClient extends Client {
public function createOrder(array $data) {
return $this->post('/orders', ['json' => $data]);
}
}
Webhook Handling
Use Laravel’s HandleIncomingWebhook trait or a dedicated route:
Route::post('/dzangocart/webhook', [DzangoCartWebhookController::class, 'handle']);
Testing Mock the client in tests:
$mock = Mockery::mock(DzangoCart\Client::class);
$mock->shouldReceive('get')->andReturn((object)['json' => fn() => []]);
pcrypt for secret storage. Ensure the opichon/pcrypt package is installed.$client = new Client([
'base_uri' => config('app.env') === 'production'
? 'https://api.dzangocart.com/v1'
: 'https://staging.dzangocart.com/v1',
]);
How can I help you explore Laravel packages today?