Magento 2 – Unable to Set Payment Transaction ID After Successful Payment Processing

magento-2.1magento-2.1.3magento2paymentPHP

I am unable to set payment transaction id after successful payment processing…

I have got payment confirmation transaction details by payment gateway on my controller action. Now I need to save the payment transaction id in \Magento\Payment\Model\InfoInterface $payment
by using setTransactionId method of above interface.

But unable to use the \Magento\Payment\Model\InfoInterface

Using code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$payment = $objectManager->get('\Magento\Payment\Model\InfoInterface');
$payment->setTransactionId(htmlentities($transaction_result->id))
                ->setIsTransactionClosed(0);

Getting error as: Cannot instantiate interface Magento\Payment\Model\InfoInterface

Please suggest way to get payment instance for save/update order transaction in custom method of model.

I am in return url page of payment

As I have redirected payment gateways after payment done or failed at payment on a controller action.

On the url's at controller, I need to get current order/payment and then update truncation id to that order/payment object.

I have just tried below solution:

protected $_checkoutSession;
public function __construct(
   ...
    \Magento\Checkout\Model\Session $checkoutSession,
    .....
) {
    $this->_checkoutSession = $checkoutSession;
}

 public function execute(){
    $order = $this->_checkoutSession->getLastRealOrder();
    $payment = $order->getPayment();
    $payment->setTransactionId(htmlentities($transaction_result->id));
    $payment->setIsTransactionClosed(0)
            ->setTransactionAdditionalInfo(
                    "some text",
                    htmlentities($transaction_result->id)
                );    
}

But $this->_checkoutSession comes with empty array…
It means after coming back from payment gateway session get flushed… but how and why.. then how to get order object?

I have got order Increment id… Now how to get order object with order increment id?

I have also used below links:

Magento 2.1.1 How to load Order with Increment ID using OrderRepository object

https://github.com/magento/magento2/issues/3534#issuecomment-227413082

for get order object by order increment id but don't found order object yet for get payment object.

Update:

When i am coming back from payment gateway as redirect url on my module controller execute function i am not getting object manager or session object or order object… I dont know much about dependency injection… please elaborate if i have to create and put any code in the di file in etc folder…

below code are not working in my module controller execute function:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$payment = $objectManager->get('\Magento\Payment\Model\InfoInterface');

It return null/empty

And if I am using below code:

protected $_checkoutSession;
public function __construct(
   ...
    \Magento\Checkout\Model\Session $checkoutSession,
    .....
) {
    $this->_checkoutSession = $checkoutSession;
}

 public function execute(){
    $order = $this->_checkoutSession->getLastRealOrder();
    $payment = $order->getPayment();
    $payment->setTransactionId(htmlentities($transaction_result->id));
    $payment->setIsTransactionClosed(0)
            ->setTransactionAdditionalInfo(
                    "some text",
                    htmlentities($transaction_result->id)
                );    
}

then this also not working…

Please help to resolve the issue…

Best Answer

I was unable to save/update payment gateway transaction id in my order while coming back from payment gateway after successful payment but was getting in return url controller action..

I have solved my problem by adding another method in payment model and save transaction id with respect to order id in db and get that in capture method of payment model....

As I was unable to create/get object manger and session data variable in my controller and was unable to get transaction id in capture function of payment module which is already extending \Magento\Payment\Model\Method\Cc because of use of curl.

Transaction id is coming on redirect url controller action and then we are coming after curl excution line of code where i was needed to save transaction id in the order and payment.

As I am using CURL method in my payment capture function for call payment gateway and passing order id and order detail to payment gateway with a return url. Payment gateway successfully coming back on the return url controller action. So I call my payment model new function captureNew and pass transaction detail as a argument. Then go in payment module captureNew function and save transaction id with order id in a database custom table and after the execution of captureNew function...

Then it return back in capture method under the curl execution line and there I get the transaction id by the order id from db and save in the order and payment object by below code

$payment->setParentTransactionId(null);
$payment->setTransactionId($order_transactionId)->setIsTransactionClosed(0);
$order->save();
Related Topic