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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
<?php
defined( 'ABSPATH' ) || exit();
class WC_Stripe_API_Settings extends WC_Stripe_Settings_API {
public function __construct() {
$this->id = 'stripe_api';
$this->tab_title = __( 'API Settings', 'woo-stripe-payment' );
parent::__construct();
}
public function hooks() {
parent::hooks();
add_action( 'woocommerce_update_options_checkout_' . $this->id, array( $this, 'process_admin_options' ) );
add_filter( 'wc_stripe_settings_nav_tabs', array( $this, 'admin_nav_tab' ) );
add_action( 'woocommerce_stripe_settings_checkout_' . $this->id, array( $this, 'admin_options' ) );
}
public function init_form_fields() {
$this->form_fields = array(
'title' => array(
'type' => 'title',
'title' => __( 'API Settings', 'woo-stripe-payment' ),
),
'test_mode_keys' => array(
'type' => 'description',
'description' => __( 'When test mode is enabled you can manually enter your API keys or go through the connect process. Live mode requires that you click the Connect button.',
'woo-stripe-payment' ),
),
'mode' => array(
'type' => 'select',
'title' => __( 'Mode', 'woo-stripe-payment' ),
'class' => 'wc-enhanced-select',
'options' => array(
'test' => __( 'Test', 'woo-stripe-payment' ),
'live' => __( 'Live', 'woo-stripe-payment' ),
),
'default' => 'test',
'desc_tip' => true,
'description' => __( 'The mode determines if you are processing test transactions or live transactions on your site. Test mode allows you to simulate payments so you can test your integration.',
'woo-stripe-payment' ),
),
'account_id' => array(
'type' => 'paragraph',
'title' => __( 'Account ID', 'woo-stripe-payment' ),
'text' => '',
'class' => '',
'default' => '',
'desc_tip' => true,
'description' => __( 'This is your Stripe Connect ID and serves as a unique identifier.', 'woo-stripe-payment' ),
),
'stripe_connect' => array(
'type' => 'stripe_connect',
'title' => __( 'Connect Stripe Account', 'woo-stripe-payment' ),
'label' => __( 'Click to Connect', 'woo-stripe-payment' ),
'class' => 'do-stripe-connect',
'description' => __( 'We make it easy to connect Stripe to your site. Click the Connect button to go through our connect flow.', 'woo-stripe-payment' ),
),
'publishable_key_test' => array(
'title' => __( 'Test Publishable Key', 'woo-stripe-payment' ),
'type' => 'text',
'default' => '',
'desc_tip' => true,
'description' => __( 'Your publishable key is used to initialize Stripe assets.', 'woo-stripe-payment' ),
'custom_attributes' => array(
'data-show-if' => array(
'mode' => 'test'
)
)
),
'secret_key_test' => array(
'title' => __( 'Test Secret Key', 'woo-stripe-payment' ),
'type' => 'password',
'default' => '',
'desc_tip' => true,
'description' => __( 'Your secret key is used to authenticate requests to Stripe.', 'woo-stripe-payment' ),
'custom_attributes' => array(
'data-show-if' => array(
'mode' => 'test'
)
)
),
'connection_test_live' => array(
'type' => 'stripe_button',
'title' => __( 'Connection Test', 'woo-stripe-payment' ),
'label' => __( 'Connection Test', 'woo-stripe-payment' ),
'class' => 'wc-stripe-connection-test live-mode button-secondary',
'description' => __( 'Click this button to perform a connection test. If successful, your site is connected to Stripe.', 'woo-stripe-payment' ),
'custom_attributes' => array(
'data-show-if' => array(
'mode' => 'live'
)
)
),
'connection_test_test' => array(
'type' => 'stripe_button',
'title' => __( 'Connection Test', 'woo-stripe-payment' ),
'label' => __( 'Connection Test', 'woo-stripe-payment' ),
'class' => 'wc-stripe-connection-test test-mode button-secondary',
'description' => __( 'Click this button to perform a connection test. If successful, your site is connected to Stripe.', 'woo-stripe-payment' ),
'custom_attributes' => array(
'data-show-if' => array(
'mode' => 'test'
)
)
),
'webhook_button_test' => array(
'type' => 'stripe_button',
'title' => __( 'Create Webhook', 'woo-stripe-payment' ),
'label' => __( 'Create Webhook', 'woo-stripe-payment' ),
'class' => 'wc-stripe-create-webhook test-mode button-secondary',
'custom_attributes' => array(
'data-show-if' => array(
'mode' => 'test'
)
)
),
'webhook_button_live' => array(
'type' => 'stripe_button',
'title' => __( 'Create Webhook', 'woo-stripe-payment' ),
'label' => __( 'Create Webhook', 'woo-stripe-payment' ),
'class' => 'wc-stripe-create-webhook live-mode button-secondary',
'custom_attributes' => array(
'data-show-if' => array(
'mode' => 'live'
)
)
),
'webhook_url' => array(
'type' => 'paragraph',
'title' => __( 'Webhook url', 'woo-stripe-payment' ),
'class' => 'wc-stripe-webhook',
'text' => stripe_wc()->rest_api->webhook->rest_url( 'webhook' ),
'desc_tip' => true,
'description' => __( '<strong>Important:</strong> the webhook url is called by Stripe when events occur in your account, like a source becomes chargeable. Use the Create Webhook button or add the webhook manually in your Stripe account.',
'woo-stripe-payment' ),
),
'webhook_secret_live' => array(
'type' => 'password',
'title' => __( 'Live Webhook Secret', 'woo-stripe-payment' ),
'description' => sprintf( __( 'The webhook secret is used to authenticate webhooks sent from Stripe. It ensures no 3rd party can send you events, pretending to be Stripe. %1$sWebhook guide%2$s',
'woo-stripe-payment' ),
'<a target="_blank" href="https://docs.paymentplugins.com/wc-stripe/config/#/webhooks?id=configure-webhooks">',
'</a>' ),
'custom_attributes' => array( 'data-show-if' => array( 'mode' => 'live' ) ),
),
'webhook_secret_test' => array(
'type' => 'password',
'title' => __( 'Test Webhook Secret', 'woo-stripe-payment' ),
'description' => sprintf( __( 'The webhook secret is used to authenticate webhooks sent from Stripe. It ensures no 3rd party can send you events, pretending to be Stripe. %1$sWebhook guide%2$s',
'woo-stripe-payment' ),
'<a target="_blank" href="https://docs.paymentplugins.com/wc-stripe/config/#/webhooks?id=configure-webhooks">',
'</a>' ),
'custom_attributes' => array( 'data-show-if' => array( 'mode' => 'test' ) ),
),
'debug_log' => array(
'title' => __( 'Debug Log', 'woo-stripe-payment' ),
'type' => 'checkbox',
'desc_tip' => true,
'default' => 'yes',
'description' => __( 'When enabled, the plugin logs important errors and info that can help you troubleshoot potential issues.', 'woo-stripe-payment' ),
),
);
if ( $this->get_option( 'account_id' ) ) {
$this->form_fields['account_id']['text'] = $this->get_option( 'account_id' );
$this->form_fields['stripe_connect']['description']
= sprintf( __( '%s Your Stripe account has been connected. You can now accept Live and Test payments. You can Re-Connect if you want to recycle your API keys for security.',
'woo-stripe-payment' ),
'<span class="dashicons dashicons-yes stipe-connect-active"></span>' );
$this->form_fields['stripe_connect']['active'] = true;
} else {
unset( $this->form_fields['account_id'] );
unset( $this->form_fields['connection_test_live'] );
}
foreach ( array( 'test', 'live' ) as $mode ) {
$webhook_id = $this->get_webhook_id( $mode );
if ( $webhook_id ) {
$this->form_fields["webhook_button_{$mode}"]['title'] = __( 'Delete Webhook', 'woo-stripe-payment' );
$this->form_fields["webhook_button_{$mode}"]['label'] = __( 'Delete Webhook', 'woo-stripe-payment' );
$this->form_fields["webhook_button_{$mode}"]['class'] .= ' wc-stripe-delete-webhook';
$this->form_fields["webhook_button_{$mode}"]['description'] = sprintf( __( '%1$s Webhook created. ID: %2$s' ),
'<span class="dashicons dashicons-yes stripe-webhook-created"></span>',
$webhook_id );
}
}
}
public function generate_stripe_connect_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$data = wp_parse_args(
$data,
array(
'class' => '',
'style' => '',
'description' => '',
'desc_tip' => false,
'css' => '',
'active' => false,
)
);
$data['connect_url'] = $this->get_connect_url();
if ( $data['active'] ) {
$data['label'] = __( 'Click To Re-Connect', 'woo-stripe-payment' );
}
ob_start();
include stripe_wc()->plugin_path() . 'includes/admin/views/html-stripe-connect.php';
return ob_get_clean();
}
public function admin_options() {
if ( isset( $_GET['_stripe_connect_nonce'] ) && wp_verify_nonce( $_GET['_stripe_connect_nonce'], 'stripe-connect' ) ) {
if ( isset( $_GET['error'] ) ) {
$error = json_decode( base64_decode( wc_clean( $_GET['error'] ) ) );
if ( property_exists( $error, 'message' ) ) {
$message = $error->message;
} elseif ( property_exists( $error, 'raw' ) ) {
$message = $error->raw->message;
} else {
$message = __( 'Please try again.', 'woo-stripe-payment' );
}
wc_stripe_log_error( sprintf( 'Error connecting to Stripe account. Reason: %s', $message ) );
$this->add_error( sprintf( __( 'We were not able to connect your Stripe account. Reason: %s', 'woo-stripe-payment' ), $message ) );
} elseif ( isset( $_GET['response'] ) ) {
$response = json_decode( base64_decode( $_GET['response'] ) );
$this->settings['account_id'] = $response->live->stripe_user_id;
$this->settings['refresh_token'] = $response->live->refresh_token;
$this->settings['secret_key_live'] = $response->live->access_token;
$this->settings['publishable_key_live'] = $response->live->stripe_publishable_key;
$this->settings['secret_key_test'] = $response->test->access_token;
$this->settings['publishable_key_test'] = $response->test->stripe_publishable_key;
update_option( $this->get_option_key(), $this->settings );
delete_option( 'wc_stripe_connect_notice' );
$this->create_webhooks();
do_action( 'wc_stripe_connect_settings', $response, $this );
$this->init_form_fields();
echo '<div class="updated inline notice-success is-dismissible "><p>' .
__( 'Your Stripe account has been connected to your WooCommerce store. You may now accept payments in Live and Test mode.', 'woo-stripe-payment' ) .
'</p></div>';
}
}
parent::admin_options();
}
public function get_connect_url() {
return \Stripe\OAuth::authorizeUrl( array(
'response_type' => 'code',
'client_id' => stripe_wc()->client_id,
'stripe_landing' => 'login',
'always_prompt' => 'true',
'scope' => 'read_write',
'state' => base64_encode(
wp_json_encode(
array(
'redirect' => add_query_arg( '_stripe_connect_nonce', wp_create_nonce( 'stripe-connect' ), admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=stripe_api' ) )
)
)
)
) );
}
public function localize_settings() {
return parent::localize_settings();
}
public function delete_webhook_settings( $mode ) {
unset( $this->settings["webhook_secret_{$mode}"], $this->settings["webhook_id_{$mode}"] );
update_option( $this->get_option_key(), $this->settings );
}
public function create_webhook( $mode, $events = array() ) {
$client = WC_Stripe_Gateway::load();
$url = get_rest_url( null, '/wc-stripe/v1/webhook' );
if ( ! in_array( '*', $events ) ) {
$events = apply_filters(
'wc_stripe_webhook_events',
array_values( array_unique( array_merge( array(
'charge.failed',
'charge.succeeded',
'source.chargeable',
'payment_intent.succeeded',
'charge.refunded',
'charge.dispute.created',
'charge.dispute.closed',
'review.opened',
'review.closed'
), $events ) ) )
);
}
$webhook = $client->mode( $mode )->webhookEndpoints->create( array(
'api_version' => '2020-03-02',
'url' => $url,
'enabled_events' => $events,
) );
if ( is_wp_error( $webhook ) ) {
wc_stripe_log_error( sprintf( 'Error creating Stripe webhook. Mode: %1$s. Reason: %2$s', $mode, $webhook->get_error_message() ) );
} else {
$this->settings["webhook_secret_{$mode}"] = $webhook['secret'];
$this->settings["webhook_id_{$mode}"] = $webhook['id'];
update_option( $this->get_option_key(), $this->settings );
}
return $webhook;;
}
private function create_webhooks() {
foreach ( array( 'live', 'test' ) as $mode ) {
$this->create_webhook( $mode );
}
}
public function get_webhook_id( $mode ) {
return $this->get_option( "webhook_id_{$mode}", null );
}
}