cboxdk/laravel-billing-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

cboxdk/laravel-billing-client

Composer 安装命令:

composer require cboxdk/laravel-billing-client

包简介

Cbox Billing — app-local enforcement SDK: leases allowance from a remote Cbox Billing service and enforces hard limits locally on the hot path, buffering and reporting usage in the background.

README 文档

README

cboxdk/laravel-billing-client — the app-local enforcement SDK a product app embeds to bill against a remote Cbox Billing service. It enforces usage limits locally, on the hot path — no network round-trip per request — while billing stays the eventual authority.

Install

composer require cboxdk/laravel-billing-client
# .env
BILLING_CLIENT_BASE_URL=https://billing.internal
BILLING_CLIENT_API_TOKEN=your-service-token
BILLING_CLIENT_LEASE_SIZE=100
BILLING_CLIENT_FAIL=allow

With a base URL and token set, the provider binds the HTTP transport and the BillingClient automatically.

Use

use Cbox\Billing\Client\BillingClient;
use Cbox\Billing\Client\Exceptions\QuotaExceeded;

public function handle(BillingClient $billing): mixed
{
    try {
        $reservation = $billing->reserve('org_123', 'api.calls', 1);
    } catch (QuotaExceeded) {
        abort(429, 'Usage limit reached.');
    }

    try {
        $result = $this->doTheWork();
        $billing->commit($reservation, actual: 1);
        return $result;
    } catch (\Throwable $e) {
        $billing->release($reservation);
        throw $e;
    }
}

Reserve several meters atomically by passing a [meter => estimate] map — all-or-nothing across dimensions, each taken from its own local lease:

$set = $billing->reserve('org_123', ['api.calls' => 1, 'compute.ms' => 250]);
$billing->commit($set, ['api.calls' => 1, 'compute.ms' => 210]);

Schedule the background usage flush and the abandoned-reservation sweep:

Schedule::command('billing:report-usage')->everyMinute();
Schedule::command('billing:sweep-reservations')->everyFiveMinutes();

Self-service management

A typed BillingManagement client (and BillingManager facade) lets a product app's users manage their own billing over the management API:

use Cbox\Billing\Client\Facades\BillingManager;

$plans   = BillingManager::plans();
$preview = BillingManager::previewChange('org_123', 'pro');
$result  = BillingManager::subscribe('org_123', 'pro');   // + payment intent if due
$usage   = BillingManager::usage('org_123');

How it works

Two tiers:

  • Hot path (local, no network). A reservation takes units from a node-local leased slice of the organization's allowance via an atomic decrement-and-compensate.
  • Background (remote). When the slice runs short the SDK leases a fresh slice from billing; committed usage is buffered durably and reported back cumulatively.

Leasing is pessimistic — billing reserves the granted units centrally — so an organization can never exceed its allowance beyond a bounded overshoot of roughly lease_size × nodes. Usage reporting is cumulative and self-correcting: a dropped report is backfilled by the next flush, which carries the running total.

Failure policy

Failure handling splits by cause:

  • An exhausted allowance (billing granted zero) is a semantic hard limit — QuotaExceeded, always fail closed.
  • An unreachable billing service is an infrastructure fault, resolved by the fail policy: allow admits best-effort (usage still buffered and reconciled later); deny refuses.

More enforcement hardening

  • Reservation TTL recovery. A held reservation a crashed request never settles is swept back to the local slice (billing:sweep-reservations), not leaked.
  • Single-flight refills. A burst that empties a lease is coalesced behind a per-(org, meter) cache lock into one round-trip.
  • Durable buffer options. Cache-backed by default, or a crash-safe database buffer (buffer => 'database'; publish the migration) that survives eviction and restart.
  • Observability signals. BillingSignals (allowed / denied / refill / report) so a host can meter the meter; no-op by default, LoggingBillingSignals or your own metrics optional.

Design

  • One network seam per surface. Contracts\BillingTransport (enforcement) and Contracts\ManagementTransport (self-service) are the only things that touch the network — real Http\* implementations (bearer token, deny-by-default about responses) in production, Testing\Fake*Transport in tests.
  • Contracts-first, deny-by-default. Depend on interfaces; unknown meters/plans are not entitled; malformed or non-2xx responses raise rather than being trusted.
  • Dogfooded testing. Testing\InteractsWithBillingClient drives the whole two-tier flow and the management flow offline; the package's own suite uses it.

Requirements

PHP ^8.4; Laravel ^12 || ^13. A cache store with atomic increment / decrement (any Laravel driver) backs the local counters, the reservation registry, and the cache usage ledger; the database buffer additionally uses illuminate/database.

Documentation

See docs/ — overview, quickstart, core concepts (two-tier leasing, multi-meter enforcement, reservation recovery, cumulative reporting, the failure policy, the management client, and the architecture), and the self-service cookbook.

License

MIT.

cboxdk/laravel-billing-client 适用场景与选型建议

cboxdk/laravel-billing-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 cboxdk/laravel-billing-client 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-15