PHP code example of inpayuz / inpay-php

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

    

inpayuz / inpay-php example snippets


use InPay\InPay;

$inpay = new InPay([
    'merchant_id'    => 12345,          // kassa merchant_id
    'bearer_token'   => 'xxxxxxxx',     // kassa bearer token
    'webhook_secret' => 'yyyyyyyy',     // webhook imzosi uchun
]);

// 1) To'lov yaratish
$invoice = $inpay->create([
    'amount'       => 50000,            // so'm (min 1000)
    'callback_url' => 'https://shop.uz/inpay/webhook.php',
    'order_id'     => 'A-123',          // sizning ichki reference
]);

header('Location: ' . $invoice->payUrl);   // foydalanuvchini yo'naltiring

$invoice = $inpay->create([
    'amount'         => 50000,
    'payment_method' => 'click',   // 'click' | 'payme' | 'myuzcard'
]);
header('Location: ' . $invoice->payUrlLink);

$tx = $inpay->status('a1b2c3d4e5f6a7b8');
if ($tx->isPaid()) {
    // ...
}
// $tx->status: 'pending' | 'success' | 'failed'

use InPay\InPay;
use InPay\Transaction;

$inpay = new InPay([
    'merchant_id'    => 12345,
    'bearer_token'   => 'xxxxxxxx',
    'webhook_secret' => 'yyyyyyyy',
]);

$inpay->webhook()->onSuccess(function (Transaction $tx) {
    // ⚠️ IDEMPOTENT bo'ling — takror kelishi mumkin
    // if (already_paid($tx->orderId)) return;
    // mark_paid($tx->orderId, $tx->amount);
});

$wh   = $inpay->webhook();
$body = file_get_contents('php://input');
$sig  = $_SERVER['HTTP_X_INPAY_SIGNATURE'] ?? '';

if ($wh->isValid($body, $sig)) {
    $tx = $wh->verify($body, $sig);   // Transaction
}

use InPay\Exception\ApiException;      // inPAY biznes-xatosi (error_code bilan)
use InPay\Exception\SignatureException;// webhook imzosi mos emas
use InPay\Exception\InPayException;    // bazaviy tur (hammasini ushlaydi)

try {
    $inpay->create([...]);
} catch (ApiException $e) {
    echo $e->getErrorCode();   // masalan "MERCHANT_INACTIVE", "AMOUNT_TOO_LOW"
}
bash
composer