|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cryptapi\Cryptapi\Helper; |
| 4 | + |
| 5 | +use Psr\Log\LoggerInterface; |
| 6 | +use Magento\Framework\App\Area; |
| 7 | +use Magento\Store\Model\ScopeInterface; |
| 8 | +use Magento\Framework\App\Helper\Context; |
| 9 | +use Magento\Store\Model\StoreManagerInterface; |
| 10 | +use Magento\Framework\Exception\MailException; |
| 11 | +use Magento\Framework\App\Helper\AbstractHelper; |
| 12 | +use Magento\Framework\Exception\LocalizedException; |
| 13 | +use Magento\Framework\Mail\Template\TransportBuilder; |
| 14 | +use Magento\Framework\Translate\Inline\StateInterface; |
| 15 | + |
| 16 | +class Mail extends AbstractHelper |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var StateInterface |
| 20 | + */ |
| 21 | + private $inlineTranslation; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var TransportBuilder |
| 25 | + */ |
| 26 | + private $transportBuilder; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var StoreManagerInterface |
| 30 | + */ |
| 31 | + private $storeManager; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var LoggerInterface |
| 35 | + */ |
| 36 | + private $logger; |
| 37 | + |
| 38 | + /** |
| 39 | + * Data constructor. |
| 40 | + * @param Context $context |
| 41 | + * @param StoreManagerInterface $storeManager |
| 42 | + * @param TransportBuilder $transportBuilder |
| 43 | + * @param StateInterface $inlineTranslation |
| 44 | + * @param LoggerInterface $logger |
| 45 | + */ |
| 46 | + public function __construct( |
| 47 | + Context $context, |
| 48 | + StoreManagerInterface $storeManager, |
| 49 | + TransportBuilder $transportBuilder, |
| 50 | + StateInterface $inlineTranslation, |
| 51 | + LoggerInterface $logger |
| 52 | + ) |
| 53 | + { |
| 54 | + $this->storeManager = $storeManager; |
| 55 | + $this->transportBuilder = $transportBuilder; |
| 56 | + $this->inlineTranslation = $inlineTranslation; |
| 57 | + $this->logger = $logger; |
| 58 | + parent::__construct($context); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Send Mail |
| 63 | + * |
| 64 | + * @return $this |
| 65 | + * |
| 66 | + * @throws LocalizedException |
| 67 | + * @throws MailException |
| 68 | + */ |
| 69 | + public function sendMail($order, $metadata) |
| 70 | + { |
| 71 | + $customerId = $order->getCustomerId(); |
| 72 | + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); |
| 73 | + $scopeConfig = $objectManager->create('\Magento\Framework\App\Config\ScopeConfigInterface'); |
| 74 | + |
| 75 | + $customerData = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId); |
| 76 | + |
| 77 | + $email = $customerData->getEmail(); |
| 78 | + |
| 79 | + $storeId = $this->getStoreId(); |
| 80 | + $orderId = $order->getId(); |
| 81 | + $coin = $metadata['cryptapi_currency']; |
| 82 | + $url = $metadata['cryptapi_payment_url']; |
| 83 | + |
| 84 | + $sender = [ |
| 85 | + 'email' => $scopeConfig->getValue('trans_email/ident_sales/email', ScopeInterface::SCOPE_STORE), |
| 86 | + 'name' => $scopeConfig->getValue('trans_email/ident_sales/name', ScopeInterface::SCOPE_STORE) |
| 87 | + ]; |
| 88 | + |
| 89 | + $this->inlineTranslation->suspend(); |
| 90 | + |
| 91 | + $vars = [ |
| 92 | + 'order' => $orderId, |
| 93 | + 'coin' => strtoupper($coin), |
| 94 | + 'url' => $url, |
| 95 | + ]; |
| 96 | + |
| 97 | + $postObject = new \Magento\Framework\DataObject(); |
| 98 | + $postObject->setData($vars); |
| 99 | + |
| 100 | + $transport = $this->transportBuilder->setTemplateIdentifier( |
| 101 | + 'cryptapi_email_link' |
| 102 | + )-> |
| 103 | + setTemplateVars(['data' => $postObject])-> |
| 104 | + setTemplateOptions([ |
| 105 | + 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, |
| 106 | + 'store' => $storeId, |
| 107 | + ])-> |
| 108 | + setFromByScope($sender)-> |
| 109 | + addTo($email)-> |
| 110 | + getTransport(); |
| 111 | + |
| 112 | + try { |
| 113 | + $transport->sendMessage(); |
| 114 | + } catch (\Exception $exception) { |
| 115 | + $this->logger->critical($exception->getMessage()); |
| 116 | + } |
| 117 | + $this->inlineTranslation->resume(); |
| 118 | + |
| 119 | + return $this; |
| 120 | + } |
| 121 | + |
| 122 | + /* |
| 123 | + * get Current store id |
| 124 | + */ |
| 125 | + public function getStoreId() |
| 126 | + { |
| 127 | + return $this->storeManager->getStore()->getId(); |
| 128 | + } |
| 129 | + |
| 130 | + /* |
| 131 | + * get Current store Info |
| 132 | + */ |
| 133 | + public function getStore() |
| 134 | + { |
| 135 | + return $this->storeManager->getStore(); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | + |
0 commit comments