cancel
Showing results for 
Search instead for 
Did you mean: 

Order split with online transaction on checkout in magento 2.4 enterprise

SOLVED

Order split with online transaction on checkout in magento 2.4 enterprise

I'm facing one issue while splitting the order on checkout. I followed these code mentioned in the link:- https://magento.stackexchange.com/questions/196669/magento-2-split-order-for-every-item and https://github.com/magestat/magento2-split-order

Both solution is working with offline payment like check/mo, Cash on delivery, po number etc. But its not working with credit card details. Always getting error regarding credit card details.

I'm putting some more information through code:-

I am stuck at a point to distribute order and assign payment method into it. there are two scenario i'm getting:

  1. if i assign payment method checkmo,Cash on delivery then order is splitted and everything is working fine with this.
  2. But i need to order products using credit card and when i assign payment method code(credit card payment method is 'nmi_directpost') and also assign card details into quote and placed and order then its showing me error differently, Some time its shows credit card details is not valid, sometime page is redirected to cart page without any log/exception. Here is bunch of code i'm trying to do:-
public function aroundPlaceOrder(QuoteManagement $subject, callable $proceed, $cartId, $payment = null)
    {
        $currentQuote = $this->quoteRepository->getActive($cartId);

        // Separate all items in quote into new quotes.
        $quotes = $this->quoteHandler->normalizeQuotes($currentQuote);
        if (empty($quotes)) {
            return $result = array_values([($proceed($cartId, $payment))]);
        }
        // Collect list of data addresses.
        $addresses = $this->quoteHandler->collectAddressesData($currentQuote);

        /** @var \Magento\Sales\Api\Data\OrderInterface[] $orders */
        $orders = [];
        $orderIds = [];
        foreach ($quotes as $items) {
            /** @var \Magento\Quote\Model\Quote $split */
            $split = $this->quoteFactory->create();

            // Set all customer definition data.
            $this->quoteHandler->setCustomerData($currentQuote, $split);
            $this->toSaveQuote($split);

            // Map quote items.
            foreach ($items as $item) {
                // Add item by item.
                $item->setId(null);
                $split->addItem($item);
            }
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote 1st :-'. print_r($split->getData(),true));
            $this->quoteHandler->populateQuote($quotes, $split, $items, $addresses, $payment);
            // $split->getPayment()->setMethod('nmi_directpost');
            // if ($payment) {
            //     $split->getPayment()->setQuote($split);
                // $data = $payment->getData();

                // $paymentDetails = $paymentCardDetails = '';
                // $postData = file_get_contents("php://input");//Get all param
                // $postData = (array)json_decode($postData);//Decode all json param
                // foreach ($postData as $key => $value) {
                //     if ($key == 'paymentMethod') { //Get paymentMethod details
                //         $paymentDetails = (array)$value;
                //         foreach ($paymentDetails as $key1 => $paymentValue) {
                //             if ($key1 == 'additional_data') { //get  paymentMethod Details like card details
                //                 $paymentCardDetails = (array)$paymentValue;
                //             }
                //         }
                //     }
                // }
                // $split->setMethod('checkmo');

                \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('Paynet :-');
                // $payment = $quotes->getPayment();
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

$quote = $cart->getQuote();
$paymentMethod = $quote->getPayment()->getMethod();
                $payment = $this->checkoutSession->getQuote()->getData();
                \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('second Paynet :-');
                \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote :-'. print_r($paymentMethod,true));
                // $split->setPayment($payment);
                // $split->getPayment()->importData(array(
                //     'method'      =>'nmi_directpost',
                //     'cc_type'     =>'VI',
                //     'cc_number'   =>'4111111111111111',
                //     'cc_exp_year' =>'2025',
                //     'cc_exp_month'=>'10',
                //   ));
            // }
            // \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('original quote :-'. print_r($quotes->getData(),true));
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote :-'. print_r($split->getData(),true));
            // \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote :-'. print_r($payment->getData(),true));
            // Dispatch event as Magento standard once per each quote split.
            $this->eventManager->dispatch(
                'checkout_submit_before',
                ['quote' => $split]
            );

            $this->toSaveQuote($split);
            $order = $subject->submit($split);

            $orders[] = $order;
            $orderIds[$order->getId()] = $order->getIncrementId();

            if (null == $order) {
                throw new LocalizedException(__('Please try to place the order again.'));
            }
        }
        $currentQuote->setIsActive(false);
        $this->toSaveQuote($currentQuote);

        $this->quoteHandler->defineSessions($split, $order, $orderIds);

        $this->eventManager->dispatch(
            'checkout_submit_all_after',
            ['orders' => $orders, 'quote' => $currentQuote]
        );
        return $this->getOrderKeys($orderIds);
    }

If anyone have solution regarding this then please share you solution/Opinion? I'll be really thankful for this.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Order split with online transaction on checkout in magento 2.4 enterprise


@sushant693 wrote:
I'm facing one issue while splitting the order on checkout kbc head office. I followed these code mentioned in the link:- https://magento.stackexchange.com/questions/196669/magento-2-split-order-for-every-item and https://github.com/magestat/magento2-split-order

Both solution is working with offline payment like check/mo, Cash on delivery, po number etc. But its not working with credit card details. Always getting error regarding credit card details.

I'm putting some more information through code:-

I am stuck at a point to distribute order and assign payment method into it. there are two scenario i'm getting:

  1. if i assign payment method checkmo,Cash on delivery then order is splitted and everything is working fine with this.
  2. But i need to order products using credit card and when i assign payment method code(credit card payment method is 'nmi_directpost') and also assign card details into quote and placed and order then its showing me error differently, Some time its shows credit card details is not valid, sometime page is redirected to cart page without any log/exception. Here is bunch of code i'm trying to do:-
public function aroundPlaceOrder(QuoteManagement $subject, callable $proceed, $cartId, $payment = null)
    {
        $currentQuote = $this->quoteRepository->getActive($cartId);

        // Separate all items in quote into new quotes.
        $quotes = $this->quoteHandler->normalizeQuotes($currentQuote);
        if (empty($quotes)) {
            return $result = array_values([($proceed($cartId, $payment))]);
        }
        // Collect list of data addresses.
        $addresses = $this->quoteHandler->collectAddressesData($currentQuote);

        /** @var \Magento\Sales\Api\Data\OrderInterface[] $orders */
        $orders = [];
        $orderIds = [];
        foreach ($quotes as $items) {
            /** @var \Magento\Quote\Model\Quote $split */
            $split = $this->quoteFactory->create();

            // Set all customer definition data.
            $this->quoteHandler->setCustomerData($currentQuote, $split);
            $this->toSaveQuote($split);

            // Map quote items.
            foreach ($items as $item) {
                // Add item by item.
                $item->setId(null);
                $split->addItem($item);
            }
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote 1st :-'. print_r($split->getData(),true));
            $this->quoteHandler->populateQuote($quotes, $split, $items, $addresses, $payment);
            // $split->getPayment()->setMethod('nmi_directpost');
            // if ($payment) {
            //     $split->getPayment()->setQuote($split);
                // $data = $payment->getData();

                // $paymentDetails = $paymentCardDetails = '';
                // $postData = file_get_contents("php://input");//Get all param
                // $postData = (array)json_decode($postData);//Decode all json param
                // foreach ($postData as $key => $value) {
                //     if ($key == 'paymentMethod') { //Get paymentMethod details
                //         $paymentDetails = (array)$value;
                //         foreach ($paymentDetails as $key1 => $paymentValue) {
                //             if ($key1 == 'additional_data') { //get  paymentMethod Details like card details
                //                 $paymentCardDetails = (array)$paymentValue;
                //             }
                //         }
                //     }
                // }
                // $split->setMethod('checkmo');

                \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('Paynet :-');
                // $payment = $quotes->getPayment();
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

$quote = $cart->getQuote();
$paymentMethod = $quote->getPayment()->getMethod();
                $payment = $this->checkoutSession->getQuote()->getData();
                \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('second Paynet :-');
                \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote :-'. print_r($paymentMethod,true));
                // $split->setPayment($payment);
                // $split->getPayment()->importData(array(
                //     'method'      =>'nmi_directpost',
                //     'cc_type'     =>'VI',
                //     'cc_number'   =>'4111111111111111',
                //     'cc_exp_year' =>'2025',
                //     'cc_exp_month'=>'10',
                //   ));
            // }
            // \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('original quote :-'. print_r($quotes->getData(),true));
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote :-'. print_r($split->getData(),true));
            // \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote :-'. print_r($payment->getData(),true));
            // Dispatch event as Magento standard once per each quote split.
            $this->eventManager->dispatch(
                'checkout_submit_before',
                ['quote' => $split]
            );

            $this->toSaveQuote($split);
            $order = $subject->submit($split);

            $orders[] = $order;
            $orderIds[$order->getId()] = $order->getIncrementId();

            if (null == $order) {
                throw new LocalizedException(__('Please try to place the order again.'));
            }
        }
        $currentQuote->setIsActive(false);
        $this->toSaveQuote($currentQuote);

        $this->quoteHandler->defineSessions($split, $order, $orderIds);

        $this->eventManager->dispatch(
            'checkout_submit_all_after',
            ['orders' => $orders, 'quote' => $currentQuote]
        );
        return $this->getOrderKeys($orderIds);
    }

If anyone have solution regarding this then please share you solution/Opinion? I'll be really thankful for this.


Hi, thanks for sharing actually i am also looking facing the same problem, But could you please suggest me the best solution please? If you have found it otherwise i am following your thread to check the latest update. 

 

thanks

View solution in original post

1 REPLY 1

Re: Order split with online transaction on checkout in magento 2.4 enterprise


@sushant693 wrote:
I'm facing one issue while splitting the order on checkout kbc head office. I followed these code mentioned in the link:- https://magento.stackexchange.com/questions/196669/magento-2-split-order-for-every-item and https://github.com/magestat/magento2-split-order

Both solution is working with offline payment like check/mo, Cash on delivery, po number etc. But its not working with credit card details. Always getting error regarding credit card details.

I'm putting some more information through code:-

I am stuck at a point to distribute order and assign payment method into it. there are two scenario i'm getting:

  1. if i assign payment method checkmo,Cash on delivery then order is splitted and everything is working fine with this.
  2. But i need to order products using credit card and when i assign payment method code(credit card payment method is 'nmi_directpost') and also assign card details into quote and placed and order then its showing me error differently, Some time its shows credit card details is not valid, sometime page is redirected to cart page without any log/exception. Here is bunch of code i'm trying to do:-
public function aroundPlaceOrder(QuoteManagement $subject, callable $proceed, $cartId, $payment = null)
    {
        $currentQuote = $this->quoteRepository->getActive($cartId);

        // Separate all items in quote into new quotes.
        $quotes = $this->quoteHandler->normalizeQuotes($currentQuote);
        if (empty($quotes)) {
            return $result = array_values([($proceed($cartId, $payment))]);
        }
        // Collect list of data addresses.
        $addresses = $this->quoteHandler->collectAddressesData($currentQuote);

        /** @var \Magento\Sales\Api\Data\OrderInterface[] $orders */
        $orders = [];
        $orderIds = [];
        foreach ($quotes as $items) {
            /** @var \Magento\Quote\Model\Quote $split */
            $split = $this->quoteFactory->create();

            // Set all customer definition data.
            $this->quoteHandler->setCustomerData($currentQuote, $split);
            $this->toSaveQuote($split);

            // Map quote items.
            foreach ($items as $item) {
                // Add item by item.
                $item->setId(null);
                $split->addItem($item);
            }
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote 1st :-'. print_r($split->getData(),true));
            $this->quoteHandler->populateQuote($quotes, $split, $items, $addresses, $payment);
            // $split->getPayment()->setMethod('nmi_directpost');
            // if ($payment) {
            //     $split->getPayment()->setQuote($split);
                // $data = $payment->getData();

                // $paymentDetails = $paymentCardDetails = '';
                // $postData = file_get_contents("php://input");//Get all param
                // $postData = (array)json_decode($postData);//Decode all json param
                // foreach ($postData as $key => $value) {
                //     if ($key == 'paymentMethod') { //Get paymentMethod details
                //         $paymentDetails = (array)$value;
                //         foreach ($paymentDetails as $key1 => $paymentValue) {
                //             if ($key1 == 'additional_data') { //get  paymentMethod Details like card details
                //                 $paymentCardDetails = (array)$paymentValue;
                //             }
                //         }
                //     }
                // }
                // $split->setMethod('checkmo');

                \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('Paynet :-');
                // $payment = $quotes->getPayment();
                $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

$quote = $cart->getQuote();
$paymentMethod = $quote->getPayment()->getMethod();
                $payment = $this->checkoutSession->getQuote()->getData();
                \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('second Paynet :-');
                \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote :-'. print_r($paymentMethod,true));
                // $split->setPayment($payment);
                // $split->getPayment()->importData(array(
                //     'method'      =>'nmi_directpost',
                //     'cc_type'     =>'VI',
                //     'cc_number'   =>'4111111111111111',
                //     'cc_exp_year' =>'2025',
                //     'cc_exp_month'=>'10',
                //   ));
            // }
            // \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('original quote :-'. print_r($quotes->getData(),true));
            \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote :-'. print_r($split->getData(),true));
            // \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info('new quote :-'. print_r($payment->getData(),true));
            // Dispatch event as Magento standard once per each quote split.
            $this->eventManager->dispatch(
                'checkout_submit_before',
                ['quote' => $split]
            );

            $this->toSaveQuote($split);
            $order = $subject->submit($split);

            $orders[] = $order;
            $orderIds[$order->getId()] = $order->getIncrementId();

            if (null == $order) {
                throw new LocalizedException(__('Please try to place the order again.'));
            }
        }
        $currentQuote->setIsActive(false);
        $this->toSaveQuote($currentQuote);

        $this->quoteHandler->defineSessions($split, $order, $orderIds);

        $this->eventManager->dispatch(
            'checkout_submit_all_after',
            ['orders' => $orders, 'quote' => $currentQuote]
        );
        return $this->getOrderKeys($orderIds);
    }

If anyone have solution regarding this then please share you solution/Opinion? I'll be really thankful for this.


Hi, thanks for sharing actually i am also looking facing the same problem, But could you please suggest me the best solution please? If you have found it otherwise i am following your thread to check the latest update. 

 

thanks