From cfdde8c9796115d5ccd4ecb14ab331442ef09856 Mon Sep 17 00:00:00 2001 From: Nada Ismail <112021025+nadaismail-stripe@users.noreply.github.com> Date: Wed, 25 Jan 2023 12:25:46 -0800 Subject: [PATCH] Update 'add_invoice_items.period.end.type' on proration invoices (#987) --- lib/stripe-force/translate/order/amendments.rb | 4 ++-- sorbet/custom/stripe.rbi | 8 ++++++++ .../amendments/test_proration_amendments.rb | 15 ++++++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/stripe-force/translate/order/amendments.rb b/lib/stripe-force/translate/order/amendments.rb index aaddf8522a..cbcb801a46 100644 --- a/lib/stripe-force/translate/order/amendments.rb +++ b/lib/stripe-force/translate/order/amendments.rb @@ -202,7 +202,7 @@ def self.generate_proration_credits_from_terminated_phase_items(user:, sf_order_ price_data: price_data, period: { end: { - type: 'phase_end', + type: 'subscription_period_end', }, start: { type: 'phase_start', @@ -288,7 +288,7 @@ def self.generate_proration_items_from_phase_items(user:, sf_order_amendment:, p price: proration_price.id, period: { end: { - type: 'phase_end', + type: 'subscription_period_end', }, start: { type: 'phase_start', diff --git a/sorbet/custom/stripe.rbi b/sorbet/custom/stripe.rbi index 940212bc8b..8da9be9dc5 100644 --- a/sorbet/custom/stripe.rbi +++ b/sorbet/custom/stripe.rbi @@ -267,6 +267,14 @@ class Stripe::SubscriptionItem < Stripe::APIResource def discounts=(arg); end end +class Stripe::Invoice + sig { returns(Integer)} + def period_end; end + + sig { returns(Integer)} + def period_start; end +end + class Stripe::InvoiceItem sig { returns(T.any(String, Stripe::Subscription))} def subscription; end diff --git a/test/integration/amendments/test_proration_amendments.rb b/test/integration/amendments/test_proration_amendments.rb index 5d3d668c81..60c0d225e5 100644 --- a/test/integration/amendments/test_proration_amendments.rb +++ b/test/integration/amendments/test_proration_amendments.rb @@ -31,7 +31,6 @@ class Critic::ProratedAmendmentTranslation < Critic::OrderAmendmentFunctionalTes additional_product_fields: { CPQ_QUOTE_BILLING_FREQUENCY => CPQBillingFrequencyOptions::ANNUAL.serialize, CPQ_QUOTE_SUBSCRIPTION_TERM => nil, - # CPQ_QUOTE_SUBSCRIPTION_TERM => 12, } ) @@ -48,7 +47,6 @@ class Critic::ProratedAmendmentTranslation < Critic::OrderAmendmentFunctionalTes # increase quantity amendment_data["lineItems"].first["record"][CPQ_QUOTE_QUANTITY] = 3 - amendment_data["record"][CPQ_QUOTE_SUBSCRIPTION_START_DATE] = format_date_for_salesforce(amendment_start_date) amendment_data["record"][CPQ_QUOTE_SUBSCRIPTION_TERM] = amendment_term @@ -112,7 +110,7 @@ class Critic::ProratedAmendmentTranslation < Critic::OrderAmendmentFunctionalTes prorated_price = Stripe::Price.retrieve(T.cast(prorated_item.price, String), @user.stripe_credentials) # check additional fields added to the proration invoice item - assert_equal("phase_end", prorated_item.period.end.type) + assert_equal("subscription_period_end", prorated_item.period.end.type) assert_equal("phase_start", prorated_item.period.start.type) assert_equal("true", prorated_item.metadata[StripeForce::Translate::Metadata.metadata_key(@user, MetadataKeys::PRORATION)]) assert_equal(second_phase_item_additive.metadata['salesforce_order_item_id'], prorated_item.metadata['salesforce_order_item_id']) @@ -155,6 +153,13 @@ class Critic::ProratedAmendmentTranslation < Critic::OrderAmendmentFunctionalTes assert_equal(2, invoice_line.quantity) assert_equal(60_00 * 2, invoice.total) assert_equal("true", invoice.metadata[StripeForce::Translate::Metadata.metadata_key(@user, MetadataKeys::PRORATION_INVOICE)]) + + # sanity check the usage period of this proration invoice + # should be from the amendment start to the next billing cycle date + assert_equal(amendment_start_date.to_i, invoice.period_start) + # the proration period end should align with the next billing cycle + # since this is billed 'Annually', add a year to the initial order start date to get the start of the next billing cycle + assert_equal((initial_order_start_date + 1.year).to_i, invoice.period_end.to_i) end # NOTE this was the first test written and has more extensive edge cases than other amendment tests @@ -276,7 +281,7 @@ class Critic::ProratedAmendmentTranslation < Critic::OrderAmendmentFunctionalTes prorated_price = Stripe::Price.retrieve(T.cast(prorated_item.price, String), @user.stripe_credentials) # check additional fields added to the proration invoice item - assert_equal("phase_end", prorated_item.period.end.type) + assert_equal("subscription_period_end", prorated_item.period.end.type) assert_equal("phase_start", prorated_item.period.start.type) assert_equal("true", prorated_item.metadata[StripeForce::Translate::Metadata.metadata_key(@user, MetadataKeys::PRORATION)]) assert_equal(second_phase_item_additive.metadata['salesforce_order_item_id'], prorated_item.metadata['salesforce_order_item_id']) @@ -401,7 +406,7 @@ class Critic::ProratedAmendmentTranslation < Critic::OrderAmendmentFunctionalTes second_phase_item_additive_price = Stripe::Price.retrieve(T.cast(second_phase_item_additive.price, String), @user.stripe_credentials) # check additional fields added to the proration invoice item - assert_equal("phase_end", prorated_item.period.end.type) + assert_equal("subscription_period_end", prorated_item.period.end.type) assert_equal("phase_start", prorated_item.period.start.type) assert_equal("true", prorated_item.metadata[StripeForce::Translate::Metadata.metadata_key(@user, MetadataKeys::PRORATION)]) assert_equal(second_phase_item_additive.metadata['salesforce_order_item_id'], prorated_item.metadata['salesforce_order_item_id'])