Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AddPaymentSourcesToWallet changing default when reused #4198

Conversation

RyanofWoods
Copy link
Contributor

Fixes issue #4004 [1]

PR #2913 [2] changed how the new default WalletPaymentSource was set,
from using the last one found/created * within the Order's
PaymentSources to the user's last WalletPaymentSource. A subtle
difference which assumes that the user's last WalletPaymentSource was
just created by add_to_wallet.

However, if the Order's PaymentSource was from the User's wallet default
and add_to_wallet is called, a new WalletPaymentSource would not be
created * and make_default would try to set the new default as the last
WalletPaymentSource. This would change the default if the last
WalletPaymentSource was not the default.

Now, the WalletPaymentSources are scoped to the order's PaymentSources
and the last one is given to Wallet#default_wallet_payment_source=.
Meaning that in this scenario, the default would be given and ignored as it
is already the default.

[1] #4004
[2] #2913

  • Wallet#add creates or finds the WalletPaymentSource if exists.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@waiting-for-dev
Copy link
Contributor

I might be a bit lost here. But isn't it effectively reverting #2913? Wouldn't #2652 still be an issue in this case?

I haven't studied it closely enough, but I guess that ideally, #add_to_wallet shouldn't try to set the default unless:

  • Only one source exists
  • The user checks a "set default" checkbox (so we'd need to push that data from the controller layer down to this method).

@RyanofWoods
Copy link
Contributor Author

I might be a bit lost here. But isn't it effectively reverting #2913? Wouldn't #2652 still be an issue in this case?

I haven't studied it closely enough, but I guess that ideally, #add_to_wallet shouldn't try to set the default unless:

  • Only one source exists
  • The user checks a "set default" checkbox (so we'd need to push that data from the controller layer down to this method).

Thank you for raising this to my attention. Thinking about it more, I think you are correct. 👍 #add_to_wallet shouldn't set the default unless the user gives input to do so. If I change the PR to do this, then it can be to resolve #2652 and would automatically resolve #4004.

I think perhaps it would be more appropriate for Spree::Wallet to set default the wallet_payment_source if it is the only one. Then it can be done as callbacks for adding or removing a wallet_payment_source, what do you think?

@waiting-for-dev
Copy link
Contributor

I think perhaps it would be more appropriate for Spree::Wallet to set default the wallet_payment_source if it is the only one. Then it can be done as callbacks for adding or removing a wallet_payment_source, what do you think?

Do you mean Active Record callbacks? I'd strongly recommend against using them. Coupling business logic with persistence is an endless source of problems. My feeling is that we're trying to move away from them in Solidus.

I'm not very familiar with that part of the system, so at first sight, I'd leave that piece of logic there if there's nowhere more appropriate. About making that method to know if a user has checked something, I have no idea how hard it'll be 🙂 I'm happy to help here if it's needed.

Copy link
Contributor

@adammathys adammathys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @waiting-for-dev on almost all points here. Ideally we're only setting a default source if there isn't one or if the user prompts to have it changed. Also, I think the logic for setting the initial default fits fairly well within the AddPaymentSourcesToWallet class instead of trying to add it to callbacks. e.g.:

unless order.user.wallet.default_wallet_payment_source
  order.user.wallet.default_wallet_payment_source = new_default_source
end

That being said, I don't think this PR as is reverts #2913. The main purpose of that change was to make it easier to override the make_default behaviour so that users could decide to opt out of the default behaviour of having the most recently used credit card becoming the default. (I think the switch to have it be "most recently created" instead of "most recently used" was an unintended side-effect of that change.)

@waiting-for-dev
Copy link
Contributor

It makes sense. Another point to consider, as @kennyadsl commented offline, is payments made offsite, where users can't select a default payment method. We definitely should give more thought and study a suitable architecture for a broad solution going beyond the scope of this PR.

Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can merge it. It's not the complete solution, but at least it's fixing the issue introduced in #2913.

@RyanofWoods, I left some suggestions that, when combined, get rid of the unneeded global state and make use of a local variable instead.

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RyanofWoods hey, thanks a lot for this. I think it's close to something we can merge if you can address comments made by @waiting-for-dev and myself. 🙏

@waiting-for-dev waiting-for-dev added the changelog:solidus_core Changes to the solidus_core gem label Aug 30, 2022
@RyanofWoods RyanofWoods force-pushed the fix/add-wallet-payments-sources-changing-reused-default-payment-source branch from 838f64a to b429598 Compare October 31, 2022 12:55
@codecov
Copy link

codecov bot commented Oct 31, 2022

Codecov Report

Merging #4198 (88aed7e) into master (d7ef0f2) will increase coverage by 15.61%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##           master    #4198       +/-   ##
===========================================
+ Coverage   71.08%   86.69%   +15.61%     
===========================================
  Files         578      578               
  Lines       15611    14673      -938     
===========================================
+ Hits        11097    12721     +1624     
+ Misses       4514     1952     -2562     
Impacted Files Coverage Δ
...dels/spree/wallet/add_payment_sources_to_wallet.rb 100.00% <100.00%> (ø)
backend/lib/spree/backend/action_callbacks.rb 100.00% <0.00%> (+40.00%) ⬆️
backend/lib/spree/backend/callbacks.rb 93.33% <0.00%> (+46.66%) ⬆️
backend/lib/spree/backend_configuration.rb 100.00% <0.00%> (+48.57%) ⬆️
...controllers/spree/admin/option_types_controller.rb 60.00% <0.00%> (+60.00%) ⬆️
...d/app/controllers/spree/admin/taxons_controller.rb 66.66% <0.00%> (+66.66%) ⬆️
.../app/helpers/spree/admin/stock_movements_helper.rb 70.58% <0.00%> (+70.58%) ⬆️
...s/spree/admin/promotion_code_batches_controller.rb 75.00% <0.00%> (+75.00%) ⬆️
...ollers/spree/admin/promotion_actions_controller.rb 81.25% <0.00%> (+81.25%) ⬆️
...d/app/controllers/spree/admin/stores_controller.rb 83.33% <0.00%> (+83.33%) ⬆️
... and 54 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@RyanofWoods
Copy link
Contributor Author

I don't see how my change is going to decrease coverage by 15%? 😅

Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @RyanofWoods! I left a question about the test, but it looks good otherwise. I rerun the build, and the issue about the coverage is gone 🙂

@RyanofWoods RyanofWoods force-pushed the fix/add-wallet-payments-sources-changing-reused-default-payment-source branch from b429598 to 7693f12 Compare November 1, 2022 07:04
@RyanofWoods RyanofWoods force-pushed the fix/add-wallet-payments-sources-changing-reused-default-payment-source branch from 7693f12 to db4c217 Compare November 3, 2022 10:36
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a typo in the test context. Also, there's a hanging discussion with @waiting-for-dev, is that solved?

@RyanofWoods RyanofWoods force-pushed the fix/add-wallet-payments-sources-changing-reused-default-payment-source branch from db4c217 to 3e83de5 Compare December 21, 2022 13:07
@RyanofWoods RyanofWoods requested a review from a team as a code owner December 21, 2022 13:07
@RyanofWoods
Copy link
Contributor Author

Resolved both points, @waiting-for-dev and @kennyadsl, thanks!

@RyanofWoods RyanofWoods force-pushed the fix/add-wallet-payments-sources-changing-reused-default-payment-source branch 3 times, most recently from 06923e4 to eedef85 Compare January 17, 2023 07:28
@RyanofWoods RyanofWoods force-pushed the fix/add-wallet-payments-sources-changing-reused-default-payment-source branch from eedef85 to db25c25 Compare January 27, 2023 09:37
PR solidusio#2913 [1] changed how the new default WalletPaymentSource was set.
Before:
- using the last one found/created by the Order's PaymentSources

After:
- using the user's last WalletPaymentSource.

This is problematic when an order uses a default WalletPaymentSource
but it's not the most recent one.

Fixes issue solidusio#4004 [2]

Now, #make_default uses WalletPaymentSources found or created by the
order, which means it will keep the default payment source if used.

[1] solidusio#2913
[2] solidusio#4004
@RyanofWoods RyanofWoods force-pushed the fix/add-wallet-payments-sources-changing-reused-default-payment-source branch from db25c25 to 88aed7e Compare February 9, 2023 09:10
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @RyanofWoods!

@kennyadsl kennyadsl merged commit 4718a33 into solidusio:master Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_core Changes to the solidus_core gem type:bug Error, flaw or fault
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants