6 declare(strict_types=1);
21 use Psr\Log\LoggerInterface;
42 private $inlineTranslation;
47 private $transportBuilder;
57 private $cartRepository;
80 $this->scopeConfig = $scopeConfig;
81 $this->inlineTranslation = $inlineTranslation;
82 $this->transportBuilder = $transportBuilder;
83 $this->localeDate = $localeDate;
99 string $checkoutType =
'onepage' 101 $this->inlineTranslation->suspend();
104 $template = $this->getConfigValue(
'checkout/payment_failed/template',
$quote);
105 $receiver = $this->getConfigValue(
'checkout/payment_failed/receiver',
$quote);
108 'email' => $this->getConfigValue(
'trans_email/ident_' . $receiver .
'/email',
$quote),
109 'name' => $this->getConfigValue(
'trans_email/ident_' . $receiver .
'/name',
$quote),
113 $copyMethod = $this->getConfigValue(
'checkout/payment_failed/copy_method',
$quote);
114 $copyTo = $this->getConfigEmails(
$quote);
117 if (!empty($copyTo)) {
118 switch ($copyMethod) {
123 foreach ($copyTo as
$email) {
124 $sendTo[] = [
'email' =>
$email,
'name' =>
null];
130 foreach ($sendTo as $recipient) {
131 $transport = $this->transportBuilder
133 ->setTemplateOptions([
137 ->setTemplateVars($this->getTemplateVars(
$quote,
$message, $checkoutType))
138 ->setFrom($this->getSendFrom(
$quote))
139 ->addTo($recipient[
'email'], $recipient[
'name'])
144 $transport->sendMessage();
146 $this->logger->critical($e->getMessage());
150 $this->inlineTranslation->resume();
163 private function getTemplateVars(
Quote $quote,
string $message,
string $checkoutType): array
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',
177 'paymentMethod' => $this->getConfigValue(
178 'payment/' . $this->getPaymentMethod(
$quote) .
'/title',
181 'items' => implode(
'<br />', $this->getQuoteItems(
$quote)),
182 'total' =>
$quote->getCurrency()->getStoreCurrencyCode() .
' ' .
$quote->getGrandTotal(),
193 private function getConfigValue(
string $configPath,
Quote $quote)
195 return $this->scopeConfig->getValue(
208 private function getShippingMethod(Quote
$quote): string
211 $shippingInfo =
$quote->getShippingAddress()->getShippingMethod();
214 $data = explode(
'_', $shippingInfo);
227 private function getPaymentMethod(Quote
$quote): string
229 $paymentMethod =
$quote->getPayment()->getMethod() ??
'';
231 return $paymentMethod;
240 private function getQuoteItems(Quote
$quote): array
244 $itemData =
$item->getProduct()->getName() .
' x ' .
$item->getQty() .
' ' .
245 $quote->getCurrency()->getStoreCurrencyCode() .
' ' .
246 $item->getProduct()->getFinalPrice(
$item->getQty());
259 private function getConfigEmails(Quote
$quote)
275 private function getSendFrom(Quote
$quote): string
277 return $this->getConfigValue(
'checkout/payment_failed/identity',
$quote);
285 private function getLocaleDate(): string
287 return $this->localeDate->formatDateTime(
289 \IntlDateFormatter::MEDIUM,
290 \IntlDateFormatter::MEDIUM
300 private function getCustomerName(Quote
$quote): string
303 if (!
$quote->getCustomerIsGuest()) {
305 $quote->getCustomer()->getLastname();
handle(int $cartId, string $message, string $checkoutType='onepage')
__construct(ScopeConfigInterface $scopeConfig, StateInterface $inlineTranslation, TransportBuilder $transportBuilder, TimezoneInterface $localeDate, CartRepositoryInterface $cartRepository, LoggerInterface $logger=null)