PHP code example of kuti-pe / kuti-php

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

    

kuti-pe / kuti-php example snippets


use Kuti\KutiClient;
use Kuti\Money;
use Kuti\PaymentMethodType;
use Kuti\CheckoutSessionCustomer;

$kuti = new KutiClient($_ENV['KUTI_SECRET_KEY']);

$session = $kuti->checkoutSessions->create(
    amount: new Money('249.90', 'PEN'),
    paymentMethodTypes: [PaymentMethodType::InteroperableQr],
    customer: new CheckoutSessionCustomer(id: 'cus_01ABC'),
    // customer: new CheckoutSessionCustomer(firstName: 'María', lastName: 'López', email: '[email protected]'),
    description: 'Zapatillas running talla 42',
    idempotencyKey: "order-{$orderId}",
);

// window.Kuti.open({ checkoutUrl: $session->checkoutUrl, onSuccess, onFailure })
echo json_encode(['checkoutUrl' => $session->checkoutUrl]);

use Kuti\CustomerInput;
use Kuti\PaymentIntentCustomer;

$customer = $kuti->customers->create(new CustomerInput(
    type: 'INDIVIDUAL',
    firstName: 'María',
    lastName: 'López',
    document: ['type' => 'DNI', 'number' => '45678912'], // type opcional: se deduce del número
    email: '[email protected]',
    customFields: ['grade' => 'quinto', 'student_code' => '2026-00781'],
));

// En un cobro: se reutiliza el cliente por id → externalId → documento, o se crea.
$kuti->paymentIntents->create(
    amount: new Money('250.00', 'PEN'),
    paymentMethodTypes: [PaymentMethodType::InteroperableQr],
    customer: new PaymentIntentCustomer(document: ['number' => '45678912'], customFields: ['grade' => 'sexto']),
    description: 'Pensión marzo',
    idempotencyKey: 'pension-2026-03-45678912',
);

// Editar: solo cambian las keys enviadas; null borra el valor.
$kuti->customers->update($customer->id, ['customFields' => ['birth_date' => null]]);

$intent = $kuti->paymentIntents->retrieve($paymentIntentId);
if ($intent->isPaid()) {
    // fulfill order
}

use Kuti\Webhooks;
use Kuti\Exception\KutiSignatureVerificationException;

$payload = file_get_contents('php://input'); // el string CRUDO, sin json_decode antes de verificar

try {
    Webhooks::verifySignature(
        $payload,
        $_SERVER['HTTP_X_KUTI_SIGNATURE'],
        $_SERVER['HTTP_X_KUTI_TIMESTAMP'],
        $_ENV['KUTI_WEBHOOK_SECRET'],
    );
} catch (KutiSignatureVerificationException $e) {
    http_response_code(400);
    exit('Invalid signature');
}

$event = json_decode($payload, true);
// procesa $event['type'] (payment.succeeded, checkout.session.completed, ...)
http_response_code(200);

use Kuti\Exception\KutiValidationException;
use Kuti\Exception\KutiNotFoundException;
use Kuti\Exception\KutiApiException;

try {
    $kuti->checkoutSessions->create(...);
} catch (KutiValidationException $e) {
    // $e->getDetails() => [['field' => 'amount.amount', 'code' => 'MUST_BE_POSITIVE', ...]]
} catch (KutiNotFoundException $e) {
    // ...
} catch (KutiApiException $e) {
    error_log("{$e->getKutiCode()} request_id={$e->getRequestId()}"); // para reportar a soporte
}

$kuti = new KutiClient($secretKey, httpClient: $miClienteGuzzlePersonalizado);

$link = $kuti->paymentLinks->create([
    'title' => 'Taller de Excel — sábado 10am',
    'template' => 'COURSE',
    'pricing' => 'FIXED',
    'amount' => '120.00',
    'paymentMethodTypes' => ['INTEROPERABLE_QR', 'BANK_TRANSFER'],
    'customerFieldIds' => ['cfd_…'],     // [] = solo nombre, apellido y correo
    'buttonLabel' => 'Inscribirme',
    'successMessage' => '¡Listo! Te esperamos el sábado.',
    'successButtonLabel' => 'Unirme al grupo',
    'successButtonUrl' => 'https://chat.whatsapp.com/…',
]);
echo $link->url; // https://pay.kuti.pe/l/taller-de-excel

// Quienes pagaron el link
$paid = $kuti->paymentIntents->list(['paymentLinkId' => $link->id, 'status' => 'SUCCEEDED', 'perPage' => 'all']);

$kuti->paymentIntents->create(
    new Money('250.00', 'PEN'),
    [PaymentMethodType::InteroperableQr],
    customer: new CustomerInput(id: 'cus_…'),
    sendVia: ['EMAIL', 'WHATSAPP'], // null = ['EMAIL']; [] = no enviar
);
bash
composer