PHP code example of payscribe / php-sdk

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

    

payscribe / php-sdk example snippets



Payscribe\Payscribe;

$payscribe = Payscribe::sandbox('sk_test_your_key');

// 1. Create a customer first (returns array)
$customer = $payscribe->customers->create(
    firstName: 'John',
    lastName: 'Doe',
    email: '[email protected]',
    phone: '+2348012345678',
);
$customerId = $customer['customer_id'] ?? '';

// 2. Create a static virtual account
$account = $payscribe->virtualAccounts->createStatic($customerId, ['9PSB']);
echo $account->accountNumber;        // 5031240100

// 3. Create a virtual card
$card = $payscribe->virtualCards->create(
    customerId: $customerId,
    currency: 'USD',
    brand: 'visa',
    amount: 100,
    reference: 'ref_' . uniqid(),
);

echo $card->getMaskedNumber();       // 411111******1111 (safe for display)
echo $card->getNumber();             // 4111111111111111 (full PAN — use carefully)

$payscribe = new Payscribe(
    new \Payscribe\Type\Config(
        secretKey: 'sk_test_xxx',         //                      // optional, default 30s
        maxRetries: 3,                     // optional, default 3 (exponential backoff)
    ),
    $yourPsr18Client,                      // optional, must implement Psr\Http\Client\ClientInterface
);

$customer = $payscribe->customers->create(
    firstName: 'Jane',
    lastName: 'Smith',
    email: '[email protected]',
    phone: '+2348012345678',
);
$customerId = $customer['customer_id'] ?? $customer['id'] ?? '';

// Static account (permanent, reusable)
$account = $payscribe->virtualAccounts->createStatic(
    customerId: 'cus_xxx',
    banks: ['9PSB'],
);
echo $account->accountNumber;

// Dynamic account (checkout with expiry)
$account = $payscribe->virtualAccounts->createDynamic(
    reference: 'order_123',
    amount: 2500,
    amountType: 'EXACT',
    expiresDuration: 30,
    expiresType: 'minutes',
    customerName: 'Ada Lovelace',
    customerEmail: '[email protected]',
    customerPhone: '08099228833',
);

// Input validation throws InvalidArgumentException early:
$card = $payscribe->virtualCards->create(
    customerId: 'cus_xxx',
    currency: 'DOGE', // throws InvalidArgumentException: "Invalid currency 'DOGE'. Supported: USD, NGN, GBP, EUR"
    brand: 'visa',
    amount: 100,
    reference: 'ref_xxx',
);

// Airtime
$result = $payscribe->bills->airtime->vend(
    network: 'mtn',
    amount: 500,
    recipient: '08030000000',
    reference: 'ref_' . uniqid(),
);

// Electricity
$meter = $payscribe->bills->electricity->validate('1234567890', 'prepaid', 2000, 'ikedc');
$vend  = $payscribe->bills->electricity->pay(
    meterNumber: '1234567890',
    meterType: 'prepaid',
    amount: 2000,
    service: 'ikedc',
    customerName: $meter['name'] ?? '',
    reference: 'ref_' . uniqid(),
);

// Data
$plans = $payscribe->bills->data->lookup(network: 'mtn', category: 'data');
$data  = $payscribe->bills->data->vend('data_plan_1', '08030000000', 'mtn', 'ref_' . uniqid());

// Requery a transaction
$status = $payscribe->bills->requery($transactionId);

// Simple verification (no replay protection)
$isValid = $payscribe->webhooks->verifySignature(
    $rawBody,
    $_SERVER['HTTP_X_PAYSCRIBE_SIGNATURE'],
);

// With replay protection (recommended)
$event = $payscribe->webhooks->constructEvent(
    $rawBody,
    $signature,
    timestamp: time(),     // signed into the HMAC
    tolerance: 300,         // reject if older than 5 min
);

## Error handling

All API errors throw typed exceptions. Catch them individually or use the base class:


$config = new \Payscribe\Type\Config(
    secretKey: 'sk_test_xxx',
    timeout: 60,       // request timeout in seconds
    maxRetries: 5,      // max retry attempts
);

  $key = getenv('PAYSCRIBE_SECRET_KEY');
  $payscribe = Payscribe::production($key);
  

$payscribe = Payscribe::createFromEnv();
$account = $payscribe->account();
$payscribe->vendAirtime('mtn', 100, '08030000000');
bash
composer