I'm trying to figure out why my order balance isn't directly updated after I've created a payment. I'm debugging it with xDebug. This is my OnNotify function

$this->order = Order::load($orderId);
//isPaid = false
  if ($this->order !== NULL) {
    //create the payment, assign it to the order
    $payment = $this->createPayment($this->order, $mt);
    if ($this->order->isPaid()) { //isPaid still false instead of true

This is how the payment is created

  protected function createPayment(OrderInterface $order, $mt){
    $payment_storage = $this->entityTypeManager->getStorage('commerce_payment');
    /** @var \Drupal\commerce_payment\Entity\Payment $payment */
    $payments = $payment_storage->loadByProperties(['order_id' => $this->order->id()]);
    $price = new Price(((int)$mt)/100, 'EUR');
    if (count($payments) == 0) {
      $payment = $payment_storage->create([
        'state' => 'completed',
        'amount' => $price,
        'payment_gateway' => $this->entityId,
        'order_id' => $this->order->id(),
        'remote_id' => null,
        'remote_state' => null,
        'completed' => time()
      return $payment;

So if i put breakpoint, the isPaid is always false. However, if I don't use the debugger, the order balance works correctly (without the process beeing executed twice or anything...). Any ideas ?

1 Answer 1


The Payment entity's post save function explains it:

   * {@inheritdoc}
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);

    $order = $this->getOrder();
    if ($order && $this->isCompleted()) {
      $payment_order_updater = \Drupal::service('commerce_payment.order_updater');

When a payment is saved, a request to the related order is merely requested, not initiated. At the end of the page request, when PHP is destroying the PaymentOrderUpdater service, that is when the updates to the relevant orders are finally processed. This is to avoid any kind of "save within a save" scenario.

  • Thanks for your explanations.
    – lobafox
    Aug 9, 2022 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.