Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentFailuresService.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
21 use Psr\Log\LoggerInterface;
22 
31 {
37  private $scopeConfig;
38 
42  private $inlineTranslation;
43 
47  private $transportBuilder;
48 
52  private $localeDate;
53 
57  private $cartRepository;
58 
62  private $logger;
63 
72  public function __construct(
73  ScopeConfigInterface $scopeConfig,
74  StateInterface $inlineTranslation,
75  TransportBuilder $transportBuilder,
76  TimezoneInterface $localeDate,
77  CartRepositoryInterface $cartRepository,
78  LoggerInterface $logger = null
79  ) {
80  $this->scopeConfig = $scopeConfig;
81  $this->inlineTranslation = $inlineTranslation;
82  $this->transportBuilder = $transportBuilder;
83  $this->localeDate = $localeDate;
84  $this->cartRepository = $cartRepository;
85  $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
86  }
87 
96  public function handle(
97  int $cartId,
98  string $message,
99  string $checkoutType = 'onepage'
101  $this->inlineTranslation->suspend();
102  $quote = $this->cartRepository->get($cartId);
103 
104  $template = $this->getConfigValue('checkout/payment_failed/template', $quote);
105  $receiver = $this->getConfigValue('checkout/payment_failed/receiver', $quote);
106  $sendTo = [
107  [
108  'email' => $this->getConfigValue('trans_email/ident_' . $receiver . '/email', $quote),
109  'name' => $this->getConfigValue('trans_email/ident_' . $receiver . '/name', $quote),
110  ],
111  ];
112 
113  $copyMethod = $this->getConfigValue('checkout/payment_failed/copy_method', $quote);
114  $copyTo = $this->getConfigEmails($quote);
115 
116  $bcc = [];
117  if (!empty($copyTo)) {
118  switch ($copyMethod) {
119  case 'bcc':
120  $bcc = $copyTo;
121  break;
122  case 'copy':
123  foreach ($copyTo as $email) {
124  $sendTo[] = ['email' => $email, 'name' => null];
125  }
126  break;
127  }
128  }
129 
130  foreach ($sendTo as $recipient) {
131  $transport = $this->transportBuilder
132  ->setTemplateIdentifier($template)
133  ->setTemplateOptions([
135  'store' => Store::DEFAULT_STORE_ID,
136  ])
137  ->setTemplateVars($this->getTemplateVars($quote, $message, $checkoutType))
138  ->setFrom($this->getSendFrom($quote))
139  ->addTo($recipient['email'], $recipient['name'])
140  ->addBcc($bcc)
141  ->getTransport();
142 
143  try {
144  $transport->sendMessage();
145  } catch (\Exception $e) {
146  $this->logger->critical($e->getMessage());
147  }
148  }
149 
150  $this->inlineTranslation->resume();
151 
152  return $this;
153  }
154 
163  private function getTemplateVars(Quote $quote, string $message, string $checkoutType): array
164  {
165  return [
166  'reason' => $message,
167  'checkoutType' => $checkoutType,
168  'dateAndTime' => $this->getLocaleDate(),
169  'customer' => $this->getCustomerName($quote),
170  'customerEmail' => $quote->getBillingAddress()->getEmail(),
171  'billingAddress' => $quote->getBillingAddress(),
172  'shippingAddress' => $quote->getShippingAddress(),
173  'shippingMethod' => $this->getConfigValue(
174  'carriers/' . $this->getShippingMethod($quote) . '/title',
175  $quote
176  ),
177  'paymentMethod' => $this->getConfigValue(
178  'payment/' . $this->getPaymentMethod($quote) . '/title',
179  $quote
180  ),
181  'items' => implode('<br />', $this->getQuoteItems($quote)),
182  'total' => $quote->getCurrency()->getStoreCurrencyCode() . ' ' . $quote->getGrandTotal(),
183  ];
184  }
185 
193  private function getConfigValue(string $configPath, Quote $quote)
194  {
195  return $this->scopeConfig->getValue(
196  $configPath,
198  $quote->getStoreId()
199  );
200  }
201 
208  private function getShippingMethod(Quote $quote): string
209  {
210  $shippingMethod = '';
211  $shippingInfo = $quote->getShippingAddress()->getShippingMethod();
212 
213  if ($shippingInfo) {
214  $data = explode('_', $shippingInfo);
215  $shippingMethod = $data[0];
216  }
217 
218  return $shippingMethod;
219  }
220 
227  private function getPaymentMethod(Quote $quote): string
228  {
229  $paymentMethod = $quote->getPayment()->getMethod() ?? '';
230 
231  return $paymentMethod;
232  }
233 
240  private function getQuoteItems(Quote $quote): array
241  {
242  $items = [];
243  foreach ($quote->getAllVisibleItems() as $item) {
244  $itemData = $item->getProduct()->getName() . ' x ' . $item->getQty() . ' ' .
245  $quote->getCurrency()->getStoreCurrencyCode() . ' ' .
246  $item->getProduct()->getFinalPrice($item->getQty());
247  $items[] = $itemData;
248  }
249 
250  return $items;
251  }
252 
259  private function getConfigEmails(Quote $quote)
260  {
261  $configData = $this->getConfigValue('checkout/payment_failed/copy_to', $quote);
262  if (!empty($configData)) {
263  return explode(',', $configData);
264  }
265 
266  return false;
267  }
268 
275  private function getSendFrom(Quote $quote): string
276  {
277  return $this->getConfigValue('checkout/payment_failed/identity', $quote);
278  }
279 
285  private function getLocaleDate(): string
286  {
287  return $this->localeDate->formatDateTime(
288  new \DateTime(),
289  \IntlDateFormatter::MEDIUM,
290  \IntlDateFormatter::MEDIUM
291  );
292  }
293 
300  private function getCustomerName(Quote $quote): string
301  {
302  $customer = __('Guest')->render();
303  if (!$quote->getCustomerIsGuest()) {
304  $customer = $quote->getCustomer()->getFirstname() . ' ' .
305  $quote->getCustomer()->getLastname();
306  }
307 
308  return $customer;
309  }
310 }
handle(int $cartId, string $message, string $checkoutType='onepage')
$cartRepository
Definition: quote.php:18
$customer
Definition: customers.php:11
$email
Definition: details.phtml:13
$quote
__()
Definition: __.php:13
$message
$cartId
Definition: quote.php:22
$shippingMethod
Definition: popup.phtml:12
$template
Definition: export.php:12
__construct(ScopeConfigInterface $scopeConfig, StateInterface $inlineTranslation, TransportBuilder $transportBuilder, TimezoneInterface $localeDate, CartRepositoryInterface $cartRepository, LoggerInterface $logger=null)
$items