class-ld-settings-section-stripe-connect.php
19.2 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/**
* LearnDash Settings Section for Stripe Connect.
*
* @since 4.0.0
* @package \LearnDash\Settings\Sections
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'LearnDash_Settings_Section' ) ) && ( ! class_exists( 'LearnDash_Settings_Section_Stripe_Connect' ) ) ) {
/**
* Class LearnDash Settings Section for Stripe Connect.
*
* @since 4.0.0
*/
class LearnDash_Settings_Section_Stripe_Connect extends LearnDash_Settings_Section {
const CONNECT_SERVER_URL = 'https://connect.learndash.com/stripe/connect.php';
const STRIPE_RETURNED_SUCCESS = 1;
const STRIPE_RETURNED_AND_PROCESSED_SUCCESS = 2;
const STRIPE_CUSTOMER_ID_META_KEY = 'stripe_connect_customer_id';
const STRIPE_CUSTOMER_ID_META_KEY_TEST = 'stripe_connect_test_customer_id';
/**
* Protected constructor for class
*
* @since 4.0.0
*/
protected function __construct() {
$this->settings_page_id = 'learndash_lms_payments';
// This is the 'option_name' key used in the wp_options table.
$this->setting_option_key = 'learndash_stripe_connection_settings';
// This is the HTML form field prefix used.
$this->setting_field_prefix = 'learndash_stripe_connection_settings';
// Used within the Settings API to uniquely identify this section.
$this->settings_section_key = 'settings_stripe_connection';
// Section label/header.
$this->settings_section_label = esc_html__( 'Stripe Connect Settings', 'learndash' );
// Used to associate this section with the parent section.
$this->settings_parent_section_key = 'settings_payments_list';
$this->settings_section_listing_label = esc_html__( 'Stripe Connect', 'learndash' );
parent::__construct();
$this->handle_connection_request();
$this->handle_disconnection_request();
add_action( 'admin_notices', array( $this, 'show_notices' ) );
}
/**
* Initialize the metabox settings values.
*
* @since 4.0.0
*/
public function load_settings_values() {
parent::load_settings_values();
if ( ! isset( $this->setting_option_values['payment_methods'] ) ) {
$this->setting_option_values['payment_methods'] = array( 'card' );
}
if ( ! isset( $this->setting_option_values['test_mode'] ) ) {
$this->setting_option_values['test_mode'] = '';
}
}
/**
* Get the default webhook url.
*
* @return string
*/
public static function get_default_stripe_webhook_url() {
return add_query_arg(
array( 'learndash-integration' => 'stripe-connect' ),
esc_url_raw( get_site_url() )
);
}
/**
* Get the stripe webhook URL.
*
* @since 4.0.0
*/
private function get_stripe_webhook_url() {
if ( isset( $this->setting_option_values['webhook_url'] ) && ! empty( $this->setting_option_values['webhook_url'] ) ) {
return $this->setting_option_values['webhook_url'];
}
return self::get_default_stripe_webhook_url();
}
/**
* Initialize the metabox settings fields.
*
* @since 4.0.0
*/
public function load_settings_fields() {
$this->setting_option_fields = array(
'connection_button' => array(
'name' => 'connection_button',
'type' => 'text',
'label' => '',
'value' => null,
'display_callback' => array( $this, 'connection_button' ),
),
'enabled' => array(
'name' => 'enabled',
'type' => 'checkbox-switch',
'label' => esc_html__( 'Active', 'learndash' ),
'value' => $this->setting_option_values['enabled'] ?? '',
'options' => array(
'yes' => '',
'' => '',
),
),
'test_mode' => array(
'name' => 'test_mode',
'label' => esc_html__( 'Test Mode', 'learndash' ),
'help_text' => esc_html__( 'Check this box to enable test mode.', 'learndash' ),
'type' => 'checkbox-switch',
'options' => array(
'1' => '',
'0' => '',
),
'default' => '',
'value' => $this->setting_option_values['test_mode'] ?? 0,
),
'publishable_key_test' => array(
'name' => 'publishable_key_test',
'label' => __( 'Test Publishable Key', 'learndash' ),
'help_text' => __( 'Test publishable key used in test mode.', 'learndash' ),
'type' => 'hidden',
'value' => $this->setting_option_values['publishable_key_test'] ?? '',
),
'secret_key_test' => array(
'name' => 'secret_key_test',
'label' => __( 'Test Secret Key', 'learndash' ),
'help_text' => __( 'Test secret key used in test mode.', 'learndash' ),
'type' => 'hidden',
'value' => $this->setting_option_values['secret_key_test'] ?? '',
),
'publishable_key_live' => array(
'name' => 'publishable_key_live',
'label' => __( 'Live Publishable Key', 'learndash' ),
'help_text' => __( 'Live publishable key used in real transaction.', 'learndash' ),
'type' => 'hidden',
'value' => $this->setting_option_values['publishable_key_live'] ?? '',
),
'secret_key_live' => array(
'name' => 'secret_key_live',
'label' => __( 'Live Secret Key', 'learndash' ),
'help_text' => __( 'Live secret key used in real transaction.', 'learndash' ),
'type' => 'hidden',
'value' => $this->setting_option_values['secret_key_live'] ?? '',
),
'account_id' => array(
'name' => 'account_id',
'label' => __( 'Account Id', 'learndash' ),
'type' => 'hidden',
'value' => $this->setting_option_values['account_id'] ?? '',
),
'payment_methods' => array(
'name' => 'payment_methods',
'label' => __( 'Payment Methods', 'learndash' ),
'help_text' => __( 'Stripe payment methods to be enabled on the site.', 'learndash' ),
'value' => $this->setting_option_values['payment_methods'],
'type' => 'checkbox',
'options' => array(
'card' => __( 'Credit Card', 'learndash' ),
'ideal' => __( 'Ideal', 'learndash' ),
),
),
'return_url' => array(
'name' => 'return_url',
'label' => __( 'Return URL ', 'learndash' ),
'help_text' => __(
'Redirect the user to a specific URL after the purchase. Leave blank to let user remain on the Course page.',
'learndash'
),
'type' => 'text',
'value' => $this->setting_option_values['return_url'] ?? '',
),
'webhook_url' => array(
'name' => 'webhook_url',
'type' => 'text',
'label' => esc_html__( 'Webhook URL', 'learndash' ),
'help_text' => esc_html__( 'Stripe webhook endpoint. You have to add this URL in the webhooks section of your Stripe Dashboard.', 'learndash' ),
'value' => $this->get_stripe_webhook_url(),
'class' => 'regular-text',
),
);
/** This filter is documented in includes/settings/settings-metaboxes/class-ld-settings-metabox-course-access-settings.php */
$this->setting_option_fields = apply_filters( 'learndash_settings_fields', $this->setting_option_fields, $this->settings_section_key );
parent::load_settings_fields();
}
/**
* Filter the section saved values.
*
* @param array $value An array of setting fields values.
* @param array $old_value An array of setting fields old values.
* @param string $settings_section_key Settings section key.
* @param string $settings_screen_id Settings screen ID.
*
* @return array
* @since 4.0.0
*/
public function filter_section_save_fields( $value, $old_value, $settings_section_key, $settings_screen_id ): array {
if ( $settings_section_key !== $this->settings_section_key ) {
return $value;
}
if ( ! isset( $value['enabled'] ) ) {
$value['enabled'] = '';
}
if ( ! isset( $value['payment_methods'] ) ) {
$value['payment_methods'] = array();
}
if ( isset( $_POST['learndash_settings_payments_list_nonce'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
if ( ! is_array( $old_value ) ) {
$old_value = array();
}
foreach ( $value as $value_idx => $value_val ) {
$old_value[ $value_idx ] = $value_val;
}
$value = $old_value;
}
return $value;
}
/**
* Show notices.
*/
public function show_notices() {
// Show Stripe disconnection error.
if ( ! empty( $_GET['ld_stripe_error'] ) && ! empty( $_GET['error_code'] ) && ! empty ( $_GET['error_message'] ) ) { // phpcs:ignore
?>
<div class="notice notice-error is-dismissible">
<p>
<b>
<?php esc_html_e( 'Stripe Error', 'learndash' ); ?>:
</b>
<?php esc_html_e( $_GET['error_message'] ); // phpcs:ignore ?>
</p>
</div>
<?php
}
// Show Stripe connection success and endpoint webhook requirement.
// TODO: Add webhook url via API.
if (
isset( $_GET['ld_stripe_connected'] ) && self::STRIPE_RETURNED_AND_PROCESSED_SUCCESS === intval( $_GET['ld_stripe_connected'] ) && // phpcs:ignore
$this->account_is_connected()
) {
$webhook_title = esc_html__(
'You are connected! Please configure your Stripe webhook to finalize the setup.',
'learndash'
);
$webhook_first_detail = sprintf(
'%1$s %2$s',
__(
'In order for Stripe to function properly, you must add a new Stripe webhook endpoint. To do this please visit the <a href=\'https://dashboard.stripe.com/webhooks\' target=\'_blank\'>Webhooks Section of your Stripe Dashboard</a> and click the <strong>Add endpoint</strong> button and paste the following URL:',
'learndash'
),
"<strong>{$this->get_stripe_webhook_url()}</strong>"
);
$webhook_second_detail = esc_html__(
'Stripe webhooks are required so LearnDash can communicate properly with the payment gateway to confirm payment completion, renewals, and more.',
'learndash'
);
?>
<div class="notice notice-info is-dismissible" style="">
<h1><?php echo $webhook_title; // phpcs:ignore ?></h1>
<p><?php echo $webhook_first_detail; // phpcs:ignore ?></p>
<p><?php echo $webhook_second_detail; // phpcs:ignore ?></p>
</div>
<?php
}
// Show Stripe disconnection success.
if ( isset( $_GET['ld_stripe_disconnected'] ) && self::STRIPE_RETURNED_AND_PROCESSED_SUCCESS === intval( $_GET['ld_stripe_disconnected'] ) ) { // phpcs:ignore
?>
<div class="notice notice-success is-dismissible">
<p>
<?php esc_html_e( 'Stripe disconnected', 'learndash' ); ?>.
</p>
</div>
<?php
}
// Show Stripe Connect button.
if ( $this->is_on_payments_setting_page() && ! $this->account_is_connected() ) {
?>
<div class="notice connect-stripe" style="display: flex; justify-content: space-between; padding: 20px 25px;">
<h1>
<?php esc_html_e( 'Want to accept credit card payments directly on your website?', 'learndash' ); ?>
</h1>
<?php $this->connection_button(); ?>
</div>
<?php
}
}
/**
* Shows Stripe Connect button
*/
public function connection_button(): void {
if ( $this->account_is_connected() ) :
?>
<a href="<?php echo esc_url( $this->generate_disconnect_url() ); ?>"
class="learndash-stripe-connect" title="<?php esc_html_e( 'Disconnect Stripe', 'learndash' ); ?>">
<span class="stripe-logo">
<svg width="15" height="21" viewBox="0 0 15 21" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path d="M6.05469 6.55469C6.05469 5.69531 6.75781 5.34375 7.92969 5.34375C9.64844 5.34375 11.7969 5.85156 13.4766 6.78906V1.55469C11.6406 0.8125 9.80469 0.5 7.92969 0.5C3.4375 0.5 0.429688 2.88281 0.429688 6.82812C0.429688 13 8.86719 11.9844 8.86719 14.6406C8.86719 15.6953 7.96875 16.0078 6.75781 16.0078C4.88281 16.0078 2.5 15.2656 0.664062 14.25V19.25C2.5 20.0703 4.57031 20.5 6.75781 20.5391C11.3672 20.5391 14.5703 18.5469 14.5703 14.5234C14.5703 7.88281 6.05469 9.05469 6.05469 6.55469Z"
fill="white"></path>
</svg>
</span>
<span>
<?php esc_html_e( 'Disconnect Stripe', 'learndash' ); ?>
</span>
</a>
<?php
else :
?>
<a href="<?php echo esc_url( self::generate_connect_url() ); ?>"
class="learndash-stripe-connect" title="<?php esc_html_e( 'Connect Stripe', 'learndash' ); ?>">
<span class="stripe-logo">
<svg width="15" height="21" viewBox="0 0 15 21" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path d="M6.05469 6.55469C6.05469 5.69531 6.75781 5.34375 7.92969 5.34375C9.64844 5.34375 11.7969 5.85156 13.4766 6.78906V1.55469C11.6406 0.8125 9.80469 0.5 7.92969 0.5C3.4375 0.5 0.429688 2.88281 0.429688 6.82812C0.429688 13 8.86719 11.9844 8.86719 14.6406C8.86719 15.6953 7.96875 16.0078 6.75781 16.0078C4.88281 16.0078 2.5 15.2656 0.664062 14.25V19.25C2.5 20.0703 4.57031 20.5 6.75781 20.5391C11.3672 20.5391 14.5703 18.5469 14.5703 14.5234C14.5703 7.88281 6.05469 9.05469 6.05469 6.55469Z"
fill="white"></path>
</svg>
</span>
<span>
<?php esc_html_e( 'Connect Stripe', 'learndash' ); ?>
</span>
</a>
<?php
endif;
}
/**
* Checks if account is already connected.
*
* @return bool
*/
private function account_is_connected(): bool {
return ! empty( $this->setting_option_values['account_id'] );
}
/**
* Checks if payments settings is a current page.
*
* @return bool
*/
private function is_on_payments_setting_page(): bool {
return isset( $_GET['page'] ) && 'learndash_lms_payments' === $_GET['page']; // phpcs:ignore
}
/**
* Generates a connect url.
*
* @param string $return_url The url to return to after connection. Defaults to the current page.
*
* @return string
*/
public static function generate_connect_url( $return_url = '' ): string {
$args = array(
'stripe_action' => 'connect',
'return_url' => rawurlencode( empty( $return_url ) ? home_url() . add_query_arg( array() ) : $return_url ),
);
return add_query_arg(
$args,
esc_url_raw( self::CONNECT_SERVER_URL )
);
}
/**
* Generates Stripe disconnect url.
*
* @return string
*/
private function generate_disconnect_url(): string {
$args = array(
'stripe_action' => 'disconnect',
'stripe_user_id' => $this->setting_option_values['account_id'],
'return_url' => rawurlencode( home_url() . add_query_arg( array() ) ),
);
return add_query_arg(
$args,
esc_url_raw( self::CONNECT_SERVER_URL )
);
}
/**
* Clean up Stripe Connect customer_id metadata from users.
*/
private function cleanup_stripe_connect_customer_id() {
global $wpdb;
$sql = $wpdb->prepare(
"DELETE FROM $wpdb->usermeta
WHERE meta_key IN ( %s, %s )",
self::STRIPE_CUSTOMER_ID_META_KEY,
self::STRIPE_CUSTOMER_ID_META_KEY_TEST
);
$wpdb->query( $sql ); // phpcs:ignore
}
/**
* Handle connection.
*/
private function handle_connection_request(): void {
if (
current_user_can( 'manage_options' ) &&
isset( $_GET['ld_stripe_connected'] ) && self::STRIPE_RETURNED_SUCCESS === intval( $_GET['ld_stripe_connected'] ) // phpcs:ignore
) {
$this->load_settings_values();
// check if account connected is same of last time.
$old_account_id = isset( $this->setting_option_values['last_account_id'] ) ? $this->setting_option_values['last_account_id'] : '';
$new_account_id = isset( $_GET['stripe_user_id'] ) ? sanitize_text_field( wp_unslash( $_GET['stripe_user_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( $old_account_id !== $new_account_id ) {
// clear customer_id metadata from users.
$this->cleanup_stripe_connect_customer_id();
}
$this->setting_option_values['account_id'] = sanitize_text_field( $_GET['stripe_user_id'] ); // phpcs:ignore
$this->setting_option_values['secret_key_live'] = sanitize_text_field( $_GET['stripe_access_token'] ); // phpcs:ignore
$this->setting_option_values['secret_key_test'] = sanitize_text_field( $_GET['stripe_access_token_test'] ); // phpcs:ignore
$this->setting_option_values['publishable_key_live'] = sanitize_text_field( $_GET['stripe_publishable_key'] ); // phpcs:ignore
$this->setting_option_values['publishable_key_test'] = sanitize_text_field( $_GET['stripe_publishable_key_test'] ); // phpcs:ignore
$this->save_settings_values();
$reload_url = remove_query_arg(
array( 'ld_stripe_connected', 'ld_stripe_disconnected', 'stripe_user_id', 'stripe_access_token', 'stripe_access_token_test', 'stripe_publishable_key', 'stripe_publishable_key_test', 'ld_stripe_error', 'error_code', 'error_message' )
);
$reload_url = add_query_arg(
array( 'ld_stripe_connected' => self::STRIPE_RETURNED_AND_PROCESSED_SUCCESS ),
$reload_url
);
learndash_safe_redirect( $reload_url );
}
}
/**
* Handle disconnection.
*/
private function handle_disconnection_request(): void {
if (
current_user_can( 'manage_options' ) &&
( isset( $_GET['ld_stripe_disconnected'] ) && self::STRIPE_RETURNED_SUCCESS === intval( $_GET['ld_stripe_disconnected'] ) )
||
( current_user_can( 'manage_options' ) && isset( $_GET['ld_stripe_error'] ) && 1 === intval( $_GET['ld_stripe_error'] ) && ! isset( $_GET['ld_stripe_disconnected'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
) {
$this->load_settings_values();
$this->setting_option_values['last_account_id'] = isset( $this->setting_option_values['account_id'] ) ? $this->setting_option_values['account_id'] : '';
$this->setting_option_values['account_id'] = '';
$this->setting_option_values['publishable_key_live'] = '';
$this->setting_option_values['secret_key_live'] = '';
$this->setting_option_values['publishable_key_test'] = '';
$this->setting_option_values['secret_key_test'] = '';
$this->save_settings_values();
$reload_url = remove_query_arg( array( 'ld_stripe_connected' ) );
$reload_url = add_query_arg(
array( 'ld_stripe_disconnected' => self::STRIPE_RETURNED_AND_PROCESSED_SUCCESS ),
$reload_url
);
learndash_safe_redirect( $reload_url );
}
}
/**
* Check if Stripe is connected.
*
* @return boolean true if connected, false otherwise.
*/
public static function is_stripe_connected(): bool {
$options = LearnDash_Settings_Section::get_section_settings_all( 'LearnDash_Settings_Section_Stripe_Connect' );
return ( isset( $options['account_id'] ) && ! empty( $options['account_id'] ) );
}
/**
* Return the stripe Webhook notice
*
* @return string
*/
public static function get_stripe_webhook_notice() {
$options = LearnDash_Settings_Section::get_section_settings_all( 'LearnDash_Settings_Section_Stripe_Connect' );
$webhook_url = isset( $options['webhook_url'] ) ? $options['webhook_url'] : self::get_default_stripe_webhook_url();
return sprintf(
'%1$s %2$s',
__(
'In order for Stripe to function properly, you must add a new Stripe webhook endpoint. To do this please visit the <a href=\'https://dashboard.stripe.com/webhooks\' target=\'_blank\'>Webhooks Section of your Stripe Dashboard</a> and click the <strong>Add endpoint</strong> button and paste the following URL:',
'learndash'
),
"<strong>{$webhook_url}</strong>"
);
}
}
add_action(
'learndash_settings_sections_init',
array( LearnDash_Settings_Section_Stripe_Connect::class, 'add_section_instance' )
);
}