PHP SDK

Official RevKeen SDK for PHP 8.1+

The RevKeen PHP SDK provides a clean interface to the RevKeen API. It works with Laravel, WordPress, Symfony, and any PHP 8.1+ application.

Installation

composer require revkeen/sdk-php

Requirements

  • PHP 8.1 or later
  • Composer
  • ext-json extension
  • Guzzle HTTP client (auto-installed)

Quick Start

<?php

require_once 'vendor/autoload.php';

use RevKeen\Client;

$client = new Client(getenv('REVKEEN_API_KEY'));

// Create a customer
$customer = $client->customers->create([
    'email' => 'john@example.com',
    'name' => 'John Doe',
]);

echo $customer->data->id;

Configuration

<?php

use RevKeen\Client;
use RevKeen\ClientOptions;

$client = new Client(
    apiKey: 'rk_live_xxxxxxxx',
    options: new ClientOptions(
        baseUrl: 'https://api.revkeen.com',  // API base URL
        timeout: 30,                          // Request timeout in seconds
        maxRetries: 3,                        // Retry attempts for failures
    )
);

Environment Variables

You can also configure via environment variables:

REVKEEN_API_KEY=rk_live_xxxxxxxx

Resources

Customers

<?php

// List customers
$customers = $client->customers->list([
    'limit' => 20,
    'page' => 1,
]);

// Create a customer
$customer = $client->customers->create([
    'email' => 'john@example.com',
    'name' => 'John Doe',
    'company_name' => 'Acme Inc',
    'metadata' => [
        'salesforce_id' => '00Q123',
    ],
]);

// Retrieve a customer
$customer = $client->customers->retrieve('cus_xxxxxxxx');

// Update a customer
$customer = $client->customers->update('cus_xxxxxxxx', [
    'name' => 'John Smith',
]);

// Delete a customer
$client->customers->delete('cus_xxxxxxxx');

Products

<?php

// List products
$products = $client->products->list([
    'kind' => 'subscription',
    'is_active' => true,
]);

// Create a product
$product = $client->products->create([
    'product_id' => 'pro_monthly',
    'name' => 'Pro Plan',
    'kind' => 'subscription',
    'pricing_model' => 'recurring',
    'amount_minor' => 2900,
    'currency' => 'USD',
    'interval' => 'month',
    'interval_count' => 1,
    'trial_days' => 14,
]);

// Retrieve a product
$product = $client->products->retrieve('prod_xxxxxxxx');

// Update a product
$product = $client->products->update('prod_xxxxxxxx', [
    'name' => 'Pro Plan (Updated)',
]);

Subscriptions

<?php

// List subscriptions
$subscriptions = $client->subscriptions->list([
    'customer_id' => 'cus_xxxxxxxx',
    'status' => 'active',
]);

// Create a subscription
$subscription = $client->subscriptions->create([
    'customer_id' => 'cus_xxxxxxxx',
    'product_id' => 'prod_xxxxxxxx',
    'trial_days' => 14,
]);

// Retrieve a subscription
$subscription = $client->subscriptions->retrieve('sub_xxxxxxxx');

// Update a subscription (change plan)
$subscription = $client->subscriptions->update('sub_xxxxxxxx', [
    'product_id' => 'prod_yyyyyyyy',
    'proration' => 'create_prorations',
]);

// Cancel a subscription
$client->subscriptions->cancel('sub_xxxxxxxx', [
    'cancel_at_period_end' => true,
]);

// Pause a subscription
$client->subscriptions->pause('sub_xxxxxxxx', [
    'resume_at' => '2025-03-01T00:00:00Z',
]);

// Resume a subscription
$client->subscriptions->resume('sub_xxxxxxxx');

Invoices

<?php

// List invoices
$invoices = $client->invoices->list([
    'customer_id' => 'cus_xxxxxxxx',
    'status' => 'sent',
]);

// Create an invoice
$invoice = $client->invoices->create([
    'customer_id' => 'cus_xxxxxxxx',
    'currency' => 'USD',
    'due_date' => '2025-02-15T00:00:00Z',
    'line_items' => [
        [
            'description' => 'Consulting Services',
            'quantity' => 10,
            'unit_amount_minor' => 15000,
        ],
    ],
]);

// Submit for approval
$client->invoices->submit('inv_xxxxxxxx');

// Approve an invoice
$client->invoices->approve('inv_xxxxxxxx');

// Send an invoice
$client->invoices->send('inv_xxxxxxxx');

// Void an invoice
$client->invoices->void('inv_xxxxxxxx', [
    'reason' => 'Duplicate invoice',
]);

// Download PDF
$pdfContent = $client->invoices->getPdf('inv_xxxxxxxx');

Checkout Sessions

<?php

// Create a checkout session
$session = $client->checkoutSessions->create([
    'mode' => 'subscription',
    'product_id' => 'prod_xxxxxxxx',
    'customer_id' => 'cus_xxxxxxxx',
    'success_url' => 'https://yourapp.com/success',
    'cancel_url' => 'https://yourapp.com/cancel',
]);

// Redirect to checkout
header('Location: ' . $session->data->url);
exit;

// Retrieve a session
$session = $client->checkoutSessions->retrieve('chk_xxxxxxxx');

// Expire a session
$client->checkoutSessions->expire('chk_xxxxxxxx');

Pagination

Manual Pagination

<?php

$page1 = $client->customers->list(['limit' => 20, 'page' => 1]);
$page2 = $client->customers->list(['limit' => 20, 'page' => 2]);

echo 'Total: ' . $page1->pagination->total;
echo 'Pages: ' . $page1->pagination->totalPages;

Auto-Pagination

<?php

// Iterate through all results
foreach ($client->customers->all() as $customer) {
    echo $customer->id . ' ' . $customer->email . PHP_EOL;
}

// Get all results as array
$allCustomers = iterator_to_array($client->customers->all());

Error Handling

<?php

use RevKeen\Client;
use RevKeen\Exceptions\RevKeenException;
use RevKeen\Exceptions\NotFoundException;
use RevKeen\Exceptions\ValidationException;
use RevKeen\Exceptions\UnauthorizedException;
use RevKeen\Exceptions\ForbiddenException;
use RevKeen\Exceptions\RateLimitException;
use RevKeen\Exceptions\InternalServerException;

try {
    $customer = $client->customers->retrieve('cus_invalid');
} catch (NotFoundException $e) {
    // 404 - Resource not found
    echo 'Customer not found';
} catch (ValidationException $e) {
    // 400/422 - Validation failed
    echo 'Validation error: ' . $e->getMessage();
    print_r($e->getDetails());
} catch (UnauthorizedException $e) {
    // 401 - Invalid API key
    echo 'Invalid API key';
} catch (ForbiddenException $e) {
    // 403 - Insufficient permissions
    echo 'Permission denied';
} catch (RateLimitException $e) {
    // 429 - Rate limited
    echo 'Rate limited, retry after: ' . $e->getRetryAfter() . ' seconds';
} catch (InternalServerException $e) {
    // 5xx - Server error
    echo 'Server error, please retry';
} catch (RevKeenException $e) {
    // Generic API error
    echo 'API error: ' . $e->getCode() . ' ' . $e->getMessage();
}

Webhook Verification

<?php

use RevKeen\Webhook;

// Get raw payload and signature
$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_REVKEEN_SIGNATURE'] ?? '';

try {
    $event = Webhook::verify(
        $payload,
        $signature,
        getenv('WEBHOOK_SECRET')
    );

    switch ($event->type) {
        case 'invoice.paid':
            handleInvoicePaid($event->data);
            break;
        case 'subscription.created':
            handleSubscriptionCreated($event->data);
            break;
        case 'subscription.cancelled':
            handleSubscriptionCancelled($event->data);
            break;
    }

    http_response_code(200);
    echo json_encode(['received' => true]);
} catch (\Exception $e) {
    error_log('Webhook verification failed: ' . $e->getMessage());
    http_response_code(400);
    echo 'Invalid signature';
}

Framework Examples

Laravel

<?php

// config/services.php
return [
    'revkeen' => [
        'api_key' => env('REVKEEN_API_KEY'),
    ],
];

// app/Providers/AppServiceProvider.php
use RevKeen\Client;

public function register(): void
{
    $this->app->singleton(Client::class, function ($app) {
        return new Client(config('services.revkeen.api_key'));
    });
}

// app/Http/Controllers/CheckoutController.php
use RevKeen\Client;

class CheckoutController extends Controller
{
    public function __construct(
        private Client $revkeen
    ) {}

    public function createSession(Request $request)
    {
        $session = $this->revkeen->checkoutSessions->create([
            'mode' => 'subscription',
            'product_id' => $request->product_id,
            'customer_id' => $request->user()->revkeen_customer_id,
            'success_url' => route('dashboard'),
            'cancel_url' => route('pricing'),
        ]);

        return redirect($session->data->url);
    }
}

WordPress

<?php
/*
Plugin Name: My RevKeen Integration
*/

require_once __DIR__ . '/vendor/autoload.php';

use RevKeen\Client;

function get_revkeen_client(): Client {
    static $client = null;

    if ($client === null) {
        $api_key = get_option('revkeen_api_key');
        $client = new Client($api_key);
    }

    return $client;
}

// Example: Create checkout for WooCommerce
add_action('woocommerce_checkout_create_order', function($order) {
    $client = get_revkeen_client();

    $session = $client->checkoutSessions->create([
        'mode' => 'payment',
        'amount_minor' => $order->get_total() * 100,
        'currency' => $order->get_currency(),
        'success_url' => $order->get_checkout_order_received_url(),
        'cancel_url' => wc_get_checkout_url(),
    ]);

    $order->update_meta_data('_revkeen_checkout_url', $session->data->url);
});

Symfony

<?php

// config/services.yaml
services:
    RevKeen\Client:
        arguments:
            - '%env(REVKEEN_API_KEY)%'

// src/Controller/CheckoutController.php
namespace App\Controller;

use RevKeen\Client;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class CheckoutController extends AbstractController
{
    public function __construct(
        private Client $revkeen
    ) {}

    #[Route('/checkout', name: 'checkout')]
    public function index(): Response
    {
        $session = $this->revkeen->checkoutSessions->create([
            'mode' => 'subscription',
            'product_id' => 'prod_xxxxxxxx',
            'success_url' => $this->generateUrl('dashboard', [], true),
            'cancel_url' => $this->generateUrl('pricing', [], true),
        ]);

        return $this->redirect($session->data->url);
    }
}

Related Resources