You are here

class PaymentDeclinedMail in Commerce Recurring Framework 8

Sends an email when the payment is declined for a recurring order.

Hierarchy

Expanded class hierarchy of PaymentDeclinedMail

1 file declares its use of PaymentDeclinedMail
DunningSubscriber.php in src/EventSubscriber/DunningSubscriber.php
1 string reference to 'PaymentDeclinedMail'
commerce_recurring.services.yml in ./commerce_recurring.services.yml
commerce_recurring.services.yml
1 service uses PaymentDeclinedMail
commerce_recurring.payment_declined_mail in ./commerce_recurring.services.yml
Drupal\commerce_recurring\Mail\PaymentDeclinedMail

File

src/Mail/PaymentDeclinedMail.php, line 16

Namespace

Drupal\commerce_recurring\Mail
View source
class PaymentDeclinedMail implements PaymentDeclinedMailInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The mail handler.
   *
   * @var \Drupal\commerce\MailHandlerInterface
   */
  protected $mailHandler;

  /**
   * The order total summary.
   *
   * @var \Drupal\commerce_order\OrderTotalSummaryInterface
   */
  protected $orderTotalSummary;

  /**
   * The time.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs a new PaymentDeclinedMail object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\commerce\MailHandlerInterface $mail_handler
   *   The mail handler.
   * @param \Drupal\commerce_order\OrderTotalSummaryInterface $order_total_summary
   *   The order total summary.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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');

    /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentDeclinedMail::$entityTypeManager protected property The entity type manager.
PaymentDeclinedMail::$mailHandler protected property The mail handler.
PaymentDeclinedMail::$orderTotalSummary protected property The order total summary.
PaymentDeclinedMail::$time protected property The time.
PaymentDeclinedMail::send public function Sends a payment declined email. Overrides PaymentDeclinedMailInterface::send
PaymentDeclinedMail::__construct public function Constructs a new PaymentDeclinedMail object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.