Search code examples
c#asp.net-core.net-corestripe-paymentspayment-processing

Lack of documentation and questions for using SubscriptionItemUsageRecordService.Create()


I need help adding a customer's metered usage (this is a count of items for subscriptions that are based on metered usage).

Based on the documentation to Create a usage record, I created the following code.

SubscriptionItemUsageRecordCreateOptions options = new()
{
    Quantity = count,
    Timestamp = DateTime.UtcNow
};

SubscriptionItemUsageRecordService service = new();
await service.CreateAsync("xxxx", options);

Questions:

  1. The first argument to CreateAsync() is titled parentId. But there is no explanation for this, and searching the help for SubscriptionItemUsageRecordService returns no results. I have a customer ID. How would I get the value that goes here?

  2. What is the correct way to detect success or failure? I understand this method can throw exceptions, but it can also return null. I'm confused why Stripe doesn't have a consistent success/failure convention (and isn't better documented).


Solution

  • The "parent id" indicates the id of the object through which the UsageRecord is created. On that API, a UsageRecord is associated with a SubscriptionItem so the parent id should be the si_1234 from that SubscriptionItem. You can see it in Stripe's API reference docs here.

    Stripe also has an end to end guide for UsageRecord here: https://docs.stripe.com/billing/subscriptions/usage-based-legacy/recording-usage

    Note that this integration is considered deprecated/legacy by Stripe. They re-designed their entire usage-based billing APIs a few months ago. If you are just getting started you should look at the newer APIs instead: https://docs.stripe.com/billing/subscriptions/usage-based

    As for the failure, Stripe throws an exception if the request fails. See their detailed docs on error handling: https://docs.stripe.com/error-handling