You are here

function mollie_payment_payment_recurring_can_stop in Mollie Payment 7.2

Same name and namespace in other branches
  1. 7 mollie_payment.module \mollie_payment_payment_recurring_can_stop()

Implements hook_payment_recurring_can_OPERATION() for stop.

File

./mollie_payment.module, line 270
Provides Mollie integration for the Payment platform.

Code

function mollie_payment_payment_recurring_can_stop(Payment $payment) {

  // Initialize the client.
  $client = mollie_payment_get_client($payment);
  if ($payment && $client) {
    $customer_id = $payment->context_data['payment']['customer'];
    $subscription_id = $payment->context_data['payment']['subscription'];

    // Mollie API Client throws an exception when trying to get a canceled
    // subscription.
    // @todo Is above still valid?
    try {

      /** @var \Mollie\Api\Resources\Customer $customer */
      $customer = $client->customers
        ->get($customer_id);

      /** @var \Mollie\Api\Resources\Subscription $subscription */
      $subscription = $client->subscriptions
        ->getFor($customer, $subscription_id);
      $invalid_subscription_status = array(
        \Mollie\Api\Types\SubscriptionStatus::STATUS_CANCELED,
        \Mollie\Api\Types\SubscriptionStatus::STATUS_COMPLETED,
      );
      return !in_array($subscription->status, $invalid_subscription_status);
    } catch (Exception $e) {
      return FALSE;
    }
  }
  return TRUE;
}