PluginProbe
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe / trunk
Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe vtrunk
4.17.3 trunk 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 4.10.0 4.11.1 4.12.2 4.14.1 4.14.2 4.14.3 4.15.0 4.16.0 All 59 releases
stripe / src / Webhook / EndpointHealthCheck.php

EndpointHealthCheck.php in Stripe Payment Forms by WP Simple Pay – Accept Credit Card Payments + Subscriptions with Stripe trunk, at src/Webhook/EndpointHealthCheck.php

159 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Webhook: Health check
4 *
5 * @package SimplePay
6 * @subpackage Core
7 * @copyright Copyright (c) 2022, Sandhills Development, LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 4.4.2
10 */
11
12 namespace SimplePay\Core\Webhook;
13
14 use Exception;
15 use SimplePay\Core\EventManagement\SubscriberInterface;
16 use SimplePay\Core\License\LicenseAwareInterface;
17 use SimplePay\Core\License\LicenseAwareTrait;
18
19 /**
20 * EndpointHealthCheck class.
21 *
22 * @since 4.4.2
23 */
24 class EndpointHealthCheck implements SubscriberInterface, LicenseAwareInterface {
25
26 use LicenseAwareTrait;
27
28 /**
29 * Webhook endpoint manager.
30 *
31 * @since 4.4.2
32 * @var \SimplePay\Core\Webhook\WebhookEndpointManager $endpoint
33 */
34 private $endpoint;
35
36 /**
37 * StripeConnectCreation.
38 *
39 * @since 4.4.2
40 *
41 * @param \SimplePay\Core\Webhook\WebhookEndpointManager $endpoint Webhook endpoint manager.
42 * @return void
43 */
44 public function __construct( WebhookEndpointManager $endpoint ) {
45 $this->endpoint = $endpoint;
46 }
47
48 /**
49 * {@inheritdoc}
50 */
51 public function get_subscribed_events() {
52 if ( true === $this->license->is_lite() ) {
53 return array();
54 }
55
56 $dismissed = (bool) get_option(
57 'simpay_webhook_event_expected_dismiss',
58 false
59 );
60
61 // Do not run health check if the event received notice was permanently dismissed.
62 if ( true === $dismissed ) {
63 return array();
64 }
65
66 return array(
67 'admin_init' => 'maybe_update',
68 'generate_rewrite_rules' => 'update_endpoint_url',
69 );
70 }
71
72 /**
73 * Possibly updates the webhook endpoint if the stored values do not match what is expected.
74 *
75 * @since 4.2.2
76 *
77 * @return void
78 */
79 public function maybe_update() {
80 try {
81 $prefix = simpay_is_test_mode() ? 'test' : 'live';
82
83 // Check the ID.
84 /** @var string $endpoint_id */
85 $endpoint_id = simpay_get_setting(
86 "{$prefix}_webhook_endpoint_id",
87 ''
88 );
89
90 if ( empty( $endpoint_id ) ) {
91 $this->endpoint->create();
92
93 return;
94 }
95
96 // Check the URLs.
97 /** @var string $endpoint_url */
98 $endpoint_url = simpay_get_setting(
99 "{$prefix}_webhook_endpoint_url",
100 ''
101 );
102
103 if (
104 empty( $endpoint_url ) ||
105 simpay_get_webhook_url() !== $endpoint_url
106 ) {
107 $endpoint = $this->endpoint->get( $endpoint_id );
108 $this->endpoint->update( $endpoint );
109
110 return;
111 }
112
113 // Check the events.
114 /** @var array<string> $endpoint_events */
115 $endpoint_events = simpay_get_setting(
116 "{$prefix}_webhook_endpoint_events",
117 array()
118 );
119
120 if (
121 // No events.
122 empty( $endpoint_events ) ||
123 (
124 // Not all events, and saved is fewer than our current whitelist.
125 ! in_array( '*', $endpoint_events, true ) &&
126 count( $endpoint_events ) < count( $this->endpoint->get_event_whitelist() )
127 )
128 ) {
129 $endpoint = $this->endpoint->get( $endpoint_id );
130 $this->endpoint->update( $endpoint );
131
132 return;
133 }
134 } catch ( Exception $e ) {
135 // Fail silently.
136 // @todo Show a separate notice?
137 }
138 }
139
140 /**
141 * Updates the stored webhook URL when permalink structure changes.
142 *
143 * This will trigger an update the next time the webhook endpoint is checked.
144 *
145 * @since 4.4.2
146 *
147 * @return void
148 */
149 public function update_endpoint_url() {
150 $prefix = simpay_is_test_mode() ? 'test' : 'live';
151
152 simpay_update_setting(
153 "{$prefix}_webhook_endpoint_url",
154 simpay_get_webhook_url()
155 );
156 }
157
158 }
159