Re-use Payments in Checkout - #6564
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6564 +/- ##
=======================================
Coverage 92.26% 92.27%
=======================================
Files 1037 1037
Lines 21211 21229 +18
=======================================
+ Hits 19570 19588 +18
Misses 1641 1641 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
3b53200 to
146bc7c
Compare
|
|
||
| # Payments | ||
| has_many :payments, dependent: :destroy, inverse_of: :order | ||
| has_many :payments, dependent: :destroy, inverse_of: :order, autosave: true |
There was a problem hiding this comment.
I worry about this. Feels like it could be considered a breaking change.
There was a problem hiding this comment.
Same. That's why I called it out in the pull-request description.
|
We want to update the wallet source fieldsets too, right? Saved cards won't reused payments methods the way you ahve this PR, if I understand correctly. |
146bc7c to
050aa3e
Compare
Yeah, that's correct. This change does not work for wallet sources because of how we completely override the |
|
Are you going to do that in this PR or make that separate? |
|
I'll add that to this pull-request. |
When persisting an order, we want to persist changes made to the associated payments as well if there are any. This is a breaking change from previous behaviour, but will make it easier for us to reuse existing payment records in checkout instead of creating a new one and invalidating all of the existing ones every time the customer navigates through that step. Co-authored-by: Noah Silvera <noah@super.gd>
This will allow for updating an existing payment instead of always creating new ones. Which will lead to us being able to cut down on the number of invalid payments created on orders when customers navigated through the payment step repeatedly. Co-authored-by: Noah Silvera <noah@super.gd>
Changes the behaviour of OrderUpdateAttributes to update an existing payment if an ID is passed through for that payment instead of always creating a new one. Co-authored-by: Noah Silvera <noah@super.gd>
050aa3e to
cbe4262
Compare
If we've already got a unprocessed payment record for a specific payment method, then let's just re-use it and update the source instead of creating a brand new payment record and invalidating the old one leading to a bunch of junk invalidated payment data we don't care about. Co-authored-by: Noah Silvera <noah@super.gd> Co-authored-by: Senem <senem@super.gd>
Updates Order#validate_payments_attributes to also check if a passed payment ID param matches an ID of a payment that belongs to the order. This will ensure we don't create the possibility to modify a payment that does not belong to this order. Co-authored-by: Noah Silvera <noah@super.gd>
cbe4262 to
f288281
Compare
Summary
While repeatedly going through the payment step of checkout, we would create a new payment record each time and invalidate all of the old ones. This could eventually lead to a long list of invalidated payments which is confusing for admins and provides no real value. (It also causes issues for gateways like Stripe that expect there to be a single payment intent for a customer checkout session, and we typically tie the intent to the payment record.)
Instead, we now check for an existing valid payment record and reuse it if it exists. This means that the payment step of checkout will only ever create a single unprocessed payment record for a given payment method on each order.
This change is largely backwards compatible. Existing stores will not benefit from the new change without updating their storefront code to pass the payment ID when appropriate. If the ID is omitted, there's no functional change to behaviour.
We considered updating the admin and API uses, but felt both of those were clearer leaving the existing behaviour because they involve either an explicit call to create a payment (API) or a "New Payment" button (admin).
Possibly Breaking Changes
This updates the
Order#paymentsassociation to haveautosave: trueenabled so that we can persist the payment and its source when callingsave!on the order itself.Checklist
Check out our PR guidelines for more details.
The following are mandatory for all PRs: