DEV Community

Discussion on: Stripe - Upgrading a subscription synchronously (immediately charge for a proration without webhooks)

Collapse
 
marckohlbrugge profile image
Marc Köhlbrugge

I haven't thoroughly tested, but this seems to work more reliably:

invoice.lines.data.pluck(:amount).keep_if(&:negative?).inject(:+)

👆 This returns the 'unused amount'. So if a customer is halfway through a $25/mo period, this would be about $12.50

It looks through all the line items, selects just the negative amounts, and adds them all up. Usually this is just one line item, but in theory it could be multiple.

invoice.total

👆 This is pretty simple. It's the total amount (including any taxes) the customer were to pay. So continuing the $25/mo example, with let's say a $200/year annual plan, this would be about $200 - $12.50 (see above) = $187.50