You are here

protected function RecurringJobTypeBase::handleDecline in Commerce Recurring Framework 8

Handles a declined order payment.

Parameters

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

\Drupal\commerce_payment\Exception\DeclineException $exception: The decline exception.

int $num_retries: The number of times the job was retried so far.

Return value

\Drupal\advancedqueue\JobResult The job result.

1 call to RecurringJobTypeBase::handleDecline()
RecurringOrderClose::process in src/Plugin/AdvancedQueue/JobType/RecurringOrderClose.php
Processes the given job.

File

src/Plugin/AdvancedQueue/JobType/RecurringJobTypeBase.php, line 94

Class

RecurringJobTypeBase
Provides a base class for recurring job types.

Namespace

Drupal\commerce_recurring\Plugin\AdvancedQueue\JobType

Code

protected function handleDecline(OrderInterface $order, DeclineException $exception, $num_retries) {

  /** @var \Drupal\commerce_recurring\Entity\BillingScheduleInterface $billing_schedule */
  $billing_schedule = $order
    ->get('billing_schedule')->entity;
  $schedule = $billing_schedule
    ->getRetrySchedule();
  $max_retries = count($schedule);
  if ($num_retries < $max_retries) {
    $retry_days = $schedule[$num_retries];
    $result = JobResult::failure($exception
      ->getMessage(), $max_retries, 86400 * $retry_days);
  }
  else {
    $retry_days = 0;
    $result = JobResult::success('Dunning complete, recurring order not paid.');
    $this
      ->handleFailedOrder($order, FALSE);
  }

  // Subscribers can choose to send a dunning email.
  $event = new PaymentDeclinedEvent($order, $retry_days, $num_retries, $max_retries, $exception);
  $this->eventDispatcher
    ->dispatch(RecurringEvents::PAYMENT_DECLINED, $event);
  $order
    ->save();
  return $result;
}