You are here

public function PaymentDeclinedMail::send in Commerce Recurring Framework 8

Sends a payment declined email.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The recurring order.

int $retry_days: Days until next retry.

int $num_retries: Number of past attempts.

int $max_retries: Maximum number of retries allowed.

Return value

bool TRUE if the email was sent successfully, FALSE otherwise.

Overrides PaymentDeclinedMailInterface::send

File

src/Mail/PaymentDeclinedMail.php, line 70

Class

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

Namespace

Drupal\commerce_recurring\Mail

Code

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);
}