Webhook.php 2.48 KB
Newer Older
bgm's avatar
bgm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
<?php

use CRM_Stripe_ExtensionUtil as E;

class CRM_Stripe_Utils_Check_Webhook {

  /**
   * Checks whether the live Stripe processors have a correctly configured
   * webhook (we may want to check the test processors too, at some point, but
   * for now, avoid having false alerts that will annoy people).
   *
   * @see stripe_civicrm_check()
   */
  public static function check(&$messages) {
    $result = civicrm_api3('PaymentProcessor', 'get', [
      'class_name' => 'Payment_Stripe',
      'is_active' => 1,
      'is_test' => 0,
    ]);

    foreach ($result['values'] as $pp) {
      $sk = $pp['user_name'];

      $webhook_path = stripe_get_webhook_path(TRUE);
      $webhook_path = str_replace('NN', $pp['id'], $webhook_path);

      \Stripe\Stripe::setApiKey($sk);
      $webhooks = \Stripe\WebhookEndpoint::all(["limit" => 100]);

      if (empty($webhooks->data)) {
        $messages[] = new CRM_Utils_Check_Message(
          'stripe_webhook',
          E::ts('The %1 (%2) Payment Processor does not have a webhook configured. This is only required for recurring contributions. You can review from your Stripe account, under Developers > Webhooks. The webhook URL is: %3', [
            1 => $pp['name'],
            2 => $pp['id'],
            3 => $webhook_path,
          ]),
          E::ts('Stripe - Webhook'),
          \Psr\Log\LogLevel::INFO,
          'fa-money'
        );

        continue;
      }

      $found_wh = FALSE;

      foreach ($webhooks->data as $wh) {
        if ($wh->url == $webhook_path) {
          $found_wh = TRUE;
        }
      }

      if ($found_wh) {
        $messages[] = new CRM_Utils_Check_Message(
          'stripe_webhook',
          E::ts('The %1 (%2) Payment Processor has a webhook configured (%3).', [
            1 => $pp['name'],
            2 => $pp['id'],
            3 => $webhook_path,
          ]),
          E::ts('Stripe - Webhook'),
          \Psr\Log\LogLevel::INFO,
          'fa-money'
        );
      }
      else {
        $messages[] = new CRM_Utils_Check_Message(
          'stripe_webhook',
          E::ts('The %1 (%2) Payment Processor does not have a webhook configured. This is only required for recurring contributions. You can review from your Stripe account, under Developers > Webhooks. The webhook URL is: %3', [
            1 => $pp['name'],
            2 => $pp['id'],
            3 => $webhook_path,
          ]),
          E::ts('Stripe - Webhook'),
          \Psr\Log\LogLevel::WARNING,
          'fa-money'
        );
      }
    }
  }

}