Magento 2.1 – How to Update Payment Method with Transaction Details Programmatically

magento2orderspayment-methodspaypal-express

Im trying to create an order in Magento 2 programmatically and able to do with "checkmo" method. But I need to update the payment method with paypal and its transaction details. I have the trans details which processed the payment via Paypal API.

If anyone can help me, it would be great. Thankyou.

Best Answer

$shippingAddress = $cart->getShippingAddress(); 
//@todo set in order data
$shippingAddress->setCollectShippingRates(true)->collectShippingRates()->setShippingMethod('freeshipping_freeshipping'); //shipping method
$cart->getShippingAddress()->addShippingRate($this->shippingRate);
$cart->getShippingAddress()->setPaymentMethod('paypal_express');  //checkmo
$cart->setPaymentMethod('paypal_express');
$cart->getPayment()->setIsTransactionClosed(0);
$cart->getPayment()->setAdditionalInformation($payment_trans_array);
$cart->getPayment()->importData(array('method' => 'paypal_express', 'additional_information'=>$payment_trans_array));
//$cart->getPayment()->importData(['method' => 'checkmo']);
//@todo insert a variable to affect the invetory
$cart->setInventoryProcessed(false);