Capture amount on order state change I would like to know if it is possible to capture payments by subscribing to order transition events.

Example:


Transaction mode: Authorize only
On order placed - Capture some amount (application fee)
On order validated - Capture outstanding amount


Thank you!
I've managed to create new payment and modify authorisation amount. Here is what I have so far:

public static function getSubscribedEvents() {
  $events = ['commerce_order.place.post_transition' =&gt; ['captureFee', -50]];
  return $events;
}

public function captureFee(WorkflowTransitionEvent $event) {
  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $event-&gt;getEntity();

  if ($order-&gt;bundle() != 'training') {
    return;
  }

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
  $payment_gateway = $order-&gt;payment_gateway-&gt;entity;
  $payment_gateway_plugin = $payment_gateway-&gt;getPlugin();

  $payment_storage = $this-&gt;entityTypeManager-&gt;getStorage('commerce_payment');

  // Load first order payment
  $payments = $payment_storage-&gt;loadMultipleByOrder($order);
  $first_payment = current($payments);

  // Only proceed if payment is not captured
  if ($first_payment-&gt;isCompleted()) {
    return;
  }

  // Default fee amount
  $fee_amount = new \Drupal\commerce_price\Price('50.00', 'USD');

  // Get fee amount from product variation
  if (isset($order-&gt;getItems()[0]-&gt;getPurchasedEntity()-&gt;field_app_fee) &amp;&amp; !$order-&gt;getItems()[0]-&gt;getPurchasedEntity()-&gt;field_app_fee-&gt;isEmpty()) {
    $fee_amount = $order-&gt;getItems()[0]-&gt;getPurchasedEntity()-&gt;field_app_fee-&gt;first()-&gt;toPrice();
  }

  // Create fee payment
  /** @var \Drupal\commerce_payment\Entity\PaymentInterface $payment */
  $payment = $payment_storage-&gt;create([
    'state' =&gt; 'new',
    'amount' =&gt; $fee_amount,
    'payment_gateway' =&gt; $payment_gateway-&gt;id(),
    'order_id' =&gt; $order-&gt;id(),
  ]);

  // Capture fee payment
  if ($payment_gateway_plugin instanceof OnsitePaymentGatewayInterface) {
    $payment-&gt;payment_method = $order-&gt;payment_method-&gt;entity;
    $payment_gateway_plugin-&gt;createPayment($payment, 'capture');
  }
  elseif ($payment_gateway_plugin instanceof ManualPaymentGatewayInterface) {
    $payment_gateway_plugin-&gt;createPayment($payment, TRUE);
  }
  else {
    return;
  }

  // Subtract captured amount from authorized amount and save first payment
  $first_payment_amount = $first_payment-&gt;getAmount();
  $first_payment-&gt;setAmount($first_payment_amount-&gt;subtract($fee_amount));
  $first_payment-&gt;save();

}


I have "training" bundle that is limited to 1 item per cart. This is the result with variation price set to $2,100.00 and $45.00 fee.