Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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?
Comment | File | Size | Author |
---|---|---|---|
#5 | 2990762_get_balance_exception_on_unsaved_payment_7.patch | 818 bytes | mitrpaka |
|
Comments
Comment #2
bojanz CreditAttribution: bojanz at Centarro commentedGood idea!
Comment #3
mitrpaka CreditAttribution: mitrpaka as a volunteer commentedComment #5
mitrpaka CreditAttribution: mitrpaka as a volunteer commentedUpdated patch file. The refunded amount initialized in
::postCreate()
only if not set (null) and the payment amount exists.Comment #6
mitrpaka CreditAttribution: mitrpaka as a volunteer commentedComment #7
zaporylieLGTM. 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 😉
Comment #8
bojanz CreditAttribution: bojanz at Centarro commentedSince 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.
Comment #10
bojanz CreditAttribution: bojanz at Centarro commentedTweaked the fix as described, added tests. Thanks!