Credit System
The Credit System in ShipFast is designed to manage and track user credits, allowing users to perform actions or operations that cost a certain amount of credits. You can integrate this feature into various parts of your application where users can use their credits to interact with the system.
Key Features
1. Spend Credits for Operations
Each operation can be assigned a specific credit cost. The performOperation
function helps you handle the logic for spending credits and performing actions. Here's how you can use it:
Example Code for Using Credits
import useCreditSystem from "@/hooks/useCreditSystem";
const { performOperation } = useCreditSystem();
await performOperation({
operationCost: 1, // Cost of the operation (e.g., 1 credit)
operationFn: () => {
console.log("Item added to list!");
alert("Item added to list!");
},
onFailure: error => {
setErrorMessage(error);
}
});
File Path: /src/hooks/useCreditSystem.js
Explanation:
operationCost:
This is the number of credits required to perform the operation. In this example, it costs 1 credit.
operationFn:
This is the function that performs the actual operation (e.g., adding an item to a list). The function should contain the logic of the task that is being performed, such as showing an alert or logging a message.
onFailure:
If the operation fails (for example, if the user doesn’t have enough credits), this callback will be triggered, and you can handle the error (e.g., showing an error message or preventing further actions).
Note: If the Credit System feature is disabled in
config/features
, the operation function (operationFn
) will be executed immediately without deducting credits or waiting. The credit check and deduction will be skipped.
2. Credit Deduction and Error Handling
The system ensures that credits are deducted only if the operation is successful. If the user doesn't have enough credits, the operation is prevented, and an error message can be displayed.
- Check Credit Balance: The system checks if the user has sufficient credits before performing the operation.
- Deduct Credits: If the operation is successful, the necessary number of credits are deducted from the user's balance.
- Handle Failure: If the operation cannot proceed (e.g., due to insufficient credits), the failure is captured and handled by the
onFailure
callback, allowing the user to be notified of the issue.
3. Configurable Credit Operations
You can configure the credit cost for different operations based on your application's needs. The credit cost for each operation can be set using the operationCost
property. This allows you to specify how many credits each action will cost. For example, if you want a specific operation to cost 3 credits, you can set operationCost: 3
in the performOperation
function.
This flexibility ensures you can easily adjust the cost for various actions in your app, providing control over how credits are consumed.
Important Notes
-
Credit System Disabled: If the Credit System feature is disabled in
config/features
, the operation function (operationFn
) will be executed immediately, without deducting credits or waiting. The credit check and deduction will be skipped. -
Subscription and Invoice Status: Ensure that subscriptions are not marked as canceled. If a subscription is canceled, it will no longer generate invoices, and the credit system may not track the billing cycle properly. Instead, subscriptions should be marked as unpaid or past-due. This ensures that the credit system can continue tracking the subscription and invoice statuses. If a payment fails, the subscription should be marked as unpaid or past-due, and invoices should be marked as uncollectible if all retries for payment fail. This helps in tracking and accurate accounting.
- To configure subscription status and ensure the right settings, go to Billing > Settings > Subscriptions and emails in your Stripe Dashboard (opens in a new tab).
- To configure invoice status for failed payments, you can also adjust settings under Billing > Settings > Subscriptions and emails in your Stripe Dashboard.
By properly configuring these settings, you can ensure the credit system works as intended, even in the event of failed payments, and maintain accurate billing and subscription tracking.