PHP code example of taypi / taypi-php

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

    

taypi / taypi-php example snippets



pi = new Taypi\Taypi(
    'taypi_pk_test_...',  // Public key
    'taypi_sk_test_...',  // Secret key
);

// Crear sesión de checkout
$session = $taypi->createCheckoutSession([
    'amount'      => '25.00',
    'reference'   => 'ORD-12345',
    'description' => 'Zapatillas Nike Air',
], 'ORD-12345'); // Idempotency-Key

echo $session['checkout_token'];


pi = new Taypi\Taypi('taypi_pk_test_...', 'taypi_sk_test_...');

$reference = 'ORD-12345';
$session = $taypi->createCheckoutSession([
    'amount'      => '25.00',
    'reference'   => $reference,
    'description' => 'Mi producto',
], $reference);

// Crear sesión para checkout.js (retorna solo checkout_token)
$session = $taypi->createCheckoutSession([
    'amount'      => '50.00',
    'reference'   => 'ORD-789',
    'description' => 'Descripción del pago',
    'metadata'    => ['source' => 'web'],
], 'ORD-789');

// Crear pago (retorna datos completos: QR, checkout_url, etc.)
$payment = $taypi->createPayment([
    'amount'      => '50.00',
    'reference'   => 'ORD-789',
    'description' => 'Descripción del pago',
], 'ORD-789');

// Consultar pago
$payment = $taypi->getPayment('uuid-del-pago');

// Listar pagos
$result = $taypi->listPayments([
    'status'   => 'completed',
    'from'     => '2026-03-01',
    'to'       => '2026-03-31',
    'per_page' => 50,
]);

// Cancelar pago pendiente
$payment = $taypi->cancelPayment('uuid-del-pago', 'cancel-ORD-789');

// Datos del comercio autenticado (tier, volumen usado, limite mensual)
$merchant = $taypi->getMerchant();
echo $merchant['business_name'];
echo $merchant['monthly_volume_used'] . ' / ' . $merchant['monthly_volume_limit'];

// Listar tiendas activas del comercio
$stores = $taypi->listStores();
foreach ($stores as $store) {
    echo $store['name'] . ' — ' . $store['merchant_code'];
}

// 1. Backend: crea la sesión y entrega solo el token al frontend
$session = $taypi->createCheckoutSession([
    'amount'    => '50.00',
    'reference' => 'ORD-123',
], 'ORD-123');
$token = $session['checkout_token'];

// 2. Frontend (checkout.js) o backend: lee los datos completos de la sesión
$details = $taypi->getCheckoutSession($token);
echo $details['qr_image'];   // SVG base64
echo $details['merchant_name'];

// Verificar firma de webhook recibido
$payload   = file_get_contents('php://input');
$signature = $_SERVER['HTTP_TAYPI_SIGNATURE'];
$secret    = 'tu_webhook_secret';

if ($taypi->verifyWebhook($payload, $signature, $secret)) {
    // Webhook válido, procesar
    $event = json_decode($payload, true);
} else {
    // Firma inválida, rechazar
    http_response_code(403);
}

// Producción (default)
$taypi = new Taypi\Taypi('pk', 'sk');

// Desarrollo
$taypi = new Taypi\Taypi('pk', 'sk', ['base_url' => 'https://sandbox.taypi.pe']);

// Sandbox
$taypi = new Taypi\Taypi('pk', 'sk', ['base_url' => 'https://sandbox.taypi.pe']);

// Usar la referencia de orden como idempotency key
$taypi->createCheckoutSession($params, 'ORD-12345');

// Si el mismo key se envía dentro de los 15 minutos, retorna la respuesta cacheada
// sin crear un pago nuevo.

try {
    $session = $taypi->createCheckoutSession($params, $reference);
} catch (Taypi\TaypiException $e) {
    echo $e->getMessage();    // "El monto mínimo es S/ 1.00"
    echo $e->errorCode;       // "AMOUNT_TOO_LOW"
    echo $e->httpCode;        // 422
    echo $e->response;        // Respuesta completa del API (array)
}
bash
composer