View source
<?php
namespace Drupal\commerce_recurring\Mail;
use Drupal\commerce\MailHandlerInterface;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\OrderTotalSummaryInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
class PaymentDeclinedMail implements PaymentDeclinedMailInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $mailHandler;
protected $orderTotalSummary;
protected $time;
public function __construct(EntityTypeManagerInterface $entity_type_manager, MailHandlerInterface $mail_handler, OrderTotalSummaryInterface $order_total_summary, TimeInterface $time) {
$this->entityTypeManager = $entity_type_manager;
$this->mailHandler = $mail_handler;
$this->orderTotalSummary = $order_total_summary;
$this->time = $time;
}
public function send(OrderInterface $order, $retry_days, $num_retries, $max_retries) {
$customer = $order
->getCustomer();
if ($customer
->isAnonymous()) {
return FALSE;
}
$order_type_storage = $this->entityTypeManager
->getStorage('commerce_order_type');
$order_type = $order_type_storage
->load($order
->bundle());
$payment_methods_url = Url::fromRoute('entity.commerce_payment_method.collection', [
'user' => $order
->getCustomerId(),
], [
'absolute' => TRUE,
]);
$subject = $this
->t('Payment declined - Order #@number.', [
'@number' => $order
->getOrderNumber(),
]);
$body = [
'#theme' => 'commerce_recurring_payment_declined',
'#order_entity' => $order,
'#retry_num' => $num_retries,
'#retry_days' => "+{$retry_days} days",
'#max_retries' => $max_retries,
'#remaining_retries' => $max_retries - $num_retries,
'#now' => $this->time
->getCurrentTime(),
'#payment_method_link' => $payment_methods_url
->toString(),
'#totals' => $this->orderTotalSummary
->buildTotals($order),
];
if ($billing_profile = $order
->getBillingProfile()) {
$profile_view_builder = $this->entityTypeManager
->getViewBuilder('profile');
$body['#billing_information'] = $profile_view_builder
->view($billing_profile);
}
$params = [
'id' => 'recurring_payment_declined',
'from' => $order
->getStore()
->getEmail(),
'bcc' => $order_type
->getReceiptBcc(),
'order' => $order,
'langcode' => $customer
->getPreferredLangcode(),
];
return $this->mailHandler
->sendMail($order
->getEmail(), $subject, $body, $params);
}
}