Spun off from #2934647: The order can stay unplaced after an offsite payment.

The problem is that the "order paid" event is fired too late in the preSave process for us to be able to modify the order state. That causes only the post_transition events to fire.

An entity should not be modified after its field hooks have already fired. This can lead to critical issues.
Currently both the order paid event and the order refresh event fire after the field hooks have already fired.

The preSave process needs to go like this:
1. Default properties
2. Order refresh
3. Recalculate total
4. Order paid in full
5. Presave hooks

We let Order::preSave() handle the default properties. We keep 2-4 in the storage cause it's cleaner (DI instead of \Drupal in the already-too-huge entity class).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bojanz created an issue. See original summary.

bojanz’s picture

Status: Active » Needs review
FileSize
5.85 KB

Let's see what the tests say.

Status: Needs review » Needs work

The last submitted patch, 2: 3008959-2.patch, failed testing. View results

zaporylie’s picture

It seems like test fails for newly-created-but-already-paid order. $this->isPaid() returns NULL, because recalculateTotalPrice() have never been triggered on the order (it happens too late - in OrderStorage::doPreSave). For that very reason OrderEvents::ORDER_PAID event was never dispatched which made the test fail.

bojanz’s picture

Title: The order state can't be modified from an OrderPaid event » Rework the order pre-save logic
Issue summary: View changes

Updating issue summary. The problem is larger than I initially thought.

The test fail from above could have been worked around by recalculating the order total before the event is fired, but that still leaves two fundamental problems:
1) The order refresh can change the order total, making the paid check unprecise
2) The order refresh process also happens after the field hooks have been invoked, which is potentially very problematic

bojanz’s picture

Status: Needs work » Needs review
FileSize
5.09 KB

  • bojanz committed a17ed52 on 8.x-2.x
    Issue #3008959 by bojanz: Rework the order pre-save logic
    
bojanz’s picture

Status: Needs review » Fixed

Committed.

agoradesign’s picture

yessssssss :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.