How to change payment state programmatically?
Try this:

use Drupal\commerce_payment\Entity\Payment;

$payments = \Drupal::entityQuery('commerce_payment')
  -&gt;condition('order_id', $order-&gt;id())
  -&gt;execute();

foreach ($payments as $value) {
  $payment = Payment::load($value);
  $payment-&gt;setState('completed')-&gt;save();
}