-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Fix AddPaymentSourcesToWallet changing default when reused #4198
Conversation
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,
|
Thank you for raising this to my attention. Thinking about it more, I think you are correct. 👍 I think perhaps it would be more appropriate for |
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. |
There was a problem hiding this 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.)
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. |
There was a problem hiding this 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.
There was a problem hiding this 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. 🙏
core/spec/models/spree/wallet/add_payment_sources_to_wallet_spec.rb
Outdated
Show resolved
Hide resolved
838f64a
to
b429598
Compare
Codecov Report
@@ 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
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
I don't see how my change is going to decrease coverage by 15%? 😅 |
There was a problem hiding this 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 🙂
core/spec/models/spree/wallet/add_payment_sources_to_wallet_spec.rb
Outdated
Show resolved
Hide resolved
b429598
to
7693f12
Compare
7693f12
to
db4c217
Compare
There was a problem hiding this 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?
core/spec/models/spree/wallet/add_payment_sources_to_wallet_spec.rb
Outdated
Show resolved
Hide resolved
db4c217
to
3e83de5
Compare
Resolved both points, @waiting-for-dev and @kennyadsl, thanks! |
06923e4
to
eedef85
Compare
eedef85
to
db25c25
Compare
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
db25c25
to
88aed7e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @RyanofWoods!
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
Checklist: