#fiery_hare_68246

1 messages · Page 1 of 1 (latest)

marsh stoneBOT
inland lagoon
#

Hi there, so you want to build a usage based subscription and only create an invoice when the amount reaches a threshold ?

plucky yacht
#

I was using the code below but it does seems to work well.

#

var pendingInvoiceItems = _stripeAdapter.InvoiceItemListAsync(new Stripe.InvoiceItemListOptions
{
Customer = subscriber.GatewayCustomerId
}).ToList().Where(i => i.InvoiceId == null);
var pendingInvoiceItemsDict = pendingInvoiceItems.ToDictionary(pii => pii.Id);

    var upcomingPreview = await _stripeAdapter.InvoiceUpcomingAsync(new Stripe.UpcomingInvoiceOptions
    {
        Customer = subscriber.GatewayCustomerId,
        Subscription = subscriber.GatewaySubscriptionId,
        SubscriptionItems = subItemOptions
    });

    var itemsForInvoice = upcomingPreview.Lines?.Data?
        .Where(i => pendingInvoiceItemsDict.ContainsKey(i.Id) || (i.Plan.Id == subItemOptions[0]?.Plan && i.Proration));
    var invoiceAmount = itemsForInvoice?.Sum(i => i.Amount) ?? 0;
    var invoiceNow = invoiceAmount >= prorateThreshold;
#

Thanks

inland lagoon
#

You code is about retrieving the upcoming invoice, is this what you want to achieve?

plucky yacht
#

No, I think your suggestion works. What i want to achive is to mutiple all existing subscription and set a billingthreashold mount that will be reached before inovice is generated and send to customer and after which it will be reseted. The problem I am trying to solve is a situation that customer gets invoice for any quanity update in a subscription and most of the time amount are less than a dollar. I will want the amount to get to $5 before the customer will be invoice. And if the customer stops at $4 without any other update, Then the $4 will be Proration and add to customer next bill invoice

inland lagoon
#

Just to clarify, so you want to set up a threshold across multiple subscriptions?

plucky yacht
#

Yes, I have many customer with subscriptions and I want to set the threshold for new subscriptions and existing ones.

inland lagoon
#

I'm afraid you can set a threshold on a subscription, but you can't set a threshold across multiple subscriptions.

plucky yacht
#

each subscription is unique to the customer. So I can only set the threshold on 1 subscription only even when I have 200 customer with 200 different subscriptions

inland lagoon
#

A customer can have many subscriptions. It's just that you can only set a threshold on a subscription.

plucky yacht
#

Only 1 subscription for that customer or only 1 subscription to all the subscriptions i have?

#

How do I solve this problem that I am facing?