lib/pay/stripe/subscription.rb in pay-5.0.1 vs lib/pay/stripe/subscription.rb in pay-5.0.2

- old
+ new

@@ -238,17 +238,24 @@ # Uses the first subscription_item ID unless `subscription_item_id: "si_1234"` is passed # # create_usage_record(quantity: 4, action: :increment) # create_usage_record(subscription_item_id: "si_1234", quantity: 100, action: :set) def create_usage_record(**options) - subscription_item_id = options.fetch(:subscription_item_id, subscription_items.first["id"]) + subscription_item_id = options.fetch(:subscription_item_id, metered_subscription_item&.dig("id")) ::Stripe::SubscriptionItem.create_usage_record(subscription_item_id, options, stripe_options) end # Returns usage record summaries for a subscription item def usage_record_summaries(**options) - subscription_item_id = options.fetch(:subscription_item_id, subscription_items.first["id"]) + subscription_item_id = options.fetch(:subscription_item_id, metered_subscription_item&.dig("id")) ::Stripe::SubscriptionItem.list_usage_record_summaries(subscription_item_id, options, stripe_options) + end + + # Returns the first metered subscription item + def metered_subscription_item + subscription_items.find do |subscription_item| + subscription_item.dig("price", "recurring", "usage_type") == "metered" + end end # Returns an upcoming invoice for a subscription def upcoming_invoice(**options) ::Stripe::Invoice.upcoming(options.merge(subscription: processor_id), stripe_options)