Purchase credits
Initiates credit purchase via Stripe Checkout Session.
Purchase Process
Code
Prerequisites
- The user must have the owner role (no Stripe account for tenants)
- The user must have a
stripeCustomerIdin Better Auth- If absent, error 400: "You must have a Stripe account to purchase credits"
- The package must be active and have a
stripe_price_idconfigured
Validations
- ✅ Validation of
package_id(valid UUID) - ✅ URL validation (
success_urlandcancel_urlmust be valid URLs) - ✅ Verification that the package exists and is active
- ✅ Verification that the package has a
stripe_price_id - ✅ Retrieval of
stripeCustomerIdfrom Better Auth - ✅ Creation of Stripe Checkout Session with complete metadata
Stripe Metadata
The Stripe Checkout Session includes the following metadata for webhook processing:
Code
This metadata allows the webhook to credit the right user in the correct tenant after successful payment.
Error Handling
- 400 Bad Request: Invalid package, invalid URLs, or no Stripe account
- 401 Unauthorized: Invalid or expired JWT token
- 403 Forbidden: User is not an owner
- 404 Not Found: Package not found or inactive
- 500 Internal Server Error: Error creating the Checkout Session
Usage Example
Code
Response:
Code
The user must then be redirected to checkout_url to complete payment.
Purchase credits › Request Body
package_idIdentifier of the credit package to purchase.
The package must be active and have a configured stripe_price_id.
success_urlRedirect URL after successful payment.
The user will be redirected to this URL after completing the payment on the Stripe Checkout page.
Recommendations:
- Include a success parameter to identify the transaction
- Example:
https://app.fairplace.com/credits/success?session_id={CHECKOUT_SESSION_ID}
cancel_urlRedirect URL if the user cancels the payment.
The user will be redirected to this URL if they close the Stripe Checkout page without completing the payment.
Purchase credits › Responses
Purchase session created successfully.
The user must be redirected to checkout_url to complete payment.
Credits will be automatically credited after successful payment via webhook.
checkout_urlStripe Checkout page URL.
Required action: Redirect the user to this URL to complete the payment.
This URL is valid for a limited time (typically 24 hours). If the user does not complete the payment within this period, the session expires.
package_idIdentifier of the selected package.
package_nameName of the selected package.
credits_amountNumber of credits that will be credited after successful payment (in cents).
price_centsPackage price in euro cents.