![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/InventorySales/Plugin/Model/Service/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Ecombricks\InventorySales\Plugin\Model\Service; /** * Payment failures service plugin */ class PaymentFailuresService { /** * Scope config * * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * Inline translation * * @var \Magento\Framework\Translate\Inline\StateInterface */ protected $inlineTranslation; /** * Transport builder * * @var \Magento\Framework\Mail\Template\TransportBuilder */ protected $transportBuilder; /** * Locale date * * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface */ protected $localeDate; /** * Cart repository * * @var \Magento\Quote\Api\CartRepositoryInterface */ protected $cartRepository; /** * Logger * * @var \Psr\Log\LoggerInterface */ protected $logger; /** * Source management * * @var \Ecombricks\Inventory\Model\SourceManagement */ protected $sourceManagement; /** * Shipping method full code * * @var \Ecombricks\InventoryQuote\Data\ShippingMethodFullCode */ protected $shippingMethodFullCode; /** * Quote * * @var \Magento\Quote\Api\Data\CartInterface */ protected $quote; /** * Constructor * * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate * @param \Magento\Quote\Api\CartRepositoryInterface $cartRepository * @param \Psr\Log\LoggerInterface $logger * @param \Ecombricks\Inventory\Model\SourceManagement $sourceManagement * @param \Ecombricks\InventoryQuote\Data\ShippingMethodFullCode $shippingMethodFullCode * @return void */ public function __construct( \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate, \Magento\Quote\Api\CartRepositoryInterface $cartRepository, \Psr\Log\LoggerInterface $logger, \Ecombricks\Inventory\Model\SourceManagement $sourceManagement, \Ecombricks\InventoryQuote\Data\ShippingMethodFullCode $shippingMethodFullCode ) { $this->scopeConfig = $scopeConfig; $this->inlineTranslation = $inlineTranslation; $this->transportBuilder = $transportBuilder; $this->localeDate = $localeDate; $this->cartRepository = $cartRepository; $this->logger = $logger; $this->sourceManagement = $sourceManagement; $this->shippingMethodFullCode = $shippingMethodFullCode; } /** * Set quote by quote ID * * @param int $quoteId * @return \Magento\Sales\Api\PaymentFailuresInterface */ protected function setQuoteByQuoteId(int $quoteId) { $this->quote = $this->cartRepository->get($quoteId); return $this; } /** * Get quote * * @return \Magento\Quote\Api\Data\CartInterface * @throws \Magento\Framework\Exception\LocalizedException */ protected function getQuote(): \Magento\Quote\Api\Data\CartInterface { if ($this->quote === null) { throw new \Magento\Framework\Exception\LocalizedException(__('Quote is not set for the payment failures service.')); } return $this->quote; } /** * Get store ID * * @return int */ protected function getStoreId(): int { return (int) $this->getQuote()->getStoreId(); } /** * Get config value * * @param string $path * @return mixed */ protected function getConfigValue(string $path) { return $this->scopeConfig->getValue($path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->getStoreId()); } /** * Get current date time formatted * * @return string */ protected function getCurrentDateTimeFormatted(): string { return $this->localeDate->formatDateTime(new \DateTime(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM); } /** * Get customer name * * @return string */ protected function getCustomerName(): string { $customerName = __('Guest')->render(); $quote = $this->getQuote(); if (!$quote->getCustomerIsGuest()) { $customer = $quote->getCustomer(); $customerName = $customer->getFirstname().' '.$customer->getLastname(); } return $customerName; } /** * Get shipping carrier description * * @return string */ protected function getShippingCarrierDescription(): string { $descriptionPieces = []; $shippingMethods = $this->getQuote()->getShippingAddress()->getShippingMethod(); foreach ($shippingMethods as $sourceCode => $shippingMethod) { $source = $this->sourceManagement->getSource($sourceCode); if (empty($source)) { continue; } $shippingCarrierCode = $this->shippingMethodFullCode->parse($shippingMethod)[0]; $descriptionPieces[] = $source->getName().': '.$this->getConfigValue('carriers/'.$shippingCarrierCode.'/title'); } return implode(', ', $descriptionPieces); } /** * Get payment method description * * @return string */ protected function getPaymentMethodDescription(): string { $paymentMethodCode = $this->getQuote()->getPayment()->getMethod() ?? ''; if (empty($paymentMethodCode)) { return ''; } return $this->getConfigValue('payment/'.$paymentMethodCode.'/title'); } /** * Get quote items * * @return array */ protected function getQuoteItems(): array { $quoteItems = []; $quote = $this->getQuote(); foreach ($quote->getAllVisibleItems() as $quoteItem) { $product = $quoteItem->getProduct(); $currencyCode = $quote->getCurrency()->getStoreCurrencyCode(); $qty = $quoteItem->getQty(); $quoteItems[] = $product->getName().' x '.$qty.' '.$currencyCode.' '.$product->getFinalPrice($qty); } return $quoteItems; } /** * Get template vars * * @param string $message * @param string $checkoutType * @return array */ protected function getTemplateVars(string $message, string $checkoutType): array { $quote = $this->getQuote(); $billingAddress = $quote->getBillingAddress(); $shippingAddress = $quote->getShippingAddress(); $currencyCode = $quote->getCurrency()->getStoreCurrencyCode(); return [ 'reason' => $message, 'checkoutType' => $checkoutType, 'dateAndTime' => $this->getCurrentDateTimeFormatted(), 'customer' => $this->getCustomerName(), 'customerEmail' => $billingAddress->getEmail(), 'billingAddress' => $billingAddress, 'shippingAddress' => $shippingAddress, 'billingAddressHtml' => $billingAddress->format('html'), 'shippingAddressHtml' => $shippingAddress->format('html'), 'shippingMethod' => $this->getShippingCarrierDescription(), 'paymentMethod' => $this->getPaymentMethodDescription(), 'items' => implode('<br />', $this->getQuoteItems()), 'total' => $currencyCode.' '.$quote->getGrandTotal(), ]; } /** * Get config copy to * * @return array|false */ protected function getConfigCopyTo() { $copyTo = $this->getConfigValue('checkout/payment_failed/copy_to'); if (!empty($copyTo)) { return explode(',', $copyTo); } return false; } /** * Handle * * @param int $cartId * @param string $message * @param string $checkoutType * @return $this */ protected function handle( int $cartId, string $message, string $checkoutType = 'onepage' ) { $this->inlineTranslation->suspend(); $this->setQuoteByQuoteId($cartId); $template = $this->getConfigValue('checkout/payment_failed/template'); $receiver = $this->getConfigValue('checkout/payment_failed/receiver'); $sendTo = [ [ 'email' => $this->getConfigValue('trans_email/ident_'.$receiver.'/email'), 'name' => $this->getConfigValue('trans_email/ident_'.$receiver.'/name'), ], ]; $copyMethod = $this->getConfigValue('checkout/payment_failed/copy_method'); $copyTo = $this->getConfigCopyTo(); $bcc = []; if (!empty($copyTo)) { switch ($copyMethod) { case 'bcc': $bcc = $copyTo; break; case 'copy': foreach ($copyTo as $email) { $sendTo[] = ['email' => $email, 'name' => null]; } break; } } foreach ($sendTo as $recipient) { $transport = $this->transportBuilder->setTemplateIdentifier($template) ->setTemplateOptions([ 'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, ]) ->setTemplateVars($this->getTemplateVars($message, $checkoutType)) ->setFrom($this->getConfigValue('checkout/payment_failed/identity')) ->addTo($recipient['email'], $recipient['name']) ->addBcc($bcc) ->getTransport(); try { $transport->sendMessage(); } catch (\Exception $exception) { $this->logger->critical($exception->getMessage()); } } $this->inlineTranslation->resume(); return $this; } /** * Around handle * * @param \Magento\Sales\Model\Service\PaymentFailuresService $subject * @param \Closure $proceed * @param int $cartId * @param string $message * @param string $checkoutType * @return \Magento\Sales\Api\PaymentFailuresInterface */ public function aroundHandle( \Magento\Sales\Model\Service\PaymentFailuresService $subject, \Closure $proceed, int $cartId, string $message, string $checkoutType = 'onepage' ): \Magento\Sales\Api\PaymentFailuresInterface { $this->handle($cartId, $message, $checkoutType); return $subject; } }