Skip to content

Commit

Permalink
Disable idempotency in sandbox (#724)
Browse files Browse the repository at this point in the history
* added if

* cop

* fixed tests
  • Loading branch information
brennen-stripe authored Sep 7, 2022
1 parent 08b752a commit ac5f9f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/stripe-force/translate/translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,16 @@ def extract_salesforce_params!(sf_record, stripe_record_or_class)

sig { params(user: StripeForce::User, sf_object: Restforce::SObject, action: T.nilable(Symbol)).returns(Hash) }
def generate_idempotency_key_with_credentials(user, sf_object, action=nil)
if user.sandbox?
# Skip idempotency keys in sandbox. Using an idemptotency key is useful to ensure we do not create duplicate
# subscriptions but also causes problems when for example a request fails because of a mapping issue
# (ie days_until_due on invoice settings is not being set) and upon fixing the mapping via the UI and retrying
# it will fail due to duplicate idempotency key with a different request body.

# We cannot just add the modified date to the key as that would not have changed in the example above.
return @user.stripe_credentials
end

# TODO if the SF object is mutated in a way which changes inputs, we need a new idempotency key, maybe use created date?
# TODO feature flag to turn this off
key = sf_object[SF_ID]
Expand Down
2 changes: 2 additions & 0 deletions test/unit/test_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class TranslatorTest < Critic::UnitTest
it 'generates a key' do
sf_order = create_mock_salesforce_order

@user.expects(:sandbox?).once.returns(false)
creds = @translator.generate_idempotency_key_with_credentials(@user, sf_order)

assert_equal(sf_order.Id, creds.delete(:idempotency_key))
Expand All @@ -133,6 +134,7 @@ class TranslatorTest < Critic::UnitTest
it 'generates a key with an action' do
sf_order = create_mock_salesforce_order

@user.expects(:sandbox?).once.returns(false)
creds = @translator.generate_idempotency_key_with_credentials(
@user,
sf_order,
Expand Down

0 comments on commit ac5f9f7

Please sign in to comment.