PHP code example of applax-dev / gate-sdk
1. Go to this page and download the library: Download applax-dev/gate-sdk 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/ */
applax-dev / gate-sdk example snippets
use ApplaxDev\GateSDK\GateSDK;
// Initialize with API key
$sdk = new GateSDK(
apiKey: 'your-bearer-token-here',
sandbox: true // Use sandbox for testing
);
use ApplaxDev\GateSDK\GateSDK;
use ApplaxDev\GateSDK\Config\GateConfig;
$config = GateConfig::fromEnvironment();
$sdk = GateSDK::fromConfig($config);
// Create an order
$orderData = [
'client' => [
'email' => '[email protected] ',
'phone' => '371-12345678',
'first_name' => 'John',
'last_name' => 'Doe',
],
'products' => [
[
'title' => 'Premium Subscription',
'price' => 29.99,
'quantity' => 1,
]
],
'currency' => 'EUR',
'language' => 'en',
];
use ApplaxDev\GateSDK\Exceptions\GateException;
try {
$order = $sdk->createOrderModel($orderData);
echo "Order created: " . $order->getNumber() . "\n";
echo "Total: " . $order->getFormattedAmount() . "\n";
echo "Payment URL: " . $order->getPaymentUrl() . "\n";
} catch (GateException $e) {
echo "Error: " . $e->getMessage() . "\n";
}
// Create a brand
$brand = $sdk->rawPost('/brands/', [
'name' => 'My Brand',
'description' => 'Brand description'
]);
// Create a subscription
$subscription = $sdk->rawPost('/subscriptions/', [
'client' => ['email' => '[email protected] '],
'amount' => 29.99,
'currency' => 'EUR',
'interval' => 'monthly'
]);
// Manage taxes
$tax = $sdk->rawPost('/taxes/', [
'name' => 'VAT',
'rate' => 21.0,
'country' => 'LV'
]);
// Create charges
$charge = $sdk->rawPost('/charges/', [
'amount' => 100.00,
'currency' => 'EUR',
'description' => 'Service charge'
]);
// Execute card payment
$cardData = [
'cardholder_name' => 'John Doe',
'card_number' => '4111111111111111', // Test card
'cvv' => '123',
'exp_month' => 12,
'exp_year' => 25,
];
$paymentResult = $sdk->executeCardPayment($order->getApiDoUrl(), $cardData);
if ($paymentResult['status'] === 'success') {
echo "Payment successful! Transaction ID: " . $paymentResult['transaction_id'] . "\n";
}
// Use raw methods for full API access
$brand = $sdk->rawPost('/brands/', ['name' => 'My Brand']);
$subscription = $sdk->rawGet('/subscriptions/', ['status' => 'active']);
$tax = $sdk->rawPatch('/taxes/{id}/', ['rate' => 21.0]);
$charge = $sdk->rawDelete('/charges/{id}/');
// Or use the universal raw() method
$result = $sdk->raw('POST', '/any-endpoint/', $payload);
// Create order
$order = $sdk->createOrder($orderData);
// Get order with rich model
$order = $sdk->getOrderModel($orderId);
// Payment operations
$sdk->capturePayment($orderId, ['amount' => 50.00]);
$sdk->refundPayment($orderId, ['amount' => 25.00, 'reason' => 'Customer request']);
$sdk->cancelOrder($orderId);
$result = $sdk->executeCardPayment($order->getApiDoUrl(), $cardData);
// Apple Pay
$result = $sdk->executeApplePayPayment($order->getApplePayUrl(), $applePayData);
// Google Pay
$result = $sdk->executeGooglePayPayment($order->getGooglePayUrl(), $googlePayData);
// PayPal
$result = $sdk->initPayPalPayment($order->getPayPalInitUrl());
// Klarna
$result = $sdk->initKlarnaPayment($order->getKlarnaInitUrl(), $klarnaData);
// Product management
$product = $sdk->createProduct($productData);
$products = $sdk->getProducts(['filter_title' => 'Premium']);
// Client management
$client = $sdk->createClient($clientData);
$clientCards = $sdk->getClientCards($clientId);
// Brands
$brand = $sdk->rawPost('/brands/', ['name' => 'My Brand']);
$brands = $sdk->rawGet('/brands/', ['limit' => 20]);
$brand = $sdk->rawPatch('/brands/{id}/', ['name' => 'Updated']);
$sdk->rawDelete('/brands/{id}/');
// Subscriptions
$subscription = $sdk->rawPost('/subscriptions/', $subscriptionData);
$subscriptions = $sdk->rawGet('/subscriptions/', ['status' => 'active']);
$sdk->rawPost('/subscriptions/{id}/cancel/', []);
// Taxes
$tax = $sdk->rawPost('/taxes/', ['name' => 'VAT', 'rate' => 21.0]);
$taxes = $sdk->rawGet('/taxes/', ['country' => 'LV']);
// Charges
$charge = $sdk->rawPost('/charges/', $chargeData);
$sdk->rawPost('/charges/{id}/capture/', []);
$sdk->rawPost('/charges/{id}/refund/', ['amount' => 50.00]);
use ApplaxDev\GateSDK\Models\Order;
$order = $sdk->getOrderModel($orderId);
// Rich model methods
echo $order->getNumber();
echo $order->getFormattedAmount();
echo $order->getClient()->getDisplayName();
// Status checks
if ($order->isPayable()) {
echo "Order can be paid";
}
if ($order->isPaid()) {
echo "Order is fully paid";
}
// Get available payment methods
$methods = $order->getAvailablePaymentMethods();
// ['card', 'apple_pay', 'paypal', 'klarna']
use ApplaxDev\GateSDK\Exceptions\{
GateException,
ValidationException,
AuthenticationException,
NotFoundException,
RateLimitException,
ServerException,
NetworkException
};
try {
$order = $sdk->createOrder($orderData);
} catch (ValidationException $e) {
// Handle validation errors (400)
echo "Validation error: " . $e->getMessage() . "\n";
// Get field-specific errors
if ($e->hasFieldErrors('email')) {
print_r($e->getFieldErrors('email'));
}
} catch (AuthenticationException $e) {
// Handle authentication errors (401, 403)
echo "Auth error: " . $e->getRecommendedAction() . "\n";
} catch (RateLimitException $e) {
// Handle rate limiting (429)
echo "Rate limited. Wait " . $e->getSuggestedWaitTime() . " seconds\n";
} catch (NetworkException $e) {
// Handle network issues
if ($e->isRetryable()) {
echo "Network error, retrying in " . $e->getRecommendedRetryDelay() . "s\n";
}
} catch (GateException $e) {
// Handle any other API errors
echo "API error: " . $e->getMessage() . "\n";
print_r($e->getErrorDetails());
}
// Create webhook
$webhook = $sdk->createWebhook([
'url' => 'https://yourdomain.com/webhooks/applax',
'events' => ['order.paid', 'order.failed', 'order.refunded']
]);
$webhookSecret = $webhook['secret']; // Store securely
// In your webhook endpoint
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '';
if (!$sdk->validateWebhookSignature($payload, $signature, $webhookSecret)) {
http_response_code(401);
exit('Invalid signature');
}
$data = json_decode($payload, true);
switch ($data['event']) {
case 'order.paid':
// Handle successful payment
$orderId = $data['object']['id'];
break;
case 'order.failed':
// Handle failed payment
$orderId = $data['object']['id'];
break;
}
http_response_code(200);
use GuzzleHttp\Client;
$customClient = new Client([
'timeout' => 60,
'verify' => '/path/to/cacert.pem'
]);
$sdk = new GateSDK(
apiKey: 'your-api-key',
sandbox: true,
httpClient: $customClient
);
use Monolog\Logger;
use Monolog\Handler\FileHandler;
$logger = new Logger('applax-sdk');
$logger->pushHandler(new FileHandler('applax-sdk.log', Logger::DEBUG));
$sdk = new GateSDK(
apiKey: 'your-api-key',
sandbox: true,
config: ['debug' => true],
logger: $logger
);