2

I'm creating a custom payment module in magento2 and create success controller, I want to update order status and transaction. My code as below :

// create invoice : this is working
$invoiceService = $this->_objectManager->get('Magento\Sales\Model\Service\InvoiceService');
$invoice = $invoiceService->prepareInvoice($order);
$invoice->register();
$invoice->save();

// update order status : this is working
$order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, true);
$order->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
$order->addStatusToHistory($order->getStatus(), 'Tseting : Order processed successfully with reference '. $tracking_id);
$order->save();

// make transaction : this is not working           
$payment = $order->getPayment();
$payment->setLastTransId($tracking_id);
$payment->setTransactionId($tracking_id);
$formatedPrice = $order->getBaseCurrency()->formatTxt(
    $order->getGrandTotal()
);

$trans = $this->_objectManager->get('Magento\Sales\Model\Order\Payment\Transaction\Builder');

$message = __('The authorized amount is %1.', $formatedPrice);
$transaction = $trans->setPayment($payment)
->setOrder($order)
->setTransactionId($tracking_id)
->setFailSafe(true)
->build(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_CAPTURE);

$payment->addTransactionCommentsToOrder(
    $transaction,
    $message
);
$payment->setParentTransactionId(null);
$payment->save();

sales_payment_transaction table not updating from above code.

How to complete my transaction after payment success?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.