<?php
class Stablix {
private $apiKey;
private $baseUrl = 'https://api.stablix.xyz/api/v1';
public function __construct($apiKey) {
$this->apiKey = $apiKey;
}
private function request($method, $endpoint, $data = null) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->baseUrl . $endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'X-API-Key: ' . $this->apiKey,
'Content-Type: application/json'
]
]);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if (!$result['success']) {
throw new Exception($result['message']);
}
return $result['data'];
}
// Plans
public function createPlan($data) {
return $this->request('POST', '/plans', $data);
}
// Subscriptions
public function createSubscription($data) {
return $this->request('POST', '/subscriptions', $data);
}
// Invoices
public function createInvoice($data) {
return $this->request('POST', '/invoices', $data);
}
// Verify webhook
public function verifyWebhook($payload, $signature, $timestamp) {
$signedPayload = $timestamp . '.' . $payload;
$expected = hash_hmac('sha256', $signedPayload, $_ENV['STABLIX_WEBHOOK_SECRET']);
return hash_equals($expected, $signature);
}
}
// Usage
$stablix = new Stablix($_ENV['STABLIX_SECRET_KEY']);
// Create a plan
$plan = $stablix->createPlan([
'name' => 'Pro Monthly',
'amount' => 29.99,
'interval' => 'monthly',
'trial_days' => 14
]);
echo "Payment URL: " . $plan['payment_url'];
# Terminal 1: Start your server
npm run dev
# Terminal 2: Start ngrok
ngrok http 3000
# Copy the ngrok URL
# Set it as your webhook URL in Stablix dashboard