/**
  * MOBI-486: Add partial payment data to totals are requested with REST API.
  * MOBI-489: Add partial payment configuration to totals extension attributes.
  *
  * @param \Magento\Quote\Model\Cart\CartTotalRepository $subject
  * @param \Closure $proceed
  * @param $cartId
  * @return \Magento\Quote\Api\Data\TotalsInterface
  */
 public function aroundGet(\Magento\Quote\Model\Cart\CartTotalRepository $subject, \Closure $proceed, $cartId)
 {
     /** @var \Magento\Quote\Model\Cart\Totals $result */
     $result = $proceed($cartId);
     /* Get partial method configuration */
     $isPartialEnabled = $this->_hlpCfg->getWalletPartialEnabled();
     if ($isPartialEnabled) {
         //            $partialMaxPercent = $this->_hlpCfg->getWalletPartialPercent();
         //            /** @var \Magento\Quote\Api\Data\TotalExtensionInterface $exts */
         //            $exts = $this->_factTotalExt->create();
         //            /** @var \Praxigento\Wallet\Api\Data\Config\Payment\Method $extData */
         //            $extData = new \Praxigento\Wallet\Api\Data\Config\Payment\Method();
         //            $extData->setPartialMaxPercent($partialMaxPercent);
         //            $extData->setIsPartialEnabled($isPartialEnabled);
         //            $exts->setPraxigentoWalletPaymentConfig($extData);
         //            $result->setExtensionAttributes($exts);
         /* get partial data from repository */
         /** @var \Praxigento\Wallet\Data\Entity\Partial\Quote $found */
         $found = $this->_repoPartialQuote->getById($cartId);
         if ($found) {
             $basePartial = $found->getBasePartialAmount();
             $basePartial = $this->_hlpPriceCurrency->round($basePartial);
             /* add current partial total to segment */
             $segments = $result->getTotalSegments();
             /** @var \Magento\Quote\Api\Data\TotalSegmentInterface $seg */
             $seg = $this->_manObj->create(\Magento\Quote\Api\Data\TotalSegmentInterface::class);
             $seg->setCode(self::TOTAL_SEGMENT);
             $seg->setValue($basePartial);
             $segments[self::TOTAL_SEGMENT] = $seg;
             $result->setTotalSegments($segments);
         }
     }
     return $result;
 }
 public function getConfig()
 {
     /* Get payment method configuration */
     $isEnabled = $this->hlpCfg->getWalletActive();
     $isPartialEnabled = $this->hlpCfg->getWalletPartialEnabled();
     $partialMaxPercent = $this->hlpCfg->getWalletPartialPercent();
     /* then compose data transfer object */
     $data = new \Praxigento\Wallet\Api\Data\Config\Payment\Method();
     $data->setIsEnabled($isEnabled);
     $data->setIsPartialEnabled($isPartialEnabled);
     $data->setPartialMaxPercent($partialMaxPercent);
     /* and add configuration data to checkout config */
     $result = [self::CFG_NAME => $data->getData()];
     return $result;
 }
 public function getConfig()
 {
     /* Get payment method configuration */
     $isEnabled = $this->hlpCfg->getWalletActive();
     $isNegativeBalanceEnabled = $this->hlpCfg->getWalletNegativeBalanceEnabled();
     $isPartialEnabled = $this->hlpCfg->getWalletPartialEnabled();
     $partialMaxPercent = $this->hlpCfg->getWalletPartialPercent();
     /* ... and additional configuration for other objects */
     $customerData = $this->_populateCustomerData();
     /* then compose data transfer object */
     $data = new \Praxigento\Wallet\Api\Data\Config\Payment\Method();
     $data->setIsEnabled($isEnabled);
     $data->setIsNegativeBalanceEnabled($isNegativeBalanceEnabled);
     $data->setIsPartialEnabled($isPartialEnabled);
     $data->setPartialMaxPercent($partialMaxPercent);
     /* and add configuration data to checkout config */
     $result = ['customerData' => $customerData, self::CFG_NAME => $data->getData()];
     return $result;
 }
 public function collect(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
 {
     parent::collect($quote, $shippingAssignment, $total);
     /* get fresh grands from calculating totals */
     $grandBase = $total->getData(\Magento\Quote\Api\Data\TotalsInterface::KEY_BASE_GRAND_TOTAL);
     $grand = $total->getData(\Magento\Quote\Api\Data\TotalsInterface::KEY_GRAND_TOTAL);
     if ($grandBase == 0) {
         /* this is billing address, compose result */
         $total->setBaseTotalAmount(self::CODE, 0);
         $total->setTotalAmount(self::CODE, 0);
     } else {
         $isPartialEnabled = $this->hlpConfig->getWalletPartialEnabled();
         if ($isPartialEnabled) {
             $quoteId = $quote->getId();
             /** @var \Praxigento\Wallet\Data\Entity\Partial\Quote $partialDataSaved */
             $partialDataSaved = $this->repoPartialQuote->getById($quoteId);
             /**
              * Check quote for partial payment switcher.
              * See \Praxigento\Wallet\Observer\SalesQuotePaymentImportDataBefore
              */
             $usePartial = $quote->getData(self::ATTR_QUOTE_SWITCH_PARTIAL_PAYMENT);
             if (!is_null($usePartial)) {
                 /* there is switcher in the quote,  */
                 if ($usePartial) {
                     /* switcher is on - we need to recalculate amounts */
                     /* get max. percent to pay partially */
                     $percent = $this->hlpConfig->getWalletPartialPercent();
                     /* calculate values */
                     $partial = $this->hlpPriceCurrency->round($grand * $percent);
                     $partialBase = $this->hlpPriceCurrency->round($grandBase * $percent);
                     /* re-save partial if they are different */
                     if ($partialDataSaved) {
                         /* get saved partial totals */
                         $partialSavedBase = $partialDataSaved->getBasePartialAmount();
                         $partialSaved = $partialDataSaved->getPartialAmount();
                         if ($partialSavedBase != $partialBase || $partialSaved != $partial) {
                             /* re-save quote partial in registry */
                             $partialDataSaved->setBasePartialAmount($partialBase);
                             $partialDataSaved->setPartialAmount($partial);
                             $this->repoPartialQuote->updateById($quoteId, $partialDataSaved);
                         }
                     } else {
                         /* create new record in the registry */
                         $partialDataSaved = new \Praxigento\Wallet\Data\Entity\Partial\Quote();
                         $partialDataSaved->setQuoteRef($quoteId);
                         $partialDataSaved->setBasePartialAmount($partialBase);
                         $partialDataSaved->setPartialAmount($partial);
                         $this->repoPartialQuote->create($partialDataSaved);
                     }
                     /* reset totals in quote and compose result */
                     $quote->setData(self::CODE_BASE_TOTAL, $partialBase);
                     $quote->setData(self::CODE_TOTAL, $partial);
                     $total->setBaseTotalAmount(self::CODE, $partialBase);
                     $total->setTotalAmount(self::CODE, $partial);
                 } else {
                     /* switcher is off - clean up saved quote if exist */
                     if ($partialDataSaved) {
                         $this->repoPartialQuote->deleteById($quoteId);
                     }
                     /* reset totals in quote and compose result */
                     $quote->setData(self::CODE_BASE_TOTAL, 0);
                     $quote->setData(self::CODE_TOTAL, 0);
                     $total->setBaseTotalAmount(self::CODE, 0);
                     $total->setTotalAmount(self::CODE, 0);
                 }
             } else {
                 /* use quote saved totals if exist */
                 if ($partialDataSaved) {
                     /* there are saved data for the quote */
                     /* get max. percent to pay partially */
                     $percent = $this->hlpConfig->getWalletPartialPercent();
                     /* calculate values */
                     $partialBase = $this->hlpPriceCurrency->round($grandBase * $percent);
                     $partial = $this->hlpPriceCurrency->round($grand * $percent);
                     /* get saved partial totals */
                     $partialSavedBase = $partialDataSaved->getBasePartialAmount();
                     $partialSaved = $partialDataSaved->getPartialAmount();
                     if ($partialSavedBase != $partialBase || $partialSaved != $partial) {
                         /* re-save quote partial in registry */
                         $partialDataSaved->setBasePartialAmount($partialBase);
                         $partialDataSaved->setPartialAmount($partial);
                         $this->repoPartialQuote->updateById($quoteId, $partialDataSaved);
                     }
                     /* reset totals in quote and compose result */
                     $quote->setData(self::CODE_BASE_TOTAL, $partialBase);
                     $quote->setData(self::CODE_TOTAL, $partial);
                     $total->setBaseTotalAmount(self::CODE, $partialBase);
                     $total->setTotalAmount(self::CODE, $partial);
                 } else {
                     /* partial payment does not used */
                     /* reset totals in quote and compose result */
                     $quote->setData(self::CODE_BASE_TOTAL, 0);
                     $quote->setData(self::CODE_TOTAL, 0);
                     $total->setBaseTotalAmount(self::CODE, 0);
                     $total->setTotalAmount(self::CODE, 0);
                 }
             }
         }
     }
     return $this;
 }