Below is some guidance addressing your two questions.
1. What Should Be Passed as the Parent ID?
The first argument to CreateAsync() is the subscription item ID—not the customer’s ID. In Stripe’s API, usage records are associated with a subscription item since it’s the subscription item that defines the metered billing quantity. You'll need to retrieve the subscription associated with your customer, then get its subscription items (which typically have IDs that start with “si_”). Once you have that subscription item ID, pass it as the parentId in your call.
For example, if you’ve already obtained the subscription item id (say “si_123456”), your call should look like:
SubscriptionItemUsageRecordCreateOptions options = new()
{
Quantity = count,
Timestamp = DateTime.UtcNow
};
SubscriptionItemUsageRecordService service = new();
var usageRecord = await service.CreateAsync("si_123456", options);
2. How to Detect Success or Failure?
When you call CreateAsync(), a successful response should return a usage record object. If there’s a problem (such as an invalid parameter or network error), the Stripe library typically throws an exception (such as a StripeException) rather than returning null.
You should use a try/catch block to detect and handle errors. It is unusual for the method to return null. Instead, rely primarily on exception handling to detect failures. For example:
try
{
var usageRecord = await service.CreateAsync("si_123456", options);
if (usageRecord != null)
{
// Success - process the usage record
}
else
{
// Unlikely, but handle the case if usageRecord is null
}
}
catch (StripeException ex)
{
// Handle Stripe-specific errors here
// For example, log the error and alert the user
}
catch (Exception ex)
{
// Handle other potential exceptions
}
Using exceptions for error handling is the standard convention in the Stripe .NET libraries. This approach gives you a reliable way to differentiate between successful and failed API calls.
This should help you correctly implement metered usage for your subscriptions based on Stripe's documentation. Let me know if you need further details.