PHP code example of wpzinc / airwallex-php

1. Go to this page and download the library: Download wpzinc/airwallex-php 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/ */

    

wpzinc / airwallex-php example snippets


use WPZinc\Airwallex\AirwallexClient;

$airwallex = new AirwallexClient([
    'client_id'   => getenv('AIRWALLEX_CLIENT_ID'),
    'api_key'     => getenv('AIRWALLEX_API_KEY'),
    'environment' => 'sandbox', // or 'production' (the default)
]);

$intent = $airwallex->paymentIntents->create([
    'amount'            => 49.99,
    'currency'          => 'SGD',
    'merchant_order_id' => 'order_1001',
]);

echo $intent->id;            // property access
echo $intent['client_secret']; // or array access
echo $intent->latest_payment_attempt?->status; // nested objects

$airwallex->customers->retrieve('cus_...');
$airwallex->refunds->create(['payment_intent_id' => 'int_...', 'amount' => 10]);
$airwallex->transfers->create([...]);

$airwallex->billing->subscriptions->create([...]);
$airwallex->billing->invoices->finalize('inv_...');
$airwallex->checkout->sessions->create([...]);
$airwallex->fx->quotes->create([...]);
$airwallex->issuing->cards->activate('card_...');
$airwallex->spend->bills->create([...]);
$airwallex->batchTransfers->submit('batch_...');
$airwallex->simulation->payments->createDispute([...]); // sandbox only

$customer = $airwallex->billing->customers->create([
    'email' => '[email protected]',
    'name'  => 'Jane Doe',
]);

$product = $airwallex->billing->products->create(['name' => 'Pro plan']);

$price = $airwallex->billing->prices->create([
    'product_id'    => $product->id,
    'currency'      => 'USD',
    'unit_amount'   => 29,
    'pricing_model' => 'PER_UNIT',
    'recurring'     => ['period' => 1, 'period_unit' => 'MONTH'],
]);

$checkout = $airwallex->billing->checkouts->create([
    'billing_customer_id' => $customer->id,
    'mode'                => 'SUBSCRIPTION',
    'line_items'          => [['price_id' => $price->id, 'quantity' => 1]],
    'success_url'         => 'https://example.com/thanks',
]);

$page = $airwallex->paymentIntents->all(['status' => 'SUCCEEDED', 'page_size' => 100]);

foreach ($page as $intent) {
    // current page only
}

foreach ($page->autoPagingIterator() as $intent) {
    // every page
}

$next = $page->nextPage();        // null on the last page
$prev = $page->previousPage();

$airwallex->balances->current([], [
    'on_behalf_of' => 'acct_...',     // x-on-behalf-of, for connected accounts
    'api_version'  => '2025-02-14',   // override x-api-version for this call
    'headers'      => ['x-custom' => 'value'],
    'timeout'      => 30,
]);

$airwallex->transfers->create([
    'request_id' => 'payout-' . $payout->id,
    // ...
]);

use WPZinc\Airwallex\Exception\ApiErrorException;
use WPZinc\Airwallex\Exception\InvalidRequestException;

try {
    $airwallex->customers->create(['first_name' => 'a']);
} catch (InvalidRequestException $e) {
    $e->getMessage();       // "first_name is too short"
    $e->getAirwallexCode(); // "invalid_argument"
    $e->getSource();        // "first_name"
    $e->getDetails();
    $e->getErrors();        // details.errors, e.g. [['code' => '001', 'source' => 'entity_type']]
    $e->getHttpStatus();    // 400
} catch (ApiErrorException $e) {
    // anything else from the API
}

use WPZinc\Airwallex\Exception\SignatureVerificationException;
use WPZinc\Airwallex\Webhook;

$payload = file_get_contents('php://input');

try {
    $event = Webhook::constructEvent(
        $payload,
        $_SERVER['HTTP_X_SIGNATURE'] ?? '',
        $_SERVER['HTTP_X_TIMESTAMP'] ?? '',
        getenv('AIRWALLEX_WEBHOOK_SECRET')
    );
} catch (SignatureVerificationException $e) {
    http_response_code(400);
    exit;
}

match ($event->type()) {
    'payment_intent.succeeded' => handlePayment($event->object()),
    'subscription.created'     => handleSubscription($event->object()),
    default                    => null,
};

http_response_code(200);

$file = $airwallex->files->upload('/path/to/evidence.pdf', ['notes' => 'Delivery receipt']);
$links = $airwallex->files->downloadLinks(['file_ids' => [$file->file_id]]);

use WPZinc\Airwallex\Auth\AccessToken;
use WPZinc\Airwallex\Auth\TokenStoreInterface;

final class RedisTokenStore implements TokenStoreInterface
{
    public function __construct(private \Redis $redis) {}

    public function get(string $key): ?AccessToken
    {
        $data = $this->redis->get($key);
        return $data ? AccessToken::fromArray(json_decode($data, true)) : null;
    }

    public function put(string $key, AccessToken $token): void
    {
        $this->redis->setex($key, max(1, $token->expiresAt - time()), json_encode($token->toArray()));
    }

    public function forget(string $key): void
    {
        $this->redis->del($key);
    }
}

$airwallex = new AirwallexClient([..., 'token_store' => new RedisTokenStore($redis)]);

$result = $airwallex->rawRequest('post', '/api/v1/some/new_endpoint/create', ['foo' => 'bar'], [], requestId: true);
bash
composer 
bash
vendor/bin/phpunit --testsuite sandbox
php tests/Sandbox/sweep.php   # calls every parameterless GET endpoint and prints the result