If one forgets ::setRefundedAmount() before ::getBalance():
TypeError: Argument 1 passed to Drupal\commerce_price\Price::subtract() must be an instance of Drupal\commerce_price\Price, null given, called in /var/www/html/drupal/modules/contrib/commerce/modules/payment/src/Entity/Payment.php on line 166 in Drupal\commerce_price\Price->subtract()

::setRefundedAmount() is in fact called in ::preSave() so everything fixes once Payment is saved but perhaps we could also set it to 0 in ::postCreate() (if null) to avoid this kind of issues?

Thoughts?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

zaporylie created an issue. See original summary.

bojanz’s picture

Good idea!

mitrpaka’s picture

Status: Needs review » Needs work
mitrpaka’s picture

Updated patch file. The refunded amount initialized in ::postCreate() only if not set (null) and the payment amount exists.

mitrpaka’s picture

Status: Needs work » Needs review
zaporylie’s picture

Status: Needs review » Reviewed & tested by the community

LGTM. It's basically the same code as the corresponding part in ::preSave 👍

The impact is relatively small so probably no tests needed, despite it's been reported as a bug. That being said I'm changing the status to RTBC to bring @bojanz attention 😉

bojanz’s picture

Status: Reviewed & tested by the community » Needs work

Since getBalance is the method throwing the error, that's the one we need to fix (to deal with refunded amount being NULL).
That is more bullet proof than postCreate.

  • bojanz committed 283c30a on 8.x-2.x authored by mitrpaka
    Issue #2990762 by mitrpaka, bojanz: Payment::getBalance() on unsaved...
bojanz’s picture

Status: Needs work » Fixed

Tweaked the fix as described, added tests. Thanks!

Status: Fixed » Closed (fixed)

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