Skip to content

Commit

Permalink
Automatic invoice advancement for proration (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadaismail-stripe authored Feb 23, 2023
1 parent 0490b8f commit eca29fc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/stripe-force/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ class FeatureFlags < T::Enum
ACCOUNT_POLLING = new('account_polling')
COUPONS = new('coupons')
TERMINATED_ORDER_ITEM_CREDIT = new('terminated_order_item_credit')
AUTO_ADVANCE_PRORATION_INVOICE = new('auto_advance_proration_invoices')
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/stripe-force/proration_auto_bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ def self.create_invoice_from_invoice_item_event(user, invoice_item_event)

# TODO we need billing to offer the ability to pass in a list of invoice items,
# this logic could "scoop up" invoice items which the user does NOT intend to bill
auto_advance = user.feature_enabled? FeatureFlags::AUTO_ADVANCE_PRORATION_INVOICE
invoice = Stripe::Invoice.create({
customer: customer_id,
subscription: subscription_id,
metadata: {
# TODO should we link to the originating order? Can we extract that from the line item metadata when we have it?
Translate::Metadata.metadata_key(user, MetadataKeys::PRORATION_INVOICE) => "true",
},
auto_advance: auto_advance,
}, user.stripe_credentials)

log.info 'invoice created for proration', invoice_id: invoice.id
Expand Down
3 changes: 3 additions & 0 deletions sorbet/custom/stripe.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ class Stripe::SubscriptionItem < Stripe::APIResource
end

class Stripe::Invoice
sig { returns(T::Boolean)}
def auto_advance; end

sig { returns(Integer)}
def period_end; end

Expand Down
15 changes: 15 additions & 0 deletions test/integration/test_proration_auto_bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ class Critic::ProrationAutoBillTranslation < Critic::FunctionalTest
end

it 'creates an invoice automatically for a invoice item' do
@user.enable_feature FeatureFlags::AUTO_ADVANCE_PRORATION_INVOICE, update: true
@user.enable_feature FeatureFlags::TEST_CLOCKS, update: true

subscription = create_customer_with_subscription
_, ad_hoc_price = create_price(additional_price_fields: {
recurring: {},
})

# create an invoice item that looks similar to what we'll get from add_invoice_items
invoice_item = Stripe::InvoiceItem.create({
customer: subscription.customer,
Expand All @@ -39,6 +43,17 @@ class Critic::ProrationAutoBillTranslation < Critic::FunctionalTest
invoice = T.must(invoice)
assert_equal(1, invoice.lines.count)
assert_equal("true", invoice.metadata[StripeForce::Translate::Metadata.metadata_key(@user, MetadataKeys::PRORATION_INVOICE)])

assert(invoice.auto_advance)
assert_equal('draft', invoice.status)

# let's advance the clock by an hour and test the invoice finalizes
stripe_customer = stripe_get(subscription.customer)
refute_nil(stripe_customer.test_clock)
advance_test_clock(stripe_customer, (Time.now + 2.hour).to_i)

invoice = Stripe::Invoice.retrieve(invoice.id, @user.stripe_credentials)
assert_not_equal('draft', invoice.status)
end

describe 'skip conditions' do
Expand Down
8 changes: 7 additions & 1 deletion test/support/stripe_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ def create_customer_with_subscription
end

def create_customer_with_card
customer = create_customer
if @user.feature_enabled?(FeatureFlags::TEST_CLOCKS) && !@user.livemode
test_clock = Stripe::TestHelpers::TestClock.create({
frozen_time: Time.now.to_i,
}, @user.stripe_credentials)
end
customer = create_customer(additional_fields: {test_clock: test_clock})

payment_method = Stripe::PaymentMethod.attach(
'pm_card_visa',
{customer: customer.id},
Expand Down

0 comments on commit eca29fc

Please sign in to comment.