1

I'm trying to create an order programmatically using "paypal_express" method in Magento 2.1.x. The entire checkout process is done via custom module and paypal payment via API. All the transactions and process has been done but unable to create order.

Getting the below Error:

1 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): PayPal gateway has rejected request. Invalid token (#10410: Invalid token).

Exception #0 (Magento\Framework\Exception\LocalizedException): PayPal gateway has rejected request. Invalid token (#10410: Invalid token).
#0 /var/www/html/shop/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(89): Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject('Magento\\Framewo...', Array)

The order can successfully created with 'checkmo' method.

Code Sample:

$shippingAddress->setCollectShippingRates(true)
        ->collectShippingRates()
        ->setShippingMethod('freeshipping_freeshipping'); //shipping method
    $cart->getShippingAddress()->addShippingRate($this->shippingRate);
    $cart->setPaymentMethod('paypal_express'); //checkmo
    //@todo insert a variable to affect the invetory
    $cart->setInventoryProcessed(false);
    // Set sales order payment
    $cart->getPayment()->importData(['method' => 'paypal_express']); //checkmo

Anyone please help me to resolve this issue.

1 Answer 1

2

I got solution for the issue. Sharing the same, it may helpful for those who are looking for.

$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);
1
  • I tried above code for placing the order but still getting the same error, PayPal gateway has rejected request. This Express Checkout session has expired. Token value is no longer valid Commented Nov 28, 2018 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.