3neti/voucher
Composer 安装命令:
composer require 3neti/voucher
包简介
Digital voucher module for issuance, redemption, and lifecycle management
README 文档
README
Overview
The Digital Voucher System is designed to manage and redeem vouchers that may include monetary value (cash), specific validation rules, feedback mechanisms, and instructions for issuing or redeeming vouchers. This system includes robust handling for cash entities that are tightly integrated with redeemable vouchers.
This document outlines the Cash entity, its relationship to the voucher system, and its critical features like tagging, status management, redemption rules, and validation.
Core Components
-
Cash Entity (
CashModel):- Represents a monetary value or cash amount tied to a voucher.
- Features secure handling with hashed secrets for redemption.
- Enables tagging, tracking statuses, and metadata storage.
- Supports advanced validation, including expiry and custom rules.
-
Voucher Entity (
VoucherModel):- Represents the actual voucher, which may reference associated
Cash. - Includes instructions for issuing or redeeming vouchers (e.g., feedback or rider instructions).
- Represents the actual voucher, which may reference associated
Features of the Cash Model
1. Attributes
amount: Represents the monetary value in minor units using the Brick\Money library.currency: Default to PHP but configurable on creation.meta: Stores custom metadata (ArrayObject).secret: A hashed value used for secure redemption.expires_on: Specifies when the cash expires.status: Tracks the current status of the cash (e.g., MINTED, EXPIRED).reference: A morph-to relationship to link cash to a voucher or any reference.
2. Status Management
- Uses the Spatie
HasStatusestrait for dynamic status management. - Allows status tracking with reasons for changes.
- Provides methods like:
setStatus(CashStatus $status, string $reason = null)hasStatus(CashStatus $status)hasHadStatus(CashStatus $status)getCurrentStatus()
3. Tagging
- Uses the Spatie
HasTagstrait for adding tags to cash. - Supports:
- Attaching/detaching tags.
- Querying models by tags.
- Setting translated tags.
4. Secure Redemption
- Each cash entity includes a hashed secret for redemption validation:
- Use
setSecretAttributeto hash the secret before saving. - Validate with
verifySecret(string $providedSecret).
- Use
- Combines expiration and secret-checking for redemption with
canRedeem().
5. Validation and Metadata
- Custom validation rules are supported via
CashValidationRulesData(e.g., location, country, and mobile validations). - Metadata extends flexibility, allowing custom annotations when linking to vouchers.
6. Integration with Vouchers
- A
Cashinstance can reference a voucher via polymorphic relationships. - Voucher instructions include:
cashvalidations and amounts.- Feedback and rider instructions.
How It Works
Cash Creation
A Cash instance typically gets created and linked to a voucher:
use LBHurtado\Voucher\Models\Cash; use Brick\Money\Money; $cash = Cash::create([ 'amount' => Money::of(1000, 'PHP'), // 1,000 PHP in major units 'currency' => 'PHP', 'meta' => ['usage' => 'transport allowance'], 'expires_on' => now()->addWeek(), 'secret' => 'secure-secret-key', // Automatically hashed ]);
Redemption Validation
The canRedeem() method ensures that the cash:
- Has not expired.
- Matches the provided secret:
if ($cash->canRedeem($providedSecret)) { // Process redemption logic here } else { // Handle failure (e.g., invalid secret or expired cash) }
Status Tracking
Handle status changes such as MINTED, EXPIRED, or DISBURSED:
use LBHurtado\Voucher\Enums\CashStatus; $cash->setStatus(CashStatus::DISBURSED, 'Funds disbursed for purchase');
Tagging Example
Tags can classify or group cash instances:
$cash->attachTags(['finance', 'budget']); $cashWithTags = Cash::withAnyTags(['finance'])->get();
Testing
Unit Testing
- The suite uses Pest for fast and expressive unit tests.
- Coverage for:
- Validation:
CashValidationRulesDataTest.php,CashInstructionDataTest.php - Secure redemption:
CashSecretTest.php - Tagging:
CashTagTest.php - Statuses:
CashStatusTest.php - Instructions and metadata:
VoucherInstructionsDataTest.php
- Validation:
Sample Test:
Tests ensure methods like canRedeem work as expected:
it('does not allow redemption if cash has expired', function () { $cash = Cash::factory()->create([ 'expires_on' => now()->subDay(), // Expired yesterday 'secret' => 'secure-secret', ]); $validSecret = 'secure-secret'; expect($cash->canRedeem($validSecret))->toBeFalse(); // Fails due to expiration });
Run the test suite:
./vendor/bin/pest
Code Structure
Models
Cash: Core monetary entity with statuses, tags, and secure redemption.Voucher: A redeemable voucher that may have associated cash.
Data Classes
CashValidationRulesData: For cash-specific validation rules (e.g., mobile, location).VoucherInstructionsData: Holds all voucher-related instructions.
Enums
CashStatus: Tracks the current state of cash (e.g.,MINTED,EXPIRED).VoucherInputField: Lists possible input fields for voucher instructions (e.g.,MOBILE,EMAIL).
Key Files
| File | Purpose |
|---|---|
CashTest.php |
Core tests for Cash model functionality |
CashSecretTest.php |
Tests secure secret handling and redemption logic |
CashStatusTest.php |
Tests status management for Cash entity |
CashTagTest.php |
Tests tagging functionality for Cash entity |
VoucherInstructionsDataTest |
Tests voucher instructions and serialization |
Example Workflow
1. Create a Voucher with Cash:
Use structured data classes to create a voucher with cash, inputs, and validation:
$instructions = new VoucherInstructionsData([ 'cash' => [ 'amount' => 1000, 'currency' => 'PHP', 'validation' => [ 'secret' => 'secure-secret', ], ], ]); $voucher = Vouchers::create([ 'metadata' => ['instructions' => $instructions], ]);
2. Redeem a Voucher:
Validate cash redemption:
$cash = $voucher->cash; if ($cash->canRedeem($providedSecret)) { $cash->setStatus(CashStatus::DISBURSED); }
Conclusion
The Digital Voucher System with the Cash entity provides a robust framework for managing redeemable vouchers. Combined with secure secret handling, tagging, validation, and metadata storage, this system is designed to meet modern financial and transactional needs. Propel your application with this flexible and modular system! 🚀
3neti/voucher 适用场景与选型建议
3neti/voucher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「payments」 「laravel」 「wallet」 「voucher」 「redemption」 「issuance」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 3neti/voucher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 3neti/voucher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 3neti/voucher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The BlockTrail PHP SDK, for integration of Bitcoin functionality through the BlockTrail API
Dealing with payments through the Egyptian payment gateway PayMob
Librería para la gestión sencilla de pagos mediante TPV Redsys y Paypal
Library for validation of cryptocoin wallets
Alfabank REST API integration
Yoke: SSH Connection Manager/Wallet
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 26
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: proprietary
- 更新时间: 2026-04-03