| 1 |
<?php |
| 2 |
/** |
| 3 |
* Webhook Tool |
| 4 |
* |
| 5 |
* @since 4.14.0 |
| 6 |
* @package SimplePay\Core\Webhook |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SimplePay\Core\Webhook; |
| 10 |
|
| 11 |
use SimplePay\Core\Utils; |
| 12 |
use SimplePay\Core\EventManagement\SubscriberInterface; |
| 13 |
|
| 14 |
/** |
| 15 |
* CreateWebhookTool class. |
| 16 |
* |
| 17 |
* @since 4.14.0 |
| 18 |
*/ |
| 19 |
class CreateWebhookTool implements SubscriberInterface { |
| 20 |
|
| 21 |
/** |
| 22 |
* Webhook endpoint manager. |
| 23 |
* |
| 24 |
* @since 4.14.0 |
| 25 |
* @var \SimplePay\Core\Webhook\WebhookEndpointManager $endpoint_manager |
| 26 |
*/ |
| 27 |
protected $endpoint_manager; |
| 28 |
|
| 29 |
/** |
| 30 |
* StripeConnectCreation. |
| 31 |
* |
| 32 |
* @since 4.14.0 |
| 33 |
* |
| 34 |
* @param \SimplePay\Core\Webhook\WebhookEndpointManager $endpoint Webhook endpoint manager. |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function __construct( WebhookEndpointManager $endpoint ) { |
| 38 |
$this->endpoint_manager = $endpoint; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* {@inheritdoc} |
| 44 |
*/ |
| 45 |
public function get_subscribed_events() { |
| 46 |
return array( |
| 47 |
'__unstable_simpay_before_webhook_setting' => 'output_button', |
| 48 |
'wp_ajax_simpay_recreate_webhook' => 'handle_recreate_webhook', |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Outputs the button to recreate the webhook. |
| 54 |
* |
| 55 |
* @since 4.14.0 |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function output_button() { |
| 60 |
|
| 61 |
$webhooks_url = simpay_is_test_mode() |
| 62 |
? 'https://dashboard.stripe.com/test/workbench/webhooks/' |
| 63 |
: 'https://dashboard.stripe.com/workbench/webhooks/'; |
| 64 |
|
| 65 |
// Check current webhook status. |
| 66 |
$endpoint = $this->endpoint_manager->exists(); |
| 67 |
$status_message = ''; |
| 68 |
$status_color = ''; |
| 69 |
if ( $endpoint instanceof \SimplePay\Vendor\Stripe\WebhookEndpoint ) { |
| 70 |
if ( $this->endpoint_manager->is_valid( $endpoint ) ) { |
| 71 |
$status_message = __( 'Your Stripe webhook is set up correctly.', 'stripe' ); |
| 72 |
$status_color = 'green'; |
| 73 |
} else { |
| 74 |
$status_message = __( 'Action required: Your Stripe webhook is not valid. Please recreate it.', 'stripe' ); |
| 75 |
$status_color = 'red'; |
| 76 |
} |
| 77 |
|
| 78 |
$action_text = __( 'Recreate Webhook', 'stripe' ); |
| 79 |
} else { |
| 80 |
$status_message = __( 'No Stripe webhook is currently set up.', 'stripe' ); |
| 81 |
$status_color = 'red'; |
| 82 |
$action_text = __( 'Create Webhook', 'stripe' ); |
| 83 |
} |
| 84 |
|
| 85 |
// Output the button. |
| 86 |
|
| 87 |
echo wp_kses_post( |
| 88 |
sprintf( |
| 89 |
/* translators: %1$s Opening anchor tag, do not translate. %2$s Closing anchor tag, do not translate. */ |
| 90 |
__( 'Stripe uses webhooks to notify WP Simple Pay when an event has occurred in your Stripe account. Ensure an endpoint is present in the %1$sStripe webhook settings %2$s', 'stripe' ), |
| 91 |
'<a href="' . $webhooks_url . '" target="_blank" rel="noopener noreferrer" class="simpay-external-link"><strong>', |
| 92 |
'</strong>' . Utils\get_external_link_markup() . '</a><br><br>' |
| 93 |
) |
| 94 |
); |
| 95 |
wp_nonce_field( 'simpay_recreate_webhook', 'simpay_recreate_webhook_nonce' ); |
| 96 |
|
| 97 |
echo '<div style="display: flex; align-items: center; gap: 10px;">'; |
| 98 |
echo '<button type="button" id="simpay-recreate-webhook" class="button button-primary button-large">' . esc_html( $action_text ) . '</button>'; |
| 99 |
|
| 100 |
echo '<div id="simpay-webhook-status-message" style="color:' . esc_attr( $status_color ) . ';">' . esc_html( $status_message ) . '</div>'; |
| 101 |
echo '</div>'; |
| 102 |
|
| 103 |
echo '<div id="simpay-recreate-webhook-message" style="margin-top:10px;"></div>'; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Handles the recreate webhook request. |
| 108 |
* |
| 109 |
* @since 4.14.0 |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function handle_recreate_webhook() { |
| 114 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 115 |
wp_send_json_error( array( 'message' => __( 'Unauthorized', 'stripe' ) ) ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! isset( $_POST['simpay_recreate_webhook_nonce'] ) || ! wp_verify_nonce( $_POST['simpay_recreate_webhook_nonce'], 'simpay_recreate_webhook' ) ) { |
| 119 |
wp_send_json_error( array( 'message' => __( 'Nonce check failed. Please reload the page and try again.', 'stripe' ) ) ); |
| 120 |
} |
| 121 |
|
| 122 |
$endpoint = $this->endpoint_manager->exists(); |
| 123 |
|
| 124 |
if ( $endpoint instanceof \SimplePay\Vendor\Stripe\WebhookEndpoint && $this->endpoint_manager->is_valid( $endpoint ) ) { |
| 125 |
wp_send_json_success( array( 'message' => __( 'Webhook recreated.', 'stripe' ) ) ); |
| 126 |
} |
| 127 |
|
| 128 |
$this->endpoint_manager->create(); |
| 129 |
wp_send_json_success( array( 'message' => __( 'Webhook recreated.', 'stripe' ) ) ); |
| 130 |
} |
| 131 |
} |
| 132 |
|