[PR #3620] [CLOSED] feat (Stripe): Basic Implementation of Usage Tracking #4917

Closed
opened 2026-03-13 12:04:11 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/3620
Author: @dagmawibabi
Created: 7/26/2025
Status: Closed

Base: mainHead: ubb


📝 Commits (5)

📊 Changes

5 files changed (+723 additions, -108 deletions)

View changed files

📝 packages/stripe/src/index.ts (+248 -1)
📝 packages/stripe/src/schema.ts (+28 -0)
📝 packages/stripe/src/stripe.test.ts (+408 -107)
📝 packages/stripe/src/types.ts (+22 -0)
📝 packages/stripe/src/utils.ts (+17 -0)

📄 Description

This pull request introduces usage-based billing functionality to the Stripe integration, enabling tracking and retrieval of usage metrics for customers. It includes updates to the schema, endpoints, utility functions, and tests to support this feature.

Usage-Based Billing Feature:

  • New Endpoints for Usage Tracking and Retrieval:

    • Added trackUsage and getUsage endpoints to handle usage tracking and retrieval for customers. These endpoints support aggregation by day or hour and sync usage data with Stripe. (packages/stripe/src/index.ts, [1] [2]
  • Schema Updates:

    • Introduced a new usage schema to store usage data, including fields like stripeCustomerId, eventName, value, and createdAt. (packages/stripe/src/schema.ts, [1] [2]
  • Type Definitions:

    • Added Usage type and updated StripePlan and StripeOptions to include usage-based billing configurations, such as limits, event names, aggregation methods, and time periods. (packages/stripe/src/types.ts, [1] [2] [3]

Utility and Helper Functions:

  • Utility for Date Comparison:

Test Enhancements:

  • Mocking Stripe Billing API:

    • Extended the mocked Stripe API to include billing meters and meter events for testing usage-based billing. (packages/stripe/src/stripe.test.ts, [1] [2]
  • New Tests for Usage Tracking and Retrieval:

    • Added test cases to verify usage tracking, retrieval, and scenarios with no usage data. These tests ensure the new functionality works as expected. (packages/stripe/src/stripe.test.ts, packages/stripe/src/stripe.test.tsR953-R1126)

Summary by cubic

Added basic usage-based billing to the Stripe integration, allowing tracking and retrieval of customer usage data with daily or hourly aggregation.

  • New Features
    • Added trackUsage and getUsage endpoints for recording and fetching usage.
    • Introduced a usage schema and related types to store usage events.
    • Updated tests to cover usage tracking and retrieval scenarios.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/3620 **Author:** [@dagmawibabi](https://github.com/dagmawibabi) **Created:** 7/26/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `ubb` --- ### 📝 Commits (5) - [`1a38d59`](https://github.com/better-auth/better-auth/commit/1a38d59da2d2c982e2f14babc62d4a1ebe419101) tracking w aggregate sum working - [`5a58f26`](https://github.com/better-auth/better-auth/commit/5a58f2611f4ecd6051df1db1962f2260f3352ef5) basic ubb - [`dff2c30`](https://github.com/better-auth/better-auth/commit/dff2c308ac64717128e285e06f412ca9ef1c4b66) fixed cubic issues - [`599e6b9`](https://github.com/better-auth/better-auth/commit/599e6b98372d686f2358691dcb4a2684c046514e) resolving issues - [`6784447`](https://github.com/better-auth/better-auth/commit/678444731a32620eb267b9234be1b49e880339fe) usage based billing basics ### 📊 Changes **5 files changed** (+723 additions, -108 deletions) <details> <summary>View changed files</summary> 📝 `packages/stripe/src/index.ts` (+248 -1) 📝 `packages/stripe/src/schema.ts` (+28 -0) 📝 `packages/stripe/src/stripe.test.ts` (+408 -107) 📝 `packages/stripe/src/types.ts` (+22 -0) 📝 `packages/stripe/src/utils.ts` (+17 -0) </details> ### 📄 Description This pull request introduces usage-based billing functionality to the Stripe integration, enabling tracking and retrieval of usage metrics for customers. It includes updates to the schema, endpoints, utility functions, and tests to support this feature. ### Usage-Based Billing Feature: * **New Endpoints for Usage Tracking and Retrieval**: - Added `trackUsage` and `getUsage` endpoints to handle usage tracking and retrieval for customers. These endpoints support aggregation by day or hour and sync usage data with Stripe. (`packages/stripe/src/index.ts`, [[1]](diffhunk://#diff-ccb5282d4490ceec8e8c99f978be2383dd1915db21316f3baba6066420070b08R1033-R1234) [[2]](diffhunk://#diff-ccb5282d4490ceec8e8c99f978be2383dd1915db21316f3baba6066420070b08R1301) * **Schema Updates**: - Introduced a new `usage` schema to store usage data, including fields like `stripeCustomerId`, `eventName`, `value`, and `createdAt`. (`packages/stripe/src/schema.ts`, [[1]](diffhunk://#diff-91fd21bda903000607a4effb99381eb89e17b478d6683a31c00ffb2f2029582aR60-R82) [[2]](diffhunk://#diff-91fd21bda903000607a4effb99381eb89e17b478d6683a31c00ffb2f2029582aR94) * **Type Definitions**: - Added `Usage` type and updated `StripePlan` and `StripeOptions` to include usage-based billing configurations, such as limits, event names, aggregation methods, and time periods. (`packages/stripe/src/types.ts`, [[1]](diffhunk://#diff-34efa9c605b726e157bb1f56b408f03c2a1abceb7b49c3f60d86c60d8fd0e55dR91-R99) [[2]](diffhunk://#diff-34efa9c605b726e157bb1f56b408f03c2a1abceb7b49c3f60d86c60d8fd0e55dR335-R338) [[3]](diffhunk://#diff-34efa9c605b726e157bb1f56b408f03c2a1abceb7b49c3f60d86c60d8fd0e55dR349-R356) ### Utility and Helper Functions: * **Utility for Date Comparison**: - Added `isSameDay` function to compare dates, used for determining if usage aggregation should occur. (`packages/stripe/src/utils.ts`, [packages/stripe/src/utils.tsR26-R33](diffhunk://#diff-3fe1f6f8176987831e75602ccd92047f94910800aee7079348177f2dd15271eeR26-R33)) ### Test Enhancements: * **Mocking Stripe Billing API**: - Extended the mocked Stripe API to include billing meters and meter events for testing usage-based billing. (`packages/stripe/src/stripe.test.ts`, [[1]](diffhunk://#diff-0719c098a988c165e85048b409548dcfabd9e0ab39916077a54889f860cb9769R43-R130) [[2]](diffhunk://#diff-0719c098a988c165e85048b409548dcfabd9e0ab39916077a54889f860cb9769R141) * **New Tests for Usage Tracking and Retrieval**: - Added test cases to verify usage tracking, retrieval, and scenarios with no usage data. These tests ensure the new functionality works as expected. (`packages/stripe/src/stripe.test.ts`, [packages/stripe/src/stripe.test.tsR953-R1126](diffhunk://#diff-0719c098a988c165e85048b409548dcfabd9e0ab39916077a54889f860cb9769R953-R1126)) <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Added basic usage-based billing to the Stripe integration, allowing tracking and retrieval of customer usage data with daily or hourly aggregation. - **New Features** - Added `trackUsage` and `getUsage` endpoints for recording and fetching usage. - Introduced a `usage` schema and related types to store usage events. - Updated tests to cover usage tracking and retrieval scenarios. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:04:11 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#4917