tourze / credit-resource-bundle
Credit resource management bundle providing resource pricing and automatic billing functionality for Symfony applications
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/credit-resource-bundle
Requires
- php: ^8.1
- brick/math: ^0.13
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- knplabs/knp-menu: ^3.7
- nesbot/carbon: ^2.72 || ^3
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/http-kernel: ^6.4
- symfony/messenger: ^6.4
- symfony/security-core: ^6.4
- symfony/uid: ^6.4
- tourze/async-contracts: 0.0.*
- tourze/biz-user-bundle: 0.0.*
- tourze/bundle-dependency: 0.0.*
- tourze/credit-bundle: 0.0.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-snowflake-bundle: 0.1.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/doctrine-track-bundle: 0.1.*
- tourze/doctrine-user-bundle: 0.0.*
- tourze/easy-admin-attribute: 0.1.*
- tourze/easy-admin-menu-bundle: 0.1.*
- tourze/enum-extra: 0.1.*
- tourze/symfony-cron-job-bundle: 0.1.*
- yiisoft/arrays: ^3
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-11-01 19:12:26 UTC
README
[]
(https://packagist.org/packages/tourze/credit-resource-bundle)

[
]
(https://codecov.io/gh/tourze/php-monorepo)
[
]
(https://packagist.org/packages/tourze/credit-resource-bundle)
[
]
(https://packagist.org/packages/tourze/credit-resource-bundle)
Credit resource management bundle providing resource pricing and automatic billing functionality for Symfony applications. Supports flexible billing strategies with asynchronous bill processing via message queue.
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Configuration
- Advanced Usage
- Security
- Contributing
- License
- Authors
- Changelog
Features
- Resource pricing management with multiple billing strategies
- Automatic billing functionality with customizable rules
- Flexible billing cycles (hourly, daily, monthly, yearly)
- Tiered pricing support for complex billing scenarios
- Asynchronous bill processing via message queue
- Free quota and price ceiling/floor controls
- Integration with Symfony Messenger for scalable processing
Requirements
- PHP >= 8.1
- Symfony >= 7.3
- Doctrine ORM >= 3.0
Installation
composer require tourze/credit-resource-bundle
Quick Start
1. Register Bundle
Register the bundle in your Symfony application:
// config/bundles.php return [ // ... CreditResourceBundle\CreditResourceBundle::class => ['all' => true], ];
2. Configure Database
Run database migrations to create the required table structures:
php bin/console doctrine:migrations:migrate
3. Create Resource Price Configuration
use CreditResourceBundle\Entity\ResourcePrice; use CreditResourceBundle\Enum\FeeCycle; $resourcePrice = new ResourcePrice(); $resourcePrice->setTitle('VPN Usage'); $resourcePrice->setResource('App\Entity\VpnSession'); $resourcePrice->setCycle(FeeCycle::NEW_BY_HOUR); $resourcePrice->setPrice('0.01'); $resourcePrice->setValid(true); $entityManager->persist($resourcePrice); $entityManager->flush();
4. Generate Bills
# Manual billing execution php bin/console billing:create-resource-bill # Or configure as cron job (runs at 1st minute of every hour) # 1 * * * * php /path/to/project/bin/console billing:create-resource-bill
Configuration
Billing Strategies
The bundle supports multiple billing strategies:
Fixed Price Strategy
// Simple fixed price per unit $resourcePrice->setBillingStrategy(null); // Uses default fixed strategy $resourcePrice->setPrice('1.00'); // $1 per unit
Tiered Price Strategy
$resourcePrice->setBillingStrategy(TieredPriceStrategy::class); $resourcePrice->setPriceRules([ ['min' => 0, 'max' => 100, 'price' => '1.00'], // First 100 units: $1 each ['min' => 100, 'max' => 1000, 'price' => '0.80'], // Next 900 units: $0.80 each ['min' => 1000, 'max' => PHP_INT_MAX, 'price' => '0.50'] // 1000+: $0.50 each ]);
Billing Cycles
Configure different billing cycles based on your needs:
// Calculate new items added in the current hour $resourcePrice->setCycle(FeeCycle::NEW_BY_HOUR); // Calculate total items accumulated by end of day $resourcePrice->setCycle(FeeCycle::TOTAL_BY_DAY);
Price Controls
// Set free quota (first 100 units free) $resourcePrice->setFreeQuota(100); // Set minimum charge (at least $5 even if usage is low) $resourcePrice->setBottomPrice('5.00'); // Set maximum charge (never more than $100) $resourcePrice->setTopPrice('100.00');
Advanced Usage
Custom Resource Usage Providers
Implement the ResourceUsageProviderInterface to define custom usage calculation:
use CreditResourceBundle\Interface\ResourceUsageProviderInterface; use Symfony\Component\Security\Core\User\UserInterface; class CustomUsageProvider implements ResourceUsageProviderInterface { public function supports(string $resourceType): bool { return $resourceType === 'custom_resource'; } public function getUsage( UserInterface $user, string $resourceType, \DateTimeInterface $start, \DateTimeInterface $end ): int { // Custom usage calculation logic return $this->calculateCustomUsage($user, $start, $end); } public function getUsageDetails( UserInterface $user, string $resourceType, \DateTimeInterface $start, \DateTimeInterface $end ): array { // Return detailed usage information return []; } public function getPriority(): int { return 0; } }
Custom Billing Strategies
Create custom billing strategies for complex pricing rules:
use CreditResourceBundle\Interface\BillingStrategyInterface; use CreditResourceBundle\Entity\ResourcePrice; class VolumeDiscountStrategy implements BillingStrategyInterface { public function calculate(ResourcePrice $price, int $usage, array $context = []): string { $basePrice = bcmul($price->getPrice(), (string) $usage, 5); // Apply volume discount if ($usage > 1000) { $discount = bcmul($basePrice, '0.10', 5); // 10% discount return bcsub($basePrice, $discount, 5); } return $basePrice; } public function supports(ResourcePrice $price): bool { return true; } public function getName(): string { return 'volume_discount'; } public function getDescription(): string { return 'Volume discount strategy with 10% discount for usage over 1000 units'; } public function validateConfiguration(ResourcePrice $price): array { return []; } public function getPriority(): int { return 0; } }
Security
When using this bundle in production:
- Ensure proper access controls on admin interfaces
- Validate all pricing configurations before deployment
- Monitor billing logs for anomalies
- Use database transactions for critical billing operations
- Implement proper error handling and alerting
Contributing
Issues and Pull Requests are welcome. Please ensure:
- Code follows PSR-12 coding standards
- All tests must pass (
./vendor/bin/phpunit) - PHPStan analysis passes (
./vendor/bin/phpstan analyse) - New features require corresponding tests
- Update relevant documentation
License
This project is licensed under the MIT License. See the LICENSE file for details.
Authors
- Tourze Team
Changelog
See CHANGELOG.md for version history.