PHP code example of farzai / promptpay

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

    

farzai / promptpay example snippets


use Farzai\PromptPay\PromptPay;

// Generate QR code (backward compatible)
$qrCode = PromptPay::create('0899999999', 100);
echo $qrCode; // Raw payload string

use Farzai\PromptPay\PromptPay;

// Immutable builder pattern
$result = PromptPay::generate('0899999999')
    ->withAmount(100.50)
    ->toDataUri('png');

echo '<img src="' . $result->getData() . '" />';

// Customer scans and enters amount themselves
$qrCode = PromptPay::generate('0899999999')->build();

// Amount is pre-filled in payment app
$result = PromptPay::qrCode('0899999999', 150.75)
    ->toDataUri('png');

// Phone Number (10 digits)
PromptPay::generate('0899999999');

// Tax ID / Citizen ID (13 digits)
PromptPay::generate('1234567890123');

// E-Wallet ID (15 digits)
PromptPay::generate('123456789012345');

// Special characters are automatically removed
PromptPay::generate('089-999-9999'); // Works!

$result = PromptPay::generate('0899999999')
    ->withAmount(100)
    ->toDataUri('png');

echo '<img src="' . $result->getData() . '" />';

// Available formats: png, svg, pdf, gif

$result = PromptPay::qrCode('0899999999', 100)
    ->toFile('qrcode.png');

echo 'Saved to: ' . $result->getPath();
echo 'File size: ' . $result->getSize() . ' bytes';

use Nyholm\Psr7\Factory\Psr17Factory;

// Create PSR-17 factory (implements both ResponseFactory and StreamFactory)
$factory = new Psr17Factory();

$response = PromptPay::generate('0899999999')
    ->withAmount(100)
    ->toResponse($factory, $factory);

// Returns PSR-7 ResponseInterface
// Perfect for Laravel, Symfony, Slim, etc.
return $response;

use GuzzleHttp\Psr7\HttpFactory;

$factory = new HttpFactory();
$response = PromptPay::generate('0899999999')
    ->withAmount(100)
    ->toResponse($factory, $factory);

use Symfony\Component\Console\Output\ConsoleOutput;

$output = new ConsoleOutput();
PromptPay::generate('0899999999')
    ->withAmount(100)
    ->toConsole($output);

$payload = PromptPay::generate('0899999999')
    ->withAmount(100)
    ->toPayload();

echo $payload;
// 00020101021229370016A000000677010111011300668999999995802TH53037645406100.006304CB89

use Farzai\PromptPay\PromptPay;
use Farzai\PromptPay\ValueObjects\QrCodeConfig;

// Custom QR code size and margin
$config = QrCodeConfig::create(
    size: 400,      // 400x400 pixels
    margin: 20,     // 20px margin
    encoding: 'UTF-8'
);

$result = PromptPay::generate('0899999999')
    ->withAmount(100)
    ->withConfig($config)
    ->toDataUri('svg');

$builder1 = PromptPay::generate('0899999999')->withAmount(100);
$builder2 = $builder1->withAmount(200); // New instance!

echo $builder1->getAmount(); // 100
echo $builder2->getAmount(); // 200

use Farzai\PromptPay\Exceptions\InvalidRecipientException;

try {
    PromptPay::generate('12345')->build(); // Too short
} catch (InvalidRecipientException $e) {
    echo $e->getMessage();
    // "Invalid recipient length: 5 digits. Expected formats:
    // Too short! • Phone Number: 10 digits (e.g., 0899999999)
    // • Tax ID: 13 digits (e.g., 1234567890123)
    // • E-Wallet ID: 15 digits (e.g., 123456789012345)"

    echo $e->getCode(); // 1003
}

use Farzai\PromptPay\Exceptions\InvalidAmountException;

try {
    PromptPay::qrCode('0899999999', -50)->build();
} catch (InvalidAmountException $e) {
    echo $e->getMessage();
    // "Invalid amount: -50.00 THB cannot be negative.
    // Please provide a positive amount."

    echo $e->getCode(); // 2002
}