Skip to content

About

PHP library for the Zoneless API. A read-only mirror of sdks/php in zonelessdev/zoneless: open issues and pull requests there.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

22 Commits

Folders and files

Repository files navigation

Zoneless PHP SDK

The PHP client for the Zoneless API. Zoneless is an open-source payments platform that settles in USDC on Solana.

The SDK has the same shape as Stripe's PHP SDK: the same services, method names, arguments, objects and exceptions. An integration written for \Stripe\StripeClient moves over by changing Stripe to Zoneless in the names it uses, and how it builds the client.

This is an early release. It covers every operation of the API.

Install

composer require zoneless/zoneless-php

It needs PHP 8.1 or later, with the curl and json extensions.

Usage

$client = new \Zoneless\ZonelessClient([
    'api_key' => 'sk_z_...',
    'api_base' => 'https://api.example.com',
]);

$customer = $client->customers->create(['email' => 'ana@example.com']);
echo $customer->id;

foreach ($client->customers->all(['limit' => 50])->autoPagingIterator() as $customer) {
    echo $customer->email;
}

api_base is the address of your Zoneless API. There is no default.

Services sit where they do in Stripe's SDK. The ones below a resource are methods of that resource, and the namespaces are properties of the client:

$person = $client->accounts->createPerson('acct_z_...', ['first_name' => 'Ana']);

$session = $client->checkout->sessions->create([
    'mode' => 'payment',
    'line_items' => [['price' => 'price_z_...', 'quantity' => 2]],
    'success_url' => 'https://example.com/thanks',
]);
$items = $client->checkout->sessions->allLineItems($session->id);

The endpoints Stripe does not have are methods of the service their path belongs to, such as $client->payouts->build().

Every method takes its IDs, then an array of parameters, then an array of options. An object reads by property ($customer->email) or by key ($customer['email']), and toArray() gives it as plain arrays.

Request options

Use the options to act as a connected account, or to retry a request safely with an idempotency key:

$client->customers->create(
    ['email' => 'ana@example.com'],
    ['zoneless_account' => 'acct_z_...', 'idempotency_key' => 'customer-ana']
);

stripe_account is accepted as another name for zoneless_account, in the options and in the settings of the client.

Retries

A request is sent once unless you ask for more. With max_network_retries, a request that fails to connect, or that the API answers with a 409 or a 5xx, is sent again after a pause that doubles each time:

$client = new \Zoneless\ZonelessClient([
    'api_key' => 'sk_z_...',
    'api_base' => 'https://api.example.com',
    'max_network_retries' => 2,
]);

$client->payouts->create(['amount' => 1200], ['max_network_retries' => 0]); // this request only

Every attempt of a POST carries the same idempotency key, so a request that did reach the API is not carried out twice.

Paying out

A payout is sent on Solana in a transaction the platform's wallet signs. The API builds the transaction and broadcasts it, and never sees the key: the SDK signs in between.

// Pays every pending payout, ten to a transaction
$results = $client->payouts->processAll(getenv('SOLANA_SECRET_KEY'));

// Or one batch
$result = $client->payouts->processBatch(getenv('SOLANA_SECRET_KEY'));
echo $result->status; // "paid"

The key is the secret key of the wallet, in base58. To do the steps yourself, sign what payouts->build() returns with payouts->sign($unsignedTransaction, $secretKey) and give the result to payouts->broadcast(). Signing needs PHP's sodium extension, which PHP comes with.

Errors

A request that fails throws an exception whose class says what went wrong. All of them extend \Zoneless\Exception\ApiErrorException:

try {
    $client->customers->retrieve('cus_z_missing');
} catch (\Zoneless\Exception\InvalidRequestException $e) {
    echo $e->getHttpStatus();   // 404
    echo $e->getError()->type;  // "resource_missing"
    echo $e->getMessage();      // "Customer not found"
}

The classes are InvalidRequestException, AuthenticationException, PermissionException, RateLimitException, IdempotencyException, UnknownApiErrorException for an error on the API's side, and ApiConnectionException when the request never reached the API.

Webhooks

$event = \Zoneless\Webhook::constructEvent(
    $payload,
    $_SERVER['HTTP_ZONELESS_SIGNATURE'],
    $endpointSecret
);

if ('customer.created' === $event->type) {
    $customer = $event->data->object;
}

constructEvent throws \Zoneless\Exception\SignatureVerificationException when the signature does not match.

Development

The library is developed in sdks/php of the Zoneless repository, and zonelessdev/zoneless-php is a mirror of that folder for Composer. Open issues and pull requests in the Zoneless repository. The commands below are run from its root.

The services and resource classes are generated from the OpenAPI spec. After changing the spec, run npx nx run php-sdk:generate.

The tests need PHP and Composer:

composer install --working-dir=sdks/php
npx nx run php-sdk:check

npx nx e2e api-e2e also runs the tests in e2e against a running API. It skips them on a machine without PHP.

Without PHP installed, Docker can stand in for the first two commands:

docker run --rm -v "$PWD/sdks/php":/app -w /app composer:2 composer install
docker run --rm -v "$PWD":/repo -w /repo composer:2 sdks/php/vendor/bin/phpunit --configuration sdks/php/phpunit.xml

License

MIT

About

PHP library for the Zoneless API. A read-only mirror of sdks/php in zonelessdev/zoneless: open issues and pull requests there.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages