-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclass-gf-stripe.php
6382 lines (5469 loc) · 220 KB
/
class-gf-stripe.php
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Gravity Forms Stripe Add-On.
*
* @since 1.0
* @package GravityForms
* @author Rocketgenius
* @copyright Copyright (c) 2009 - 2018, Rocketgenius
*/
defined( 'ABSPATH' ) || die();
// Include the payment add-on framework.
GFForms::include_payment_addon_framework();
/**
* Class GFStripe
*
* Primary class to manage the Stripe add-on.
*
* @since 1.0
*
* @uses GFPaymentAddOn
*/
class GFStripe extends GFPaymentAddOn {
/**
* Contains an instance of this class, if available.
*
* @since 1.0
* @access private
*
* @used-by GFStripe::get_instance()
*
* @var object $_instance If available, contains an instance of this class.
*/
private static $_instance = null;
/**
* Defines the version of the Stripe Add-On.
*
* @since 1.0
* @access protected
*
* @used-by GFStripe::scripts()
*
* @var string $_version Contains the version, defined from stripe.php
*/
protected $_version = GF_STRIPE_VERSION;
/**
* Defines the minimum Gravity Forms version required.
*
* @since 1.0
* @access protected
*
* @var string $_min_gravityforms_version The minimum version required.
*/
protected $_min_gravityforms_version = '1.9.14.17';
/**
* Defines the plugin slug.
*
* @since 1.0
* @access protected
*
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gravityformsstripe';
/**
* Defines the main plugin file.
*
* @since 1.0
* @access protected
*
* @var string $_path The path to the main plugin file, relative to the plugins folder.
*/
protected $_path = 'gravityformsstripe/stripe.php';
/**
* Defines the full path to this class file.
*
* @since 1.0
* @access protected
*
* @var string $_full_path The full path.
*/
protected $_full_path = __FILE__;
/**
* Defines the URL where this Add-On can be found.
*
* @since 1.0
* @access protected
*
* @var string $_url The URL of the Add-On.
*/
protected $_url = 'http://www.gravityforms.com';
/**
* Defines the title of this Add-On.
*
* @since 1.0
* @access protected
*
* @var string $_title The title of the Add-On.
*/
protected $_title = 'Gravity Forms Stripe Add-On';
/**
* Defines the short title of the Add-On.
*
* @since 1.0
* @access protected
*
* @var string $_short_title The short title.
*/
protected $_short_title = 'Stripe';
/**
* Defines if Add-On should use Gravity Forms servers for update data.
*
* @since 1.0
* @access protected
*
* @var bool $_enable_rg_autoupgrade true
*/
protected $_enable_rg_autoupgrade = true;
/**
* Defines if user will not be able to create feeds for a form until a credit card field has been added.
*
* @since 1.0
* @since 3.4 Change the default value to false.
* @access protected
*
* @var bool $_requires_credit_card false.
*/
protected $_requires_credit_card = false;
/**
* Defines if callbacks/webhooks/IPN will be enabled and the appropriate database table will be created.
*
* @since 1.0
* @access protected
*
* @var bool $_supports_callbacks true
*/
protected $_supports_callbacks = true;
/**
* Stripe requires monetary amounts to be formatted as the smallest unit for the currency being used e.g. cents.
*
* @since 1.10.1
* @access protected
*
* @var bool $_requires_smallest_unit true
*/
protected $_requires_smallest_unit = true;
/**
* Defines the capability needed to access the Add-On settings page.
*
* @since 1.4.3
* @access protected
* @var string $_capabilities_settings_page The capability needed to access the Add-On settings page.
*/
protected $_capabilities_settings_page = 'gravityforms_stripe';
/**
* Defines the capability needed to access the Add-On form settings page.
*
* @since 1.4.3
* @access protected
* @var string $_capabilities_form_settings The capability needed to access the Add-On form settings page.
*/
protected $_capabilities_form_settings = 'gravityforms_stripe';
/**
* Defines the capability needed to uninstall the Add-On.
*
* @since 1.4.3
* @access protected
* @var string $_capabilities_uninstall The capability needed to uninstall the Add-On.
*/
protected $_capabilities_uninstall = 'gravityforms_stripe_uninstall';
/**
* Defines the capabilities needed for the Stripe Add-On
*
* @since 1.0
* @access protected
* @var array $_capabilities The capabilities needed for the Add-On
*/
protected $_capabilities = array( 'gravityforms_stripe', 'gravityforms_stripe_uninstall' );
/**
* Holds the custom meta key currently being processed. Enables the key to be passed to the gform_stripe_field_value filter.
*
* @since 2.1.1
* @access protected
*
* @used-by GFStripe::maybe_override_field_value()
*
* @var string $_current_meta_key The meta key currently being processed.
*/
protected $_current_meta_key = '';
/**
* Contains an instance of the Stripe API library, if available.
*
* @since 3.4
* @access protected
* @var GF_Stripe_API $api If available, contains an instance of the Stripe API library.
*/
protected $api = null;
/**
* Enable rate limits to log card errors etc.
*
* @since 3.4
*
* @var bool
*/
protected $_enable_rate_limits = true;
/**
* Whether Add-on framework has settings renderer support or not, settings renderer was introduced in Gravity Forms 2.5
*
* @since 3.8
*
* @var bool
*/
protected $_has_settings_renderer;
/**
* Settings inputs prefix string.
*
* @since 3.8
*
* @var string
*/
protected $_input_prefix = '';
/**
* Settings inputs container prefix string.
*
* @since 3.8
*
* @var string
*/
protected $_input_container_prefix = '';
/**
* Get an instance of this class.
*
* @since 1.0
* @access public
*
* @uses GFStripe
* @uses GFStripe::$_instance
*
* @return object GFStripe
*/
public static function get_instance() {
if ( null === self::$_instance ) {
self::$_instance = new GFStripe();
}
return self::$_instance;
}
/**
* Load the Stripe credit card field.
*
* @since 2.6
*/
public function pre_init() {
// For form confirmation redirection, this must be called in `wp`,
// or confirmation redirect to a page would throw PHP fatal error.
// Run before calling parent method. We don't want to run anything else before displaying thank you page.
add_action( 'wp', array( $this, 'maybe_thankyou_page' ), 5 );
parent::pre_init();
require_once 'includes/class-gf-field-stripe-creditcard.php';
}
/**
* Return the scripts which should be enqueued.
*
* @since 1.0
* @access public
*
* @uses GFPaymentAddOn::scripts()
* @uses GFAddOn::get_base_url()
* @uses GFAddOn::get_short_title()
* @uses GFStripe::$_version
* @uses GFCommon::get_base_url()
* @uses GFStripe::frontend_script_callback()
*
* @return array The scripts to be enqueued.
*/
public function scripts() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$scripts = array(
array(
'handle' => 'stripe.js',
'src' => 'https://js.stripe.com/v2/',
'version' => $this->_version,
'deps' => array(),
'in_footer' => false,
'enqueue' => array(
array( $this, 'stripe_js_callback' ),
),
),
array(
'handle' => 'stripe_v3',
'src' => 'https://js.stripe.com/v3/',
'version' => $this->_version,
'deps' => array(),
'in_footer' => false,
'enqueue' => array(
array( $this, 'stripe_js_v3_callback' ),
),
),
array(
'handle' => 'gforms_stripe_frontend',
'src' => $this->get_base_url() . "/js/frontend{$min}.js",
'version' => $this->_version,
'deps' => array( 'jquery', 'gform_json', 'gform_gravityforms', 'wp-a11y' ),
'in_footer' => false,
'enqueue' => array(
array( $this, 'frontend_script_callback' ),
),
'strings' => array(
'no_active_frontend_feed' => wp_strip_all_tags( __( 'The credit card field will initiate once the payment condition is met.', 'gravityformsstripe' ) ),
'requires_action' => wp_strip_all_tags( __( 'Please follow the instructions on the screen to validate your card.', 'gravityformsstripe' ) ),
'create_payment_intent_nonce' => wp_create_nonce( 'gf_stripe_create_payment_intent' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
),
),
array(
'handle' => 'gforms_stripe_admin',
'src' => $this->get_base_url() . "/js/admin{$min}.js",
'version' => $this->_version,
'deps' => array( 'jquery', 'thickbox', 'stripe.js' ),
'in_footer' => false,
'enqueue' => array(
array(
'admin_page' => array( 'plugin_settings', 'form_settings' ),
'tab' => array( $this->_slug, $this->get_short_title() ),
),
),
'strings' => array(
'spinner' => GFCommon::get_base_url() . '/images/spinner.gif',
'validation_error' => wp_strip_all_tags( __( 'Error validating this key. Please try again later.', 'gravityformsstripe' ) ),
'switch_account_disabled_message' => wp_strip_all_tags( __( 'In order to switch accounts, you must first save this feed by clicking the "Update Settings" button below', 'gravityformsstripe' ) ),
'disconnect' => array(
'site' => wp_strip_all_tags( __( 'Are you sure you want to disconnect from Stripe for this website?', 'gravityformsstripe' ) ),
'feed' => wp_strip_all_tags( __( 'Are you sure you want to disconnect from Stripe for this feed?', 'gravityformsstripe' ) ),
'account' => wp_strip_all_tags( __( 'Are you sure you want to disconnect all Gravity Forms sites connected to this Stripe account?', 'gravityformsstripe' ) ),
),
'settings_url' => admin_url( 'admin.php?page=gf_settings&subview=' . $this->get_slug() ),
'ajax_nonce' => wp_create_nonce( 'gf_stripe_ajax' ),
'liveDependencySupported' => $this->_has_settings_renderer,
'apiMode' => $this->get_api_mode( $this->get_settings() ),
'input_container_prefix' => $this->_input_container_prefix,
'input_prefix' => $this->_input_prefix,
),
),
);
return array_merge( parent::scripts(), $scripts );
}
/***
* Return the styles that need to be enqueued.
*
* @since 2.6
* @since 2.8 Add plugin settings CSS.
*
* @access public
*
* @return array Returns an array of styles and when to enqueue them
*/
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gforms_stripe_frontend',
'src' => $this->get_base_url() . "/css/frontend{$min}.css",
'version' => $this->_version,
'in_footer' => false,
'enqueue' => array(
array( $this, 'frontend_style_callback' ),
),
),
array(
'handle' => 'gform_stripe_pluginsettings',
'src' => $this->get_base_url() . "/css/plugin_settings{$min}.css",
'version' => $this->_version,
'deps' => array( 'thickbox' ),
'enqueue' => array(
array(
'admin_page' => array( 'plugin_settings', 'form_settings' ),
'tab' => $this->_slug,
),
),
),
);
return array_merge( parent::styles(), $styles );
}
// # PLUGIN SETTINGS -----------------------------------------------------------------------------------------------
/**
* Return the plugin's icon for the plugin/form settings menu.
*
* @since 3.8
*
* @return string
*/
public function get_menu_icon() {
return file_get_contents( $this->get_base_path() . '/images/menu-icon.svg' );
}
/**
* Initialize the AJAX hooks.
*
* @since Unknown
* @access public
*
* @uses GFPaymentAddOn::init_ajax()
*
* @return void
*/
public function init_ajax() {
parent::init_ajax();
// Add AJAX callback for de-authorizing from Stripe.
add_action( 'wp_ajax_gfstripe_deauthorize', array( $this, 'ajax_deauthorize' ) );
add_action( 'wp_ajax_gf_validate_secret_key', array( $this, 'ajax_validate_secret_key' ) );
// Add AJAX callback for creating a payment intent.
add_action( 'wp_ajax_nopriv_gfstripe_create_payment_intent', array( $this, 'create_payment_intent' ) );
add_action( 'wp_ajax_gfstripe_create_payment_intent', array( $this, 'create_payment_intent' ) );
add_action( 'wp_ajax_nopriv_gfstripe_update_payment_intent', array( $this, 'update_payment_intent' ) );
add_action( 'wp_ajax_gfstripe_update_payment_intent', array( $this, 'update_payment_intent' ) );
add_action( 'wp_ajax_nopriv_gfstripe_get_country_code', array( $this, 'get_country_code' ) );
add_action( 'wp_ajax_gfstripe_get_country_code', array( $this, 'get_country_code' ) );
}
/**
* Admin initial actions.
*
* @since 2.8
*/
public function init_admin() {
parent::init_admin();
add_action( 'admin_notices', array( $this, 'maybe_display_update_authentication_message' ) );
add_action( 'admin_init', array( $this, 'maybe_update_auth_tokens' ) );
}
/**
* Handler for the gf_validate_secret_key AJAX request.
*
* @since Unknown
* @since 3.3 Fix PHP fatal error thrown when deleting test data.
* @since 3.4 Use GF_Stripe_API class.
*
* @access public
*
* @used-by GFStripe::init_ajax()
* @uses GFStripe::include_stripe_api()
* @uses GFAddOn::log_error()
*
* @return void
*/
public function ajax_validate_secret_key() {
check_ajax_referer( 'gf_stripe_ajax', 'nonce' );
if ( ! GFCommon::current_user_can_any( $this->_capabilities_settings_page ) ) {
wp_send_json_error( array( 'message' => esc_html__( 'Access denied.', 'gravityformsstripe' ) ) );
}
// Load the Stripe API library.
if ( ! class_exists( 'GF_Stripe_API' ) ) {
require_once 'includes/class-gf-stripe-api.php';
}
$stripe = new GF_Stripe_API( rgpost( 'key' ) );
// Initialize validity state.
$is_valid = true;
$account = $stripe->get_account();
if ( is_wp_error( $account ) ) {
$this->log_error( __METHOD__ . '(): ' . $account->get_error_message() );
$is_valid = false;
}
// Prepare response.
$response = $is_valid ? 'valid' : 'invalid';
// Send API key validation response.
die( $response );
}
/**
* Configures the settings which should be rendered on the add-on settings tab.
*
* @since Unknown
* @since 2.6 Add Payment Collection section.
* @since 2.8 Add Stripe Connect. Remove Stripe Webhooks.
*
* @access public
*
* @used-by GFAddOn::maybe_save_plugin_settings()
* @used-by GFAddOn::plugin_settings_page()
* @uses GFStripe::api_settings_fields()
* @uses GFStripe::get_webhooks_section_description()
*
* @return array Plugin settings fields to add.
*/
public function plugin_settings_fields() {
$fields = array(
array(
'title' => esc_html__( 'Stripe Account', 'gravityformsstripe' ),
'fields' => $this->api_settings_fields(),
),
);
if ( version_compare( GFFormsModel::get_database_version(), '2.4-beta-1', '>=' ) ) {
$fields[] = array(
'title' => esc_html__( 'Payment Collection', 'gravityformsstripe' ),
'description' => $this->get_checkout_method_section_description(),
'fields' => array(
array(
'name' => 'checkout_method',
'label' => esc_html__( 'Payment Collection Method', 'gravityformsstripe' ),
'type' => 'radio',
'default_value' => 'stripe_elements',
'choices' => array(
array(
'label' => esc_html__( 'Stripe Credit Card Field (Elements, SCA-ready)', 'gravityformsstripe' ),
'value' => 'stripe_elements',
'tooltip' => '<h6>' . esc_html__( 'Stripe Credit Card Field (Elements)', 'gravityformsstripe' ) . '</h6>' .
'<p>' . esc_html__( 'Select this option to use a Credit Card field hosted by Stripe. This option offers the benefit of a streamlined user interface and the security of having the credit card field hosted on Stripe\'s servers. Selecting this option or "Stripe Payment Form" greatly simplifies the PCI compliance application process with Stripe.', 'gravityformsstripe' ) .
'</p><p>' .
/* translators: 1. Open link tag 2. Close link tag */
sprintf( esc_html__( 'Stripe Elements is ready for %1$sStrong Customer Authentication%2$s for European customers.', 'gravityformsstripe' ), '<a href="https://stripe.com/docs/strong-customer-authentication" target="_blank">', '</a>' ) .
'</p>',
),
array(
'label' => esc_html__( 'Stripe Payment Form (Stripe Checkout, SCA-ready)', 'gravityformsstripe' ),
'value' => 'stripe_checkout',
'tooltip' => '<h6>' . esc_html__( 'Stripe Payment Form', 'gravityformsstripe' ) . '</h6>' .
'<p>' . esc_html__( 'Select this option to collect all payment information in a separate page hosted by Stripe. This option is the simplest to implement since it doesn\'t require a credit card field in your form. Selecting this option or "Stripe Credit Card Field" greatly simplifies the PCI compliance application process with Stripe.', 'gravityformsstripe' ) .
'</p><p>' .
/* translators: 1. Open link tag 2. Close link tag */
sprintf( esc_html__( 'Stripe Checkout also supports Apple Pay and 3D secure, and is ready for %1$sStrong Customer Authentication%2$s for European customers.', 'gravityformsstripe' ), '<a href="https://stripe.com/docs/strong-customer-authentication" target="_blank">', '</a>' ) .
'</p>',
),
),
),
),
);
}
return $fields;
}
/**
* Define the settings which appear in the Stripe API section.
*
* @since 2.8 Add Stripe Connect fields.
* @since Unknown
* @access public
*
* @used-by GFStripe::plugin_settings_fields()
*
* @return array The API settings fields.
*/
public function api_settings_fields() {
// If no ssl certificate found, just return the ssl error message.
if ( ! is_ssl() ) {
return array(
array(
'name' => 'ssl_error',
'type' => 'ssl_error'
)
);
}
$api_mode = '';
if ( rgpost( 'access_token' ) ) {
$api_mode = ( rgpost( 'livemode' ) === true ) ? 'live' : 'test';
} elseif ( $this->is_detail_page() && empty( $api_mode ) ) {
$api_mode = $this->get_plugin_setting( 'api_mode' );
}
$fields = array(
array(
'name' => 'connected_to',
'label' => esc_html__( 'Connected to Stripe as', 'gravityformsstripe' ),
'type' => 'connected_to',
'dependency' => array( $this, 'is_detail_page' ),
'tooltip' => '<h6>' . esc_html__( 'Connected to Stripe as', 'gravityformsstripe' ) . '</h6>' . esc_html__( 'The Stripe account this feed is currently connected to.', 'gravityformsstripe' ),
),
array(
'name' => 'api_mode',
'label' => esc_html__( 'Mode', 'gravityformsstripe' ),
'type' => 'radio',
'default_value' => $api_mode,
'choices' => array(
array(
'label' => esc_html__( 'Live', 'gravityformsstripe' ),
'value' => 'live',
),
array(
'label' => esc_html__( 'Test', 'gravityformsstripe' ),
'value' => 'test',
),
),
'horizontal' => true,
),
array(
'name' => 'live_auth_token',
'type' => 'auth_token_button',
'dependency' => $this->get_auth_button_dependency( 'live' ),
),
array(
'name' => 'test_auth_token',
'type' => 'auth_token_button',
'dependency' => $this->get_auth_button_dependency( 'test' ),
),
array(
'label' => 'hidden',
'name' => 'live_publishable_key_is_valid',
'type' => 'hidden',
),
array(
'label' => 'hidden',
'name' => 'live_secret_key_is_valid',
'type' => 'hidden',
),
array(
'label' => 'hidden',
'name' => 'test_publishable_key_is_valid',
'type' => 'hidden',
),
array(
'label' => 'hidden',
'name' => 'test_secret_key_is_valid',
'type' => 'hidden',
),
);
if ( ! $this->is_detail_page() && ! $this->is_stripe_connect_enabled() ) {
$legacy_connect_fields = array(
array(
'name' => 'test_publishable_key',
'label' => esc_html__( 'Test Publishable Key', 'gravityformsstripe' ),
'type' => 'text',
'class' => 'medium',
'onchange' => "GFStripeAdmin.validateKey('test_publishable_key', this.value);",
),
array(
'name' => 'test_secret_key',
'label' => esc_html__( 'Test Secret Key', 'gravityformsstripe' ),
'type' => 'text',
'input_type' => 'password',
'class' => 'medium',
'onchange' => "GFStripeAdmin.validateKey('test_secret_key', this.value);",
),
array(
'name' => 'live_publishable_key',
'label' => esc_html__( 'Live Publishable Key', 'gravityformsstripe' ),
'type' => 'text',
'class' => 'medium',
'onchange' => "GFStripeAdmin.validateKey('live_publishable_key', this.value);",
),
array(
'name' => 'live_secret_key',
'label' => esc_html__( 'Live Secret Key', 'gravityformsstripe' ),
'type' => 'text',
'input_type' => 'password',
'class' => 'medium',
'onchange' => "GFStripeAdmin.validateKey('live_secret_key', this.value);",
),
);
$fields = array_merge( $fields, $legacy_connect_fields );
} else {
$stripe_connect_fields = array(
array(
'name' => 'test_publishable_key',
'label' => esc_html__( 'Test Publishable Key', 'gravityformsstripe' ),
'type' => 'hidden',
),
array(
'name' => 'test_secret_key',
'label' => esc_html__( 'Test Secret Key', 'gravityformsstripe' ),
'type' => 'hidden',
),
array(
'name' => 'live_publishable_key',
'label' => esc_html__( 'Live Publishable Key', 'gravityformsstripe' ),
'type' => 'hidden',
),
array(
'name' => 'live_secret_key',
'label' => esc_html__( 'Live Secret Key', 'gravityformsstripe' ),
'type' => 'hidden',
),
);
$fields = array_merge( $fields, $stripe_connect_fields );
}
$webhook_fields = array(
array(
'name' => 'webhooks_enabled',
'label' => esc_html__( 'Webhooks Enabled?', 'gravityformsstripe' ),
'type' => 'checkbox',
'horizontal' => true,
'required' => ( $this->get_current_feed_id() && $this->is_feed_stripe_connect_enabled() ) || ! isset( $_GET['fid'] ),
'description' => $this->get_webhooks_section_description(),
'dependency' => $this->get_webhooks_dependency(),
'choices' => array(
array(
'label' => esc_html__( 'I have enabled the Gravity Forms webhook URL in my Stripe account.', 'gravityformsstripe' ),
'value' => 1,
'name' => 'webhooks_enabled',
),
),
),
array(
'name' => 'test_signing_secret',
'label' => esc_html__( 'Test Signing Secret', 'gravityformsstripe' ),
'type' => 'text',
'input_type' => 'password',
'class' => 'medium',
'dependency' => $this->get_webhooks_dependency(),
'validation_callback' => array( $this, 'validate_webhook_signing_secret' ),
),
array(
'name' => 'live_signing_secret',
'label' => esc_html__( 'Live Signing Secret', 'gravityformsstripe' ),
'type' => 'text',
'input_type' => 'password',
'class' => 'medium',
'dependency' => $this->get_webhooks_dependency(),
'validation_callback' => array( $this, 'validate_webhook_signing_secret' ),
),
);
$fields = array_merge( $fields, $webhook_fields );
return $fields;
}
/**
* Generates dependency array for auth token button if live dependencies are supported.
*
* @param string $api_mode The API mode that the button value represents, could be live or test.
*
* @since 3.8
*
* @return array|false
*/
private function get_auth_button_dependency( $api_mode ) {
if ( ! $this->_has_settings_renderer ) {
return false;
}
return array(
'live' => true,
'fields' => array(
array(
'field' => 'api_mode',
'values' => array( $api_mode ),
),
),
);
}
/**
* Generates webhooks fields dependency if required.
*
* @since 3.8
*
* @return array|false
*/
private function get_webhooks_dependency() {
if ( ! $this->is_detail_page() ) {
return false;
}
return array( $this, 'is_feed_stripe_connect_enabled' );
}
/**
* Generates the markup for the SSL error message field.
*
* @param array $field Field properties.
* @param bool $echo Display field contents. Defaults to true.
*
* @since 3.8
*
* @return string
*/
public function settings_ssl_error( $field, $echo = true ) {
$html = $this->_has_settings_renderer ? '<div class="alert gforms_note_error">' : '<div class="alert_red" style="padding:20px; padding-top:5px;">';
$html .= '<h4>' . esc_html__( 'SSL Certificate Required', 'gravityformsstripe' ) . '</h4>';
/* Translators: 1: Open link tag 2: Close link tag */
$html .= sprintf( esc_html__( 'Make sure you have an SSL certificate installed and enabled, then %1$sclick here to reloaad the settings page%2$s.', 'gravityformsstripe' ), '<a href="' . $this->get_settings_page_url() . '">', '</a>' );
$html .= '</div>';
if ( $echo ) {
echo $html;
}
return $html;
}
/**
* Define the markup to be displayed for the webhooks section description.
*
* @since Unknown
* @access public
*
* @used-by GFStripe::plugin_settings_fields()
* @uses GFStripe::get_webhook_url()
*
* @return string HTML formatted webhooks description.
*/
public function get_webhooks_section_description() {
ob_start();
?>
<a href="javascript:void(0);"
onclick="tb_show('Webhook Instructions', '#TB_inline?width=500&inlineId=stripe-webhooks-instructions', '');" onkeypress="tb_show('Webhook Instructions', '#TB_inline?width=500&inlineId=stripe-webhooks-instructions', '');"><?php esc_html_e( 'View Instructions', 'gravityformsstripe' ); ?></a></p>
<div id="stripe-webhooks-instructions" style="display:none;">
<ol class="stripe-webhooks-instructions">
<li>
<?php esc_html_e( 'Click the following link and log in to access your Stripe Webhooks management page:', 'gravityformsstripe' ); ?>
<br/>
<a href="https://dashboard.stripe.com/account/webhooks" target="_blank">https://dashboard.stripe.com/account/webhooks</a>
</li>
<li><?php esc_html_e( 'Click the "Add Endpoint" button above the list of Webhook URLs.', 'gravityformsstripe' ); ?></li>
<li>
<?php esc_html_e( 'Enter the following URL in the "URL to be called" field:', 'gravityformsstripe' ); ?>
<code><?php echo $this->get_webhook_url( $this->get_current_feed_id() ); ?></code>
</li>
<li><?php esc_html_e( 'If offered the choice, select the latest API version.', 'gravityformsstripe' ); ?></li>
<li><?php esc_html_e( 'Click the "receive all events" link.', 'gravityformsstripe' ); ?></li>
<li><?php esc_html_e( 'Click the "Add Endpoint" button to save the webhook.', 'gravityformsstripe' ); ?></li>
<li><?php esc_html_e( 'Copy the signing secret of the newly created webhook on Stripe and paste to the setting field.', 'gravityformsstripe' ); ?></li>
</ol>
</div>
<?php
return ob_get_clean();
}
/**
* Define the markup to be displayed for the Stripe Checkout section description.
*
* @since 2.6
* @since 3.4 Update description about removing CC field support.
*/
public function get_checkout_method_section_description() {
ob_start();
?>
<p><?php esc_html_e( 'Select how payment information will be collected. You can select one of the Stripe hosted solutions (Stripe Credit Card or Stripe Checkout) which simplifies the PCI compliance process with Stripe.', 'gravityformsstripe' ); ?></p>
<p><?php echo sprintf( esc_html__( 'The Gravity Forms Credit Card Field has been deprecated in the Stripe Add-On from version 3.4. Forms that are currently using this field will continue to work. Refer to %1$sthis guide%2$s for more information about this change.', 'gravityformsstripe' ), '<a href="https://docs.gravityforms.com/deprecation-of-the-gravity-forms-credit-card-field/" target="_blank">', '</a>' ); ?></p>
<?php
return ob_get_clean();
}
/**
* Create Connect with Stripe settings field.
*
* @since 2.8
* @access public
*
* @param array $field Field properties.
* @param bool $echo Display field contents. Defaults to true.
*
* @return string
*/
public function settings_auth_token_button( $field, $echo = true ) {
$settings = $this->get_settings();
if ( ! $this->can_display_connect_button( $settings ) ) {
return '';
}
$api_mode = ( $field['name'] === 'live_auth_token' ) ? 'live' : 'test';
// Get authentication URL.
$license_key = GFCommon::get_key();
$auth_url = add_query_arg( array(
'mode' => $api_mode,
'redirect_to' => rawurlencode( $this->get_settings_page_url() ),
'license' => $license_key,
'state' => wp_create_nonce( $this->get_authentication_state_action() ),
), $this->get_gravity_api_url( '/auth/stripe' ) );
// Create connect button markup.
$connect_button = sprintf(
'<a href="%5$s" class="gform_stripe_auth_button"><img alt="%1$s" src="%2$s" srcset="%2$s 1x, %3$s 2x, %4$s 3x"></a>',
esc_html__( 'Click here to authenticate with Stripe', 'gravityformsstripe' ),
$this->get_base_url() . '/images/light-on-dark.png',
$this->get_base_url() . '/images/light-on-dark@2x.png',
$this->get_base_url() . '/images/light-on-dark@3x.png',
$auth_url
);
$deprecated_auth_message = '';
if ( $this->requires_reauthentication() ) {
$deprecated_auth_message = $this->_has_settings_renderer ? '<div class="alert gforms_note_error">' : '<div class="alert_red" style="padding:20px; padding-top:5px; margin-bottom: 20px;">';
$deprecated_auth_message .= '<p>' . esc_html__( 'You are currently logged in to Stripe using a deprecated authentication method.', 'gravityformsstripe' ) . '</p>';
/* Translators: 1: Open strong tag 2: Close strong tag */
$deprecated_auth_message .= '<p>' . sprintf( esc_html__( '%1$sPlease login to your Stripe account via Stripe Connect using the button below.%2$s It is a more secure authentication method and will be required for upcoming features of the Stripe Add-on.', 'gravityformsstripe' ), '<strong>', '</strong>' ) . '</p>';
$deprecated_auth_message .= '</div>';
}
$learn_more_message = '<p>' . sprintf( esc_html__( '%1$sLearn more%2$s about connecting with Stripe.', 'gravityformsstripe' ), '<a target="_blank" href=" https://docs.gravityforms.com/faq-authenticating-with-stripe/">', '</a>' ) . '</p>';
$connect_button = $deprecated_auth_message . $connect_button . $learn_more_message;
$is_valid = $this->is_stripe_auth_valid( $settings, $api_mode );
if ( $is_valid ) {
if ( ! rgblank( $this->get_stripe_user_id( $settings, $api_mode ) ) && ! $this->is_detail_page() ) {
$display_name = $this->get_stripe_display_name( $is_valid );
$deauth_button = sprintf(
' <a href="#" class="button gform_stripe_deauth_button" data-fid="%2$s" data-id="%3$s" data-mode="%4$s">%1$s</a>',
esc_html__( 'Disconnect', 'gravityformsstripe' ),
$this->get_current_feed_id(),
rgget( 'id' ),
$api_mode
);
$html = '<p class="connected_to_stripe_text">';
/* Translators: 1: Open strong tag 2: Account name 3: Close account tag */
if ( ! $this->is_detail_page() && ! empty( $display_name ) ) {
$html .= sprintf(
esc_html__( 'Connected to Stripe as %1$s%2$s%3$s.', 'gravityformsstripe' ),
'<strong>',
$display_name,
'</strong>'
);
}
$html .= ' ' . $deauth_button;
$html .= '</p>';
// Display the deauth options.
$html .= '<div class="alert_red deauth_scope" style="margin-top: 10px;padding:20px; padding-top:5px;">';
$html .= '<p><label for="' . $api_mode . '_deauth_scope0"><input type="radio" name="deauth_scope" value="site" id="' . $api_mode . '_deauth_scope0" checked="checked">';
$target = $this->is_detail_page() ? 'feed' : 'site';
$html .= sprintf( esc_html__( 'Disconnect this %s only', 'gravityformsstripe' ), $target );
$html .= '</label></p>';
$html .= '<p><label for="' . $api_mode . '_deauth_scope1"><input type="radio" name="deauth_scope" value="account" id="' . $api_mode . '_deauth_scope1">' . esc_html__( 'Disconnect all Gravity Forms sites connected to this Stripe account', 'gravityformsstripe' ) . '</label></p>';
$html .= $deauth_button;
$html .= '</div>';
} else {
$html = $connect_button;
}
} else {