PHP Examples
Ready-to-use PHP code for common Pixlpay API operations.
Setup
Using cURL
php
<?php
class PixlpayClient
{
private string $baseUrl;
private string $apiToken;
public function __construct(string $subdomain, string $apiToken)
{
$this->baseUrl = "https://{$subdomain}.pixlpay.net/api/external/v1";
$this->apiToken = $apiToken;
}
public function request(string $method, string $endpoint, array $data = []): array
{
$ch = curl_init();
$url = $this->baseUrl . $endpoint;
$headers = [
'Authorization: Bearer ' . $this->apiToken,
'Accept: application/json',
'Content-Type: application/json',
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return [
'status' => $httpCode,
'data' => json_decode($response, true),
];
}
public function get(string $endpoint): array
{
return $this->request('GET', $endpoint);
}
public function post(string $endpoint, array $data): array
{
return $this->request('POST', $endpoint, $data);
}
}Fetching Products
php
<?php
$client = new PixlpayClient('yourstore', 'your_api_token');
// Get all products
$response = $client->get('/products');
if ($response['status'] === 200) {
foreach ($response['data']['data'] as $product) {
echo "{$product['name']} - \${$product['price']}\n";
}
}
// Get single product
$response = $client->get('/products/1');
$product = $response['data']['data'];Fetching Orders
php
<?php
$client = new PixlpayClient('yourstore', 'your_api_token');
// Get pending orders
$response = $client->get('/orders?status=pending');
foreach ($response['data']['data'] as $order) {
echo "Order #{$order['order_number']} - {$order['customer']['email']}\n";
}
// Get orders from date range
$response = $client->get('/orders?from=2025-01-01&to=2025-01-31');Fulfilling Orders
php
<?php
$client = new PixlpayClient('yourstore', 'your_api_token');
// Mark order as fulfilled
$orderId = 1;
$response = $client->post("/orders/{$orderId}/fulfill", [
'note' => 'Delivered via API',
]);
if ($response['status'] === 200) {
echo "Order fulfilled successfully!\n";
}Webhook Handler
php
<?php
// webhook-handler.php
$secret = getenv('PIXLPAY_WEBHOOK_SECRET');
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_PIXLPAY_SIGNATURE'] ?? '';
// Verify signature
$computed = hash_hmac('sha256', $payload, $secret);
if (!hash_equals($computed, $signature)) {
http_response_code(401);
exit('Invalid signature');
}
// Parse event
$event = json_decode($payload, true);
$eventType = $event['event'];
$data = $event['data'];
// Handle different events
switch ($eventType) {
case 'order.paid':
handleOrderPaid($data);
break;
case 'order.completed':
handleOrderCompleted($data);
break;
case 'subscription.cancelled':
handleSubscriptionCancelled($data);
break;
}
http_response_code(200);
function handleOrderPaid(array $order): void
{
// Your delivery logic
error_log("Order paid: {$order['order_number']}");
}
function handleOrderCompleted(array $order): void
{
error_log("Order completed: {$order['order_number']}");
}
function handleSubscriptionCancelled(array $subscription): void
{
error_log("Subscription cancelled: {$subscription['id']}");
}Creating Webhooks
php
<?php
$client = new PixlpayClient('yourstore', 'your_api_token');
$response = $client->post('/webhooks', [
'url' => 'https://yoursite.com/webhooks/pixlpay',
'events' => ['order.paid', 'order.completed'],
]);
if ($response['status'] === 201) {
$webhook = $response['data']['data'];
echo "Webhook created! Secret: {$webhook['secret']}\n";
}Error Handling
php
<?php
$client = new PixlpayClient('yourstore', 'your_api_token');
$response = $client->get('/products/99999');
if ($response['status'] !== 200) {
$error = $response['data'];
echo "Error: {$error['message']}\n";
switch ($response['status']) {
case 401:
echo "Check your API token\n";
break;
case 404:
echo "Resource not found\n";
break;
case 429:
echo "Rate limited - slow down!\n";
break;
}
}