Webhook.php 2.93 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
<?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);
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
      try {
        $webhooks = \Stripe\WebhookEndpoint::all(["limit" => 100]);
      }
      catch (\Stripe\Error\Authentication $e) {
        $messages[] = new CRM_Utils_Check_Message(
          'stripe_webhook',
          E::ts('The %1 (%2) Payment Processor has an invalid API key', [
            1 => $pp['name'],
            2 => $pp['id'],
          ]),
          E::ts('Stripe - API Key'),
          \Psr\Log\LogLevel::ERROR,
          'fa-money'
        );

        continue;
      }
bgm's avatar
bgm committed
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
84
85

      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',
86
          E::ts('The %1 (%2) Payment Processor does not have a valid webhook configured for this website. This is only required for recurring contributions. You can review from your Stripe account, under Developers > Webhooks. The webhook URL is: %3', [
bgm's avatar
bgm committed
87
88
            1 => $pp['name'],
            2 => $pp['id'],
89
            3 => urldecode($webhook_path),
bgm's avatar
bgm committed
90
91
92
93
94
95
96
97
98
99
          ]),
          E::ts('Stripe - Webhook'),
          \Psr\Log\LogLevel::WARNING,
          'fa-money'
        );
      }
    }
  }

}