定制 farzai/promptpay 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

farzai/promptpay

Composer 安装命令:

composer require farzai/promptpay

包简介

PromptPay QR Code Generator

README 文档

README

Latest Version on Packagist Tests codecov Total Downloads

A modern, type-safe PHP library for generating PromptPay QR codes.

Features

  • Zero Config - Works out of the box with sensible defaults
  • Multiple Formats - PNG, SVG, GIF support
  • Amount Support - Static or dynamic QR codes
  • CLI Tool - Command-line interface included

Requirements

  • PHP 8.1 or higher
  • Composer

Installation

For PHP Applications

composer require farzai/promptpay

For CLI Usage (Global)

composer global require farzai/promptpay

Make sure Composer's global bin directory is in your $PATH:

  • macOS/Linux: ~/.composer/vendor/bin or ~/.config/composer/vendor/bin
  • Windows: %USERPROFILE%\AppData\Roaming\Composer\vendor\bin

Quick Start

Simple Example

use Farzai\PromptPay\PromptPay;

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

Modern Builder API (Recommended)

use Farzai\PromptPay\PromptPay;

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

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

Usage Guide

Creating QR Codes

Static QR Code (No Amount)

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

Dynamic QR Code (With Amount)

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

Recipient Types

The library automatically detects recipient type based on length:

// 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!

Output Formats

1. Data URI (for <img> tags)

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

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

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

2. Save to File

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

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

3. PSR-7 HTTP Response

First, install any PSR-17/PSR-7 implementation:

# Choose one:
composer require nyholm/psr7
# or
composer require guzzlehttp/psr7

Then create the response:

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;

With Guzzle PSR-7:

use GuzzleHttp\Psr7\HttpFactory;

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

Why PSR-17? No hard dependencies! Works with ANY PSR-7 library - choose the one your project already uses.

4. Console Output (CLI)

use Symfony\Component\Console\Output\ConsoleOutput;

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

5. Raw Payload String

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

echo $payload;
// 00020101021229370016A000000677010111011300668999999995802TH53037645406100.006304CB89

Advanced Configuration

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');

Immutable Builder Pattern

The builder is fully immutable - each method returns a new instance:

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

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

Validation & Error Handling

The library provides comprehensive validation with helpful error messages:

Recipient Validation

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
}

Amount Validation

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
}

Error Codes Reference

Recipient Errors (1xxx)

  • 1001 - Empty recipient
  • 1002 - Not numeric
  • 1003 - Invalid length
  • 1004 - Empty after normalization

Amount Errors (2xxx)

  • 2001 - Not numeric
  • 2002 - Negative amount
  • 2003 - Too large (> 999,999,999.99)
  • 2004 - Zero (when positive required)
  • 2005 - Too small (< 0.01)

Configuration Errors (3xxx)

  • 3001 - Size too small
  • 3002 - Size too large
  • 3003 - Margin too small
  • 3004 - Margin too large
  • 3005 - Invalid encoding
  • 3006 - Invalid path
  • 3007 - Missing dependency

CLI Usage

# Basic usage
promptpay 0899999999 100

# Interactive mode (no arguments)
promptpay

# Output shows QR code in terminal

Testing

# Run tests
composer test

# Run tests with coverage
composer test-coverage

# Run static analysis
composer analyse

# Run code formatting
composer format

Examples

Check the examples/ directory for real-world usage scenarios:

  • 01-basic-usage.php - Basic QR code generation and builder patterns
  • 02-file-generation.php - Saving to files with custom configurations
  • 03-error-handling.php - Comprehensive error handling patterns
  • 04-web-integration.php - Web form integration with HTML
  • 05-laravel-integration.php - Laravel framework integration
  • 06-symfony-integration.php - Symfony framework integration
  • 07-custom-validation.php - Custom validation patterns and business rules
  • 08-batch-generation.php - Batch processing and bulk generation

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

Security

If you discover any security vulnerabilities, please review our security policy on how to report them.

Changelog

Please see CHANGELOG.md for recent changes.

License

The MIT License (MIT). Please see LICENSE.md for more information.

Credits

Acknowledgments

  • Built with endroid/qr-code
  • Follows PromptPay EMV QR Code Specification
  • Inspired by Thailand's National e-Payment Master Plan

Made with ❤️ for the Thai developer community

farzai/promptpay 适用场景与选型建议

farzai/promptpay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 290 次下载、GitHub Stars 达 9, 最近一次更新时间为 2023 年 05 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「qr」 「qr-code」 「Promptpay」 「farzai」 「promptpay-qr」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 farzai/promptpay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 farzai/promptpay 我们能提供哪些服务?
定制开发 / 二次开发

基于 farzai/promptpay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 290
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-25