I am trying to capture payment online but it's not capturing. I am working with Magento 2.2. Not sure why this code does not work even the same code works with the latest version of Magento.
public function createInvoice($order, $requireCapture = false)
{
if (filter_var($requireCapture, FILTER_VALIDATE_BOOLEAN) === true) {
$captureType = Invoice::CAPTURE_ONLINE;
} else {
$captureType = Invoice::CAPTURE_OFFLINE;
}
if (!$order->getInvoiceCollection()->count()) {
$invoice = $this->invoiceService->prepareInvoice($order);
$invoice->setRequestedCaptureCase($captureType);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(false);
$invoice->getOrder()->setIsInProcess(true);
$transaction = $this->transactionFactory->create()->addObject($invoice)->addObject($invoice->getOrder());
$transaction->save();
}
}