PHP code example of igedeon / laravel-wompi

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

    

igedeon / laravel-wompi example snippets


use IGedeon\WompiLaravel\Facades\Wompi;
use IGedeon\WompiLaravel\DTOs\PaymentLinkData;

$link = Wompi::paymentLinks()->create(new PaymentLinkData(
    name: 'Orden #123',
    description: 'Compra de productos',
    singleUse: true,
    collectShipping: false,
    amountInCents: 5000000, // $50,000 COP
));

// URL para compartir con el cliente
$url = $link->checkoutUrl();
// https://checkout.wompi.co/l/abc-123-def

// ID del link
$id = $link->id;

$link = Wompi::paymentLinks()->create(new PaymentLinkData(
    name: 'Orden #456',
    description: 'Suscripción mensual',
    singleUse: false,
    collectShipping: true,
    amountInCents: 15000000,
    currency: 'COP',
    expiresAt: '2026-12-31T23:59:59.000Z',
    redirectUrl: 'https://mitienda.com/gracias',
    imageUrl: 'https://mitienda.com/logo.png',
    sku: 'PROD-456',
    taxes: [
        ['type' => 'VAT', 'amount_in_cents' => 2380952],
    ],
));

$link = Wompi::paymentLinks()->find('abc-123-def');

echo $link->name;
echo $link->amountInCents;
echo $link->active; // true o false

use IGedeon\WompiLaravel\Enums\TransactionStatus;

$transaction = Wompi::transactions()->find('12345-txn-id');

echo $transaction->id;
echo $transaction->status;          // TransactionStatus enum
echo $transaction->amountInCents;
echo $transaction->reference;
echo $transaction->paymentMethodType;

if ($transaction->status === TransactionStatus::Approved) {
    // Pago exitoso
}

// Verificar si el estado es final
if ($transaction->status->isFinal()) {
    // No cambiará más
}

$merchant = Wompi::merchants()->get();

echo $merchant->name;
echo $merchant->legalName;
echo $merchant->acceptanceToken;           // Token de aceptación de términos
echo $merchant->acceptancePersonalAuth;    // Token de autorización de datos personales

$hash = Wompi::integrityHash(
    reference: 'orden-123',
    amountInCents: 5000000,
    currency: 'COP',
);

// Con tiempo de expiración
$hash = Wompi::integrityHash(
    reference: 'orden-123',
    amountInCents: 5000000,
    currency: 'COP',
    expirationTime: '2026-12-31T23:59:59.000Z',
);

// app/Providers/EventServiceProvider.php o usando el atributo #[Listener]

use IGedeon\WompiLaravel\Events\TransactionApproved;
use IGedeon\WompiLaravel\Events\TransactionDeclined;
use IGedeon\WompiLaravel\Events\TransactionVoided;
use IGedeon\WompiLaravel\Events\TransactionError;
use IGedeon\WompiLaravel\Events\WompiWebhookReceived;

// Ejemplo de listener
class ConfirmOrderListener
{
    public function handle(TransactionApproved $event): void
    {
        $transaction = $event->transaction;

        // $transaction->id
        // $transaction->reference
        // $transaction->amountInCents
        // $transaction->status (TransactionStatus::Approved)
        // $transaction->raw (array completo de la respuesta)
    }
}

// config/wompi.php
'webhook' => [
    'path'       => 'wompi/webhook',
    'middleware'  => ['throttle:60,1'],
],

use IGedeon\WompiLaravel\Exceptions\ApiException;
use IGedeon\WompiLaravel\Exceptions\InvalidConfigurationException;
use IGedeon\WompiLaravel\Exceptions\InvalidSignatureException;

try {
    $link = Wompi::paymentLinks()->create($data);
} catch (ApiException $e) {
    $e->getMessage();      // Mensaje de error
    $e->statusCode;        // Código HTTP (401, 422, 500, etc.)
    $e->responseBody;      // Array con la respuesta completa de Wompi
} catch (InvalidConfigurationException $e) {
    // Falta una llave en la configuración
}

use Illuminate\Support\Facades\Http;

Http::fake([
    '*/payment_links' => Http::response([
        'data' => [
            'id' => 'test-link-id',
            'name' => 'Test',
            'description' => 'Test',
            'single_use' => true,
            'collect_shipping' => false,
            'amount_in_cents' => 5000000,
            'currency' => 'COP',
            'active' => true,
        ],
    ]),
]);
bash
php artisan vendor:publish --tag=wompi-config
bash
php artisan vendor:publish --tag=wompi-views