1

I have extended QuoteManagement.php file for split order. It's working fine, but I need payment details like credit card details to process the order. But I am not able to get payment details in file below are my code snap.

$quote111 = $checkoutSession->getQuote();  // or load by id
            $payment1 = $quote111->getPayment();
            $instance1 = $payment1->getMethodInstance();
            $ccNumber = $instance1->getInfoInstance()->getCcNumber();
            $ccMethod = $instance1->getInfoInstance()->getMethod();
            $ccType = $instance1->getInfoInstance()->getCcType();
            $ccExpYear = $instance1->getInfoInstance()->getCcExpYear();
            $ccExpMonth = $instance1->getInfoInstance()->getCcExpMonth();

I have also tried below code but no luck.

if ($paymentMethod) {
                $quoteSplit->getPayment()->setQuote($quoteSplit);
                $data = $paymentMethod->getData();
                $quoteSplit->getPayment()->importData($data);
            }

Below are my full code

$quoteRepository = $objectManager->get('\Magento\Quote\Api\CartRepositoryInterface');
        $quoteFactory = $objectManager->get('\Magento\Quote\Model\QuoteFactory');
        $eventManager = $objectManager->get('\Magento\Framework\Event\ManagerInterface');
        $checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session');
        if (!($helper)) {
            return $result = $proceed($cartId, $paymentMethod);
        }
        $quote = $quoteRepository->getActive($cartId);
        $paymentMethodString = $quote->getPayment()->getMethod();
        //  echo "<pre>"; print_r($quote->getData());

        /**/
        $checkoutMethod = $quote->getCheckoutMethod();
        $paymentMethod1 = $quote->getPayment()->getMethod();
        // $shippingAddress1 = $quote->getShippingAddress()->getData();
        //  $billingAddress1 = $quote->getBillingAddress()->getData();
        //   $customer = $this->checkCustomer($billingAddress1);
        $currency = $quote->getCurrency();


        /**/
        // get data from addresses and remove ids
        $billingAddress = $quote->getBillingAddress()->getData();
        $shippingAddress = $quote->getShippingAddress()->getData();
        unset($billingAddress['id']);
        unset($billingAddress['quote_id']);
        unset($shippingAddress['id']);
        unset($shippingAddress['quote_id']);

        $temparray = [];
        foreach ($quote->getAllVisibleItems() as $item) {
            $temparray[$item->getProduct()->getSku()][] = $item;
        }

        foreach ($temparray as $key => $items) {
            // init Quote Split
            $quoteSplit = $quoteFactory->create();
            $quoteSplit->setStoreId($quote->getStoreId());
            $quoteSplit->setCustomer($quote->getCustomer());
            $quoteSplit->setCustomerIsGuest($quote->getCustomerIsGuest());

            if ($quote->getCheckoutMethod() === self::METHOD_GUEST) {
                $quoteSplit->setCustomerId(null);
                $quoteSplit->setCustomerEmail($quote->getBillingAddress()->getEmail());
                $quoteSplit->setCustomerIsGuest(true);
                $quoteSplit->setCustomerGroupId(\Magento\Customer\Api\Data\GroupInterface::NOT_LOGGED_IN_ID);
            }

            // save quoteSplit in order to have a quote id for item
            $quoteRepository->save($quoteSplit);

            foreach ($items as $item) {
                // add item
                $item->setId(null);
                $quoteSplit->addItem($item);
            }
            $tax = $item->getData('tax_amount');
            $rowTotal = $item->getData('row_total');
            $rowTotalIncTax = $item->getData('base_row_total_incl_tax');
            $discountAmount = $item->getData('discount_amount');
            $itemPrice = $item->getPrice();
            $itemQty = $item->getQty();
            $finalPrice = $itemPrice * $itemQty;
            // set addresses
            $quoteSplit->getBillingAddress()->setData($billingAddress);
            $quoteSplit->getShippingAddress()->setData($shippingAddress);
            $shippingAmount = $quoteSplit->getShippingAddress()->getShippingAmount();
            // recollect totals into the quote
            foreach ($quoteSplit->getAllAddresses() as $address) {
                $address->setBaseSubtotal($finalPrice);
                $address->setSubtotal($finalPrice);
                $address->setDiscountAmount($discountAmount);
                $address->setTaxAmount($tax);
                $address->setBaseTaxAmount($tax);
                $address->setBaseGrandTotal($finalPrice + $shippingAmount + $tax - $discountAmount);
                $address->setGrandTotal($finalPrice + $shippingAmount + $tax - $discountAmount);
            }

            $checkoutSession->replaceQuote($quoteSplit);
            $quoteSplit->setInventoryProcessed(false);
            $quoteSplit->setCurrency($currency);
            $quoteSplit->setCheckoutMethod($checkoutMethod);
            $quoteSplit->setPaymentMethod($paymentMethod1);
            $quoteSplit->setSendCconfirmation(1);


            $quote111 = $checkoutSession->getQuote();  // or load by id
            $payment1 = $quote111->getPayment();
            $instance1 = $payment1->getMethodInstance();
            $ccNumber = $instance1->getInfoInstance()->getCcNumber();
            $ccMethod = $instance1->getInfoInstance()->getMethod();
            $ccType = $instance1->getInfoInstance()->getCcType();
            $ccExpYear = $instance1->getInfoInstance()->getCcExpYear();
            $ccExpMonth = $instance1->getInfoInstance()->getCcExpMonth();

            $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test1.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            $logger->info($instance1->getInfoInstance());

            return "Hello";
            $quoteSplit->getPayment()->importData(array(
                'method' => $paymentMethod1,
                'cc_type' => 'VI',
                'cc_number' => '4111111111111111',
                'cc_exp_year' => '20',
                'cc_exp_month' => '12',
                'cc_cid' => '123'
            ));        

            $quoteSplit->setIsActive(1);

I have tried with static value and it's working fine like below example

$quoteSplit->getPayment()->importData(array(
    'method' => $paymentMethod1,
    'cc_type' => 'VI',
    'cc_number' => '4111111111111111',
    'cc_exp_year' => '20',
    'cc_exp_month' => '12',
    'cc_cid' => '123'
));

I want to get credit card details in this file and use in creating order.

1

I have solved by below code snap not sure that this the correct way or not.

$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;
                            }
                        }
                    }
                }

                $quoteSplit->getPayment()->importData(
                    array(
                        'method' => $paymentMethod1,
                        'cc_type' => $paymentCardDetails['cc_type'],
                        'cc_number' => $paymentCardDetails['cc_number'],
                        'cc_exp_year' => $paymentCardDetails['cc_exp_year'],
                        'cc_exp_month' => $paymentCardDetails['cc_exp_month'],
                        'cc_cid' => $paymentCardDetails['cc_cid']
                    )
                );

I tried lots but don't find any other way. Correct me if I am doing wrong.

Your Answer

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

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