cboxdk / tax-client
Cbox Tax PHP: the official client for the Cbox Tax API. Calculate VAT, GST and sales tax, record transactions, and verify webhooks.
Requires
- php: ^8.4
- ext-curl: *
- ext-json: *
Requires (Dev)
- laravel/pint: ^1.18
- nyholm/psr7: ^1.8
- pestphp/pest: ^4.0 || ^5.0
- phpstan/phpstan: ^2.1
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.1 || ^2.0
Suggests
- psr/http-client: To send requests through your own PSR-18 client with Psr18Transport.
- psr/http-factory: Required by Psr18Transport to build PSR-7 requests.
- psr/http-message: Required by Psr18Transport.
Provides
None
Conflicts
None
Replaces
None
README
The PHP client for the Cbox Tax API: VAT, GST and US sales tax for a basket, with the reason and the source for every rate.
- Typed, readonly models generated from the OpenAPI spec. Amounts and rates stay
strings (
"100.00"), never floats. - Retries that are safe: GETs, and writes that carry an
Idempotency-Key. The SDK generates the key for you. - One exception per error type, each with the
Request-Idto quote to support. - Webhook signature verification.
- No runtime dependencies beyond ext-curl and ext-json. Bring your own PSR-18 client if you like.
Install
composer require cboxdk/tax-client
PHP 8.4 or newer. See docs/requirements.md.
Quickstart
use Cbox\TaxClient\CboxTax; $tax = new CboxTax(apiKey: getenv('CBOXTAX_API_KEY')); // sk_test_… or sk_live_… // 1. Calculate a basket. $calculation = $tax->calculations->create([ 'seller' => 'sel_8f2k1qZ0aB3cD4eF5gH6iJ', 'customer' => ['type' => 'consumer', 'address' => ['country' => 'DK']], 'currency' => 'DKK', 'lines' => [ ['id' => 'line-1', 'amount' => '100.00'], ['id' => 'line-2', 'amount' => '250.00', 'category' => 'goods.publications.book'], ], ]); echo $calculation->totals->tax; // "87.50" echo $calculation->lines[0]->reason; // why, in a sentence an accountant can read // 2. Record it as a sale under your invoice number. $transaction = $tax->calculations->finalize($calculation->id, ['reference' => 'INV-2026-0042']); // 3. Credit it: all of it, or per line. $reversal = $tax->transactions->reverse($transaction->id, ['reason' => 'Returned']); // 4. Verify a webhook delivery (raw body, byte for byte). $event = $tax->webhooks->verify( payload: file_get_contents('php://input'), signatureHeader: $_SERVER['HTTP_X_CBOX_SIGNATURE'] ?? '', secrets: getenv('CBOXTAX_WEBHOOK_SECRET'), );
Every model returned by a call carries ->meta: the HTTP status, the
requestId, and whether an idempotent write was replayed.
Errors
Every failure is a Cbox\TaxClient\Exceptions\CboxTaxException. Catch the subclass
you can act on:
use Cbox\TaxClient\Exceptions\RefusedException; use Cbox\TaxClient\Exceptions\CboxTaxException; try { $tax->calculations->create($basket); } catch (RefusedException $e) { // Not a failure: the API will not guess. It names what would answer it. $e->param; // "customer.address.subdivision" $e->reason; // "address_incomplete" $e->remedy; // "Send the subdivision (ISO 3166-2) of the delivery address." $e->callerCanClose; // true: that step is yours to take } catch (CboxTaxException $e) { log_error($e->type, $e->detail, $e->requestId); }
| Exception | error.type |
Status |
|---|---|---|
InvalidRequestException |
invalid_request, method_not_allowed |
400, 405 |
AuthenticationException |
authentication_required, invalid_api_key, invalid_token |
401 |
PlanLimitException |
plan_limit_reached |
402 |
PermissionException |
insufficient_scope, no_organization |
403 |
NotFoundException |
not_found |
404 |
IdempotencyException |
idempotency_conflict, idempotency_in_progress |
409 |
RefusedException |
refused |
422 |
RateLimitException |
rate_limited |
429 |
ApiException |
api_error, any other 5xx |
500 |
ServiceUnavailableException |
source_unavailable, service_misconfigured |
503 |
TaxIdUnverifiedException |
tax_id_unverified |
503 |
ConnectionException |
no response (DNS, TLS, timeout) | none |
Retries and idempotency
Every POST, PATCH and DELETE gets a UUID v4 Idempotency-Key unless you pass one.
The same key and the same bytes go out on every retry, so a retried write is applied
once.
The client retries (twice by default) on a connection failure, a 429 (after
Retry-After, capped at 60 seconds), a 409 idempotency_in_progress, and a 5xx
other than service_misconfigured. The backoff is exponential with jitter: 0.5s, then 1s, then 2s,
up to 8s. It never retries a refusal, a validation error or a conflict.
use Cbox\TaxClient\RequestOptions; // Your own key: safe to retry across processes, e.g. from a queue job. $tax->transactions->create($sale, new RequestOptions(idempotencyKey: 'order-1234')); // Per-call overrides. $tax->transactions->list([], new RequestOptions(timeout: 5.0, maxRetries: 0));
Pagination
$page = $tax->transactions->list(['seller' => $sellerId, 'limit' => 100]); $page->data; // list<Transaction> $page->hasMore; foreach ($tax->transactions->all(['seller' => $sellerId]) as $transaction) { // every page, fetched as the loop reaches it }
Your own HTTP client
The default transport is ext-curl. To send requests through a PSR-18 client (Guzzle, Symfony HttpClient) with your proxies or middleware:
use Cbox\TaxClient\Http\Psr18Transport; $tax = new CboxTax( apiKey: $key, transport: new Psr18Transport($psr18Client, $psr17Factory, $psr17Factory), );
Testing your code
Cbox\TaxClient\Testing\FakeTransport answers from queued responses and records
every request. FakeSleeper removes the waits between retries. See
docs/getting-started/testing.md.
Documentation
docs/index.md covers the concepts, recipes and extension points.
License
MIT. See LICENSE.