-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-gf-ppcp.php
4106 lines (3547 loc) · 141 KB
/
class-gf-ppcp.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
defined( 'ABSPATH' ) || die();
// Include the Gravity Forms Payment Add-On Framework.
GFForms::include_payment_addon_framework();
use \Gravity_Forms\Gravity_Forms_PPCP\API\Message_Parser;
use Gravity_Forms\Gravity_Forms_PPCP\PayPal_Subscriptions_Handler;
use Gravity_Forms\Gravity_Forms_PPCP\Subscriptions\Plans;
use Gravity_Forms\Gravity_Forms_PPCP\Models\Plan;
use Gravity_Forms\Gravity_Forms_PPCP\Models\Subscription;
/**
* Gravity Forms Gravity Forms PayPal Checkout Add-On.
*
* @since 1.0
* @package GravityForms
* @author Rocketgenius
* @copyright Copyright (c) 2019, Rocketgenius
*/
class GF_PPCP extends GFPaymentAddOn {
/**
* Contains an instance of this class, if available.
*
* @since 1.0
* @var GF_PPCP $_instance If available, contains an instance of this class
*/
private static $_instance = null;
/**
* Defines the version of the Gravity Forms PayPal Checkout Add-On.
*
* @since 1.0
* @var string $_version Contains the version.
*/
protected $_version = GF_PPCP_VERSION;
/**
* Defines the minimum Gravity Forms version required.
*
* @since 1.0
* @var string $_min_gravityforms_version The minimum version required.
*/
protected $_min_gravityforms_version = GF_PPCP_MIN_GF_VERSION;
/**
* Defines the plugin slug.
*
* @since 1.0
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gravityformsppcp';
/**
* Defines the main plugin file.
*
* @since 1.0
* @var string $_path The path to the main plugin file, relative to the plugins folder.
*/
protected $_path = 'gravityformsppcp/ppcp.php';
/**
* Defines the full path to this class file.
*
* @since 1.0
* @var string $_full_path The full path.
*/
protected $_full_path = __FILE__;
/**
* Defines the URL where this add-on can be found.
*
* @since 1.0
* @var string The URL of the Add-On.
*/
protected $_url = 'http://gravityforms.com';
/**
* Defines the title of this add-on.
*
* @since 1.0
* @var string $_title The title of the add-on.
*/
protected $_title = 'Gravity Forms PayPal Checkout Add-On';
/**
* Defines the short title of the add-on.
*
* @since 1.0
* @var string $_short_title The short title.
*/
protected $_short_title = 'PayPal Checkout';
/**
* Defines if Add-On should use Gravity Forms servers for update data.
*
* @since 1.0
* @access protected
* @var bool
*/
protected $_enable_rg_autoupgrade = true;
/**
* 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;
/**
* If true, feeds w/ conditional logic will evaluated on the frontend and a JS event will be triggered when the feed
* is applicable and inapplicable.
*
* @since 1.0
* @access protected
*
* @var bool
*/
protected $_supports_frontend_feeds = true;
/**
* Defines the capabilities needed for the Gravity Forms PayPal Checkout Add-On
*
* @since 1.0
* @access protected
* @var array $_capabilities The capabilities needed for the Add-On
*/
protected $_capabilities = array( 'gravityforms_ppcp', 'gravityforms_ppcp_uninstall' );
/**
* Defines the capability needed to access the Add-On settings page.
*
* @since 1.0
* @access protected
* @var string $_capabilities_settings_page The capability needed to access the Add-On settings page.
*/
protected $_capabilities_settings_page = 'gravityforms_ppcp';
/**
* Defines the capability needed to access the Add-On form settings page.
*
* @since 1.0
* @access protected
* @var string $_capabilities_form_settings The capability needed to access the Add-On form settings page.
*/
protected $_capabilities_form_settings = 'gravityforms_ppcp';
/**
* Defines the capability needed to uninstall the Add-On.
*
* @since 1.0
* @access protected
* @var string $_capabilities_uninstall The capability needed to uninstall the Add-On.
*/
protected $_capabilities_uninstall = 'gravityforms_ppcp_uninstall';
/**
* Contains an instance of the PayPal Checkout API library, if available.
*
* @since 1.0
* @var GF_PPCP_API $api If available, contains an instance of the PayPal Checkout API library.
*/
protected $api;
/**
* Subscriptions_Handler instance.
*
* @since 2.0
*
* @var PayPal_Subscriptions_Handler
*/
protected $subscriptions_handler;
/**
* Defines the styles to load in the form preview.
*
* @since 2.4
* @access private
* @var array $_preview_styles Styles to load in the form preview.
*/
private $_preview_styles = array( 'gform_ppcp_frontend' );
protected $_enable_theme_layer = true;
/**
* Returns an instance of this class, and stores it in the $_instance property.
*
* @since 1.0
*
* @return GF_PPCP $_instance An instance of the GF_PPCP class
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new GF_PPCP();
}
return self::$_instance;
}
/**
* Load the PayPal field.
*
* @since 1.0
*/
public function pre_init() {
parent::pre_init();
require_once 'includes/class-gf-field-paypal.php';
}
/**
* Register initialization hooks.
*
* @since 1.0
*/
public function init() {
parent::init();
add_action( 'script_loader_src', array( $this, 'remove_args' ), 10, 2 );
add_action( 'script_loader_tag', array( $this, 'add_custom_data' ), 10, 2 );
add_filter( 'gform_register_init_scripts', array( $this, 'register_init_scripts' ), 10, 3 );
add_filter( 'gform_submit_button', array( $this, 'add_smart_payment_buttons' ), 10, 2 );
add_filter( 'gform_form_tag', array( $this, 'add_ppcp_inputs' ), 10, 2 );
add_filter( 'gform_pre_submission', array( $this, 'populate_credit_card_fields' ) );
add_filter( 'gform_field_css_class', array( $this, 'paypal_field_css_class' ), 10, 3 );
add_filter( 'gform_field_content', array( $this->get_subscriptions_handler(), 'add_paypal_checkout_subscription_id' ), 10, 5 );
add_action( 'gform_field_standard_settings', array( 'GF_Field_PayPal', 'payment_methods_standard_settings' ) );
add_action( 'gform_field_appearance_settings', array( 'GF_Field_PayPal', 'payment_methods_appearance_settings' ) );
add_filter( 'gform_tooltips', array( 'GF_Field_PayPal', 'add_tooltips' ) );
}
/**
* Register admin initialization hooks.
*
* @since 1.0
*/
public function init_admin() {
parent::init_admin();
// Add a PayPal Checkout feed if the saved form has the PayPal field.
add_filter( 'gform_after_save_form', array( $this, 'maybe_add_feed' ), 10, 2 );
add_action( 'admin_notices', array( $this, 'maybe_display_account_status_notices' ) );
// Add Payment details action buttons.
add_action( 'gform_payment_details', array( $this, 'maybe_add_payment_details_button' ), 10, 2 );
// Reset Plan ID and Product ID after feed save.
add_action( 'gform_post_save_feed_settings', array( $this->get_subscriptions_handler(), 'maybe_reset_feed_subscription_data' ), 10, 4 );
// Save trial meta data.
add_action( 'gform_post_save_feed_settings', array( $this->get_subscriptions_handler(), 'save_trial_meta_data' ), 10, 4 );
}
/**
* Register AJAX callbacks.
*
* @since 1.0
*/
public function init_ajax() {
parent::init_ajax();
// Finish the seller onboarding process and get the seller's client id and secret.
add_action( 'wp_ajax_gfppcp_onboarding', array( $this, 'ajax_onboarding' ) );
add_action( 'wp_ajax_gfppcp_disconnect', array( $this, 'ajax_disconnect' ) );
add_action( 'wp_ajax_nopriv_gfppcp_get_order_data', array( $this, 'ajax_get_order_data' ) );
add_action( 'wp_ajax_gfppcp_get_order_data', array( $this, 'ajax_get_order_data' ) );
add_action( 'wp_ajax_nopriv_gfppcp_create_order', array( $this, 'ajax_create_order' ) );
add_action( 'wp_ajax_gfppcp_create_subscription', array( $this, 'ajax_create_subscription' ) );
add_action( 'wp_ajax_nopriv_gfppcp_create_subscription', array( $this, 'ajax_create_subscription' ) );
add_action( 'wp_ajax_gfppcp_create_order', array( $this, 'ajax_create_order' ) );
add_action( 'wp_ajax_gfppcp_payment_details_action', array( $this, 'payment_details_action_handler' ) );
add_action( 'wp_ajax_nopriv_gfppcp_get_country_code', array( $this, 'ajax_get_country_code' ) );
add_action( 'wp_ajax_gfppcp_get_country_code', array( $this, 'ajax_get_country_code' ) );
}
/**
* Display the PayPal Connect message.
*
* @since 1.0
*
* @param string $plugin_name The plugin filename. Immediately overwritten.
* @param array $plugin_data An array of plugin data.
*/
public function plugin_row( $plugin_name, $plugin_data ) {
parent::plugin_row( $plugin_name, $plugin_data );
$settings = $this->get_plugin_setting( $this->get_environment() );
$credentials = rgar( $settings, 'credentials' );
if ( empty( $credentials ) ) {
$message = sprintf(
// translators: %1$s opens link tag, Second %2$s closes link tag.
esc_html__( '%1$sConnect your PayPal account%2$s to start accepting payments on your website.', 'gravityformsppcp' ),
"<a href='" . admin_url( 'admin.php?page=gf_settings&subview=' . $this->get_slug() ) . "'>",
'</a>'
);
self::display_plugin_message( $message, false );
}
}
/**
* Add account status warning messages.
*
* @since 1.0
*/
public function maybe_display_account_status_notices() {
$classes = 'notice notice-error';
$seller = $this->is_seller_onboarded();
if ( false === $seller ) {
return;
}
$credit_card_support = $this->is_custom_card_fields_supported( $seller );
$message = '';
if ( ! $this->initialize_api() ) {
if ( ! rgar( $seller, 'primary_email_confirmed' ) ) {
$message = sprintf(
/* translators: 1: Open link tag 2: Close link tag */
esc_html__( 'PayPal has sent a confirmation email to your email address. You need to confirm your email to use the Gravity Forms PayPal Checkout add-on. %1$sLearn more here%2$s.', 'gravityformsppcp' ),
'<a href="https://docs.gravityforms.com/setting-up-the-paypal-commerce-platform-add-on/#h-email-confirmed" target="_blank">',
'</a>'
);
} elseif ( ! rgar( $seller, 'payments_receivable' ) ) {
$message = sprintf(
/* translators: 1: Open link tag 2: Close link tag */
esc_html__( 'Your PayPal account currently cannot receive payments. Please contact PayPal for more details. %1$sLearn more here%2$s.', 'gravityformsppcp' ),
'<a href="https://docs.gravityforms.com/setting-up-the-paypal-commerce-platform-add-on/#h-payment-receivable" target="_blank">',
'</a>'
);
}
} elseif ( $credit_card_support !== false && ( $credit_card_support === 'IN_REVIEW' || $credit_card_support === 'PENDING' ) ) {
$message = sprintf(
/* translators: 1: Open link tag 2: Close link tag */
esc_html__( 'PayPal is currently reviewing your application to enable the Credit Card Field feature. You can %1$sset up the PayPal Checkout add-on%2$s and start with PayPal Checkout first. Once the application is approved, the Credit Card Field will be enabled automatically.', 'gravityformsppcp' ),
'<a href="' . admin_url( 'admin.php?page=gf_settings&subview=' . $this->_slug ) . '">',
'</a>'
);
}
$environment = $this->get_environment();
$settings = $this->get_plugin_settings();
if ( ! empty( $message ) ) {
// Set the seller_onboarded to false so we will check the seller info on each API call.
$settings[ $environment ]['seller_onboarded'] = false;
$this->update_plugin_settings( $settings );
/* translators: 1: CSS classes 2: The message text */
echo sprintf( '<div class="%1$s"><p>%2$s</p></div>', $classes, $message );
} else {
if ( ! rgars( $settings, $environment . '/seller_onboarded' ) ) {
$settings[ $environment ]['seller_onboarded'] = true;
$this->update_plugin_settings( $settings );
}
}
}
/**
* Register PayPal Checkout script when displaying form.
*
* @since 1.0
*
* @param array $form Form object.
* @param array $field_values Current field values. Not used.
* @param bool $is_ajax If form is being submitted via AJAX.
*
* @return void
*/
public function register_init_scripts( $form, $field_values, $is_ajax ) {
if ( ! $this->frontend_script_callback( $form ) ) {
return;
}
// Initialize PayPal Checkout script.
$args = array(
'formId' => $form['id'],
'isAjax' => $is_ajax,
'currency' => GFCommon::get_currency(),
'feeds' => array(),
'smartPaymentButtons' => array(
'buttonsLayout' => $this->get_smart_payment_buttons_default( 'layout' ),
'buttonsSize' => $this->get_smart_payment_buttons_default( 'size' ),
'buttonsShape' => $this->get_smart_payment_buttons_default( 'shape' ),
'buttonsColor' => $this->get_smart_payment_buttons_default( 'color' ),
'planID' => false,
),
'pageInstance' => isset( $form['page_instance'] ) ? $form['page_instance'] : 0,
);
if ( $this->has_paypal_field( $form ) ) {
$cc_field = $this->get_paypal_field( $form );
$args['smartPaymentButtons']['buttonsLayout'] = $cc_field->buttonsLayout;
$args['smartPaymentButtons']['buttonsSize'] = $cc_field->buttonsSize;
$args['smartPaymentButtons']['buttonsShape'] = $cc_field->buttonsShape;
$args['smartPaymentButtons']['buttonsColor'] = $cc_field->buttonsColor;
$args['displayCreditMessages'] = isset( $cc_field->displayCreditMessages ) ? $cc_field->displayCreditMessages : $this->get_smart_payment_buttons_default( 'displayCreditMessages' );
$args['ccFieldId'] = $cc_field->id;
$args['ccPage'] = $cc_field->pageNumber;
$args['paymentMethods'] = $this->is_custom_card_fields_supported() ? $cc_field->methods : array( 'PayPal Checkout' );
$card_style = array(
'input' => array(
'font-size' => '1em',
),
':focus' => array(
'color' => 'black',
),
);
$args['cardStyle'] = apply_filters( 'gform_ppcp_card_style', $card_style, $form['id'] );
}
// Get feed data.
$feeds = $this->get_active_feeds( $form['id'] );
foreach ( $feeds as $feed ) {
$feed_settings = array(
'feedId' => $feed['id'],
'feedName' => rgars( $feed, 'meta/feedName' ),
'first_name' => rgars( $feed, 'meta/billingInformation_first_name' ),
'last_name' => rgars( $feed, 'meta/billingInformation_last_name' ),
'email' => rgars( $feed, 'meta/billingInformation_email' ),
'address_line1' => rgars( $feed, 'meta/billingInformation_address' ),
'address_line2' => rgars( $feed, 'meta/billingInformation_address2' ),
'address_city' => rgars( $feed, 'meta/billingInformation_city' ),
'address_state' => rgars( $feed, 'meta/billingInformation_state' ),
'address_zip' => rgars( $feed, 'meta/billingInformation_zip' ),
'address_country' => rgars( $feed, 'meta/billingInformation_country' ),
'no_shipping' => rgars( $feed, 'meta/no_shipping' ),
);
// By default, feed has all payment choices enabled.
$feed_settings['supportedPaymentMethods'] = GF_Field_PayPal::get_choices();
if ( $this->get_subscriptions_handler()->is_subscription_feed( $feed ) ) {
$feed_settings['intent'] = 'subscription';
$feed_settings['paymentAmount'] = rgars( $feed, 'meta/recurringAmount' );
$feed_settings['billingCycleLength'] = rgars( $feed, 'meta/billingCycle_length' );
$feed_settings['billingCylcleUnit'] = rgars( $feed, 'meta/billingCycle_unit' );
$feed_settings['recurringTimes'] = rgars( $feed, 'meta/recurringTimes' );
if ( rgars( $feed, 'meta/setupFee_enabled' ) ) {
$feed_settings['setupFee'] = rgars( $feed, 'meta/setupFee_product' );
}
if ( rgars( $feed, 'meta/trial_enabled' ) ) {
$product = rgars( $feed, 'meta/trial_product' );
if ( 'enter_amount' === $product && rgars( $feed, 'meta/trial_amount' ) ) {
$feed_settings['trial'] = rgars( $feed, 'meta/trial_amount' );
} else {
$feed_settings['trial'] = $product;
}
}
// Not all payment options are supported for subscriptions.
$feed_settings['supportedPaymentMethods'] = $this->get_subscriptions_handler()->get_supported_payment_methods( $form );
} elseif ( rgars( $feed, 'meta/transactionType' ) === 'product' ) {
$feed_settings['paymentAmount'] = rgars( $feed, 'meta/paymentAmount' );
$feed_settings['intent'] = $this->get_intent( $form['id'], $feed['id'] );
}
$args['feeds'][] = $feed_settings;
}
$args = apply_filters( 'gform_ppcp_object', $args, $form['id'] );
$script = 'new GFPPCP( ' . json_encode( $args, JSON_FORCE_OBJECT ) . ' );';
// Add PayPal Checkout script to form scripts.
GFFormDisplay::add_init_script( $form['id'], 'ppcp', GFFormDisplay::ON_PAGE_RENDER, $script );
}
/**
* Use wp_query to get forms on a page.
*
* @since 2.1
*
* @return array
*/
private function get_forms_from_posts( array $posts ) {
$forms = array();
foreach ( $posts as $post ) {
$found_forms = array();
$found_blocks = array();
GFFormDisplay::parse_forms( $post->post_content, $found_forms, $found_blocks );
$forms = array_merge( $forms, array_keys( $found_forms ) );
}
return array_map(
function( $form_id ) {
return GFAPI::get_form( $form_id );
},
$forms
);
}
/**
* Get the posts in the current query.
*
* @since 2.1
*
* @return array
*/
private function get_wp_query_posts() {
global $wp_query;
if ( ! isset( $wp_query->posts ) || ! is_array( $wp_query->posts ) ) {
return array();
}
return array_filter(
$wp_query->posts,
function( $post ) {
return $post instanceof WP_Post;
}
);
}
/**
* Get the current forms on the screen.
*
* @since 2.1
*
* @return array
*/
private function get_screen_forms() {
if ( ! ( is_admin() || $this->is_preview() ) ) {
$forms = $this->get_forms_from_posts( $this->get_wp_query_posts() );
// If we don't get any forms, try to get the form from the $post in case we're on a conversational form page.
if ( empty( $forms ) ) {
global $post;
if ( $post ) {
$post_meta = get_post_meta( $post->ID );
if ( rgar( $post_meta, 'gf_form_id' ) ) {
$forms = array( GFAPI::get_form( $post_meta['gf_form_id'][0] ) );
}
}
}
return $forms;
}
$form = $this->get_current_form();
return is_array( $form ) ? array( $form ) : array();
}
/**
* Gets all of the localized strings for each form in a set of forms.
*
* @since 2.1
*
* @param array $forms The collection of forms needing localized strings.
*/
private function get_form_strings_for_scripts( array $forms ) {
$data = array_map(
function( $form ) {
return array(
'id' => rgar( $form, 'id' ),
'has_feed' => $this->get_subscriptions_handler()->has_subscriptions_feed( $form ) ? 'true' : 'false',
'show_notice' => $this->get_subscriptions_handler()->supports_form_selected_payment_methods( $form ) ? 'false' : 'true',
'supported_methods' => $this->get_subscriptions_handler()->get_supported_payment_methods( $form ),
);
},
$forms
);
return array_column( $data, null, 'id' );
}
/**
* Register scripts.
*
* @since 1.0
*
* @return array
*/
public function scripts() {
$forms = $this->get_screen_forms();
$form_strings = $this->get_form_strings_for_scripts( $forms );
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$settings = $this->get_plugin_setting( $this->get_environment() );
$forms_with_feeds = array_filter(
$form_strings,
function( $data ) {
return 'true' === rgar( $data, 'has_feed' );
}
);
$client_id = rgars( $settings, 'credentials/client_id' );
$args = array(
'components' => 'hosted-fields,buttons,messages',
// When using "hosted-fields,buttons", the standard card fields will be disabled.
'client-id' => $client_id,
'currency' => GFCommon::get_currency(),
'integration-date' => date( 'Y-m-d' ),
'vault' => 'false',
);
$first_form_on_page = count( $forms ) <= 0 ? array() : $forms[0];
$paypal_fields = GFAPI::get_fields_by_type( $first_form_on_page, 'paypal' );
if ( rgars( $paypal_fields, '0/paypalPaymentButtons' ) ) {
$args['enable-funding'] = implode( ',', array_keys( $this->get_enabled_funding_sources() ) );
}
// Disable all funding by default.
$disabled_funding = self::get_disable_funding();
if ( ! empty( $disabled_funding ) ) {
$args['disable-funding'] = $disabled_funding;
}
$js_sdk_src = $client_id ? add_query_arg(
$args,
'https://www.paypal.com/sdk/js'
) : '';
$scripts = array(
array(
'handle' => 'paypal_partner_js',
'src' => 'https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js',
'enqueue' => array(
array( $this, 'onboarding_script_callback' ),
),
'in_footer' => true, // need to put in footer or the mini-browser couldn't work.
),
array(
'handle' => 'gform_paypal_sdk',
'src' => $js_sdk_src,
),
array(
'handle' => 'gform_ppcp_pluginsettings',
'deps' => array( 'jquery' ),
'src' => $this->get_base_url() . "/assets/js/dist/plugin_settings{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => array( 'plugin_settings' ),
'tab' => $this->_slug,
),
),
'strings' => array(
'is_legacy_markup' => $this->is_gravityforms_supported( '2.5-beta' ) ? 'false' : 'true',
'prefixes' => array(
'input' => $this->is_gravityforms_supported( '2.5-beta' ) ? '_gform_setting_' : '_gaddon_setting_',
'field' => $this->is_gravityforms_supported( '2.5-beta' ) ? 'gform_setting_' : 'gaddon-setting-row-',
),
'onboarding_nonce' => wp_create_nonce( 'gf_ppcp_onboarding' ),
'disconnect_nonce' => wp_create_nonce( 'gf_ppcp_disconnect' ),
'onboarding_error' => wp_strip_all_tags( __( 'We cannot finish the onboarding process. Please try again later.', 'gravityformsppcp' ) ),
'settings_url' => admin_url( 'admin.php?page=gf_settings&subview=' . $this->get_slug() ),
'unauthorized_url' => $this->get_gravity_api_url( '/auth/paypal/unauthorized/?license=' . GFCommon::get_key() . '&redirect_url=' . urlencode( admin_url( 'admin.php?page=gf_settings&subview=gravityformsppcp' ) ) ),
'retry_auth' => rgget( 'retry_auth' ) ? wp_strip_all_tags( __( 'There was an error while connecting with PayPal. Please try again.', 'gravityformsppcp' ) ) : '',
'disconnect' => array(
'site' => wp_strip_all_tags( __( 'Are you sure you want to disconnect from PayPal for this website?', 'gravityformsppcp' ) ),
'feed' => wp_strip_all_tags( __( 'Are you sure you want to disconnect from PayPal for this feed?', 'gravityformsppcp' ) ),
'account' => wp_strip_all_tags( __( 'Are you sure you want to disconnect all Gravity Forms sites connected to this PayPal account?', 'gravityformsppcp' ) ),
),
'loader_text' => wp_strip_all_tags( __( 'Please wait while we complete the onboarding process with PayPal. This may take a few seconds.', 'gravityformsppcp' ) ),
),
),
array(
'handle' => 'gform_ppcp_entry',
'deps' => array( 'jquery' ),
'src' => $this->get_base_url() . "/assets/js/dist/entry_detail{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => array( 'entry_view' ),
'tab' => $this->_slug,
),
),
'strings' => array(
'payment_details_action_error' => wp_strip_all_tags( __( 'Cannot complete request, please contact us for further assistance.', 'gravityformsppcp' ) ),
'payment_details_action_nonce' => wp_create_nonce( 'payment_details_action_nonce' ),
'refund_confirmation' => wp_strip_all_tags( __( 'Are you sure you want to refund this payment?', 'gravityformsppcp' ) ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
),
),
array(
'handle' => 'gform_ppcp_form_settings',
'deps' => array( 'jquery' ),
'src' => $this->get_base_url() . "/assets/js/dist/form_settings{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => array( 'form_settings' ),
'tab' => $this->_slug,
),
),
'strings' => array(
'unsupported_payment_option_message' => wp_strip_all_tags( $this->unsupported_payment_option_message() ),
'is_legacy' => $this->is_gravityforms_supported( '2.5-beta' ) ? 'false' : 'true',
'form_data' => $form_strings,
),
),
array(
'handle' => 'gform_ppcp_form_editor',
'deps' => array( 'jquery', 'gform_paypal_sdk' ),
'src' => $this->get_base_url() . "/assets/js/dist/form_editor{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_editor' ) ),
),
'strings' => array(
'unsupported_payment_option_message' => wp_strip_all_tags( $this->unsupported_payment_option_message() ),
'is_legacy' => $this->is_gravityforms_supported( '2.5-beta' ) ? 'false' : 'true',
'initialize_api' => $this->initialize_api(),
'is_custom_card_fields_supported' => $this->is_custom_card_fields_supported(),
'active' => wp_strip_all_tags( __( 'Active', 'gravityformsppcp' ) ),
'inactive' => wp_strip_all_tags( __( 'Inactive', 'gravityformsppcp' ) ),
'show' => wp_strip_all_tags( __( 'Show', 'gravityformsppcp' ) ),
'imgurl' => GFCommon::get_base_url() . '/images/',
'must_have_method' => wp_strip_all_tags( __( 'At least one payment method should be selected.', 'gravityformsppcp' ) ),
'only_one_paypal_field' => wp_strip_all_tags( __( 'Only one PayPal field can be added to the form', 'gravityformsppcp' ) ),
'form_data' => $form_strings,
),
),
array(
'handle' => 'gforms_ppcp_frontend',
'src' => $this->get_base_url() . "/assets/js/dist/frontend{$min}.js",
'version' => $this->_version,
'deps' => array( 'jquery', 'gform_json', 'gform_gravityforms', 'gform_paypal_sdk', 'wp-a11y' ),
'in_footer' => false,
'enqueue' => array(
array( $this, 'frontend_script_callback' ),
),
'strings' => array(
'card_not_supported' => wp_strip_all_tags( __( 'is not supported. Please enter one of the supported credit cards.', 'gravityformsppcp' ) ),
'card_info_error' => wp_strip_all_tags( __( 'Your credit card information is invalid. Please check it and try again.', 'gravityformsppcp' ) ),
'threed_secure_error' => wp_strip_all_tags( __( 'You have not passed the 3-D secure authentication. Please try again.', 'gravityformsppcp' ) ),
'skipped_by_buyer' => wp_strip_all_tags( __( 'Are you sure to skip the 3-D secure authentication?', 'gravityformsppcp' ) ),
'catch_all_error' => wp_strip_all_tags( __( 'An error occurred, please try again.', 'gravityformsppcp' ) ),
'on_approve_nonce' => wp_create_nonce( 'gf_ppcp_on_approve_nonce' ),
'create_order_nonce' => wp_create_nonce( 'gf_ppcp_create_order_nonce' ),
'create_subscription_nonce' => wp_create_nonce( 'gf_ppcp_create_subscription_nonce' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'currencies' => RGCurrency::get_currencies(),
),
),
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Register styles.
*
* @since 1.0
*
* @return array
*/
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gform_ppcp_pluginsettings',
'src' => $this->get_base_url() . "/assets/css/dist/admin{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => array( 'plugin_settings', 'form_settings', 'form_editor' ),
'tab' => $this->_slug,
),
),
),
array(
'handle' => 'gform_ppcp_frontend',
'src' => $this->get_base_url() . "/assets/css/dist/theme{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => 'form_editor',
'tab' => $this->_slug,
),
array( $this, 'frontend_script_callback' ),
),
),
);
return array_merge( parent::styles(), $styles );
}
/**
* Returns the message displayed when the add-on is not connected.
*
* @since 3.0
*
* @return string
*/
public function configure_addon_message() {
return str_replace( 'href', 'target="_blank" href', parent::configure_addon_message() );
}
/**
* Returns the message displayed when a selected payment method is not compatible with current feed or field settings.
*
* @since 3.0
*
* @return string
*/
public function unsupported_payment_option_message() {
return __( 'Credit card fields are not currently supported for subscriptions and will not display on your form, but the user can still pay using the card option via the PayPal Checkout Modal. ', 'gravityformsppcp' );
}
/**
* An array of styles to enqueue.
*
* @since 2.5
*
* @param $form
* @param $ajax
* @param $settings
* @param $block_settings
*
* @return array|\string[][]
*/
public function theme_layer_styles( $form, $ajax, $settings, $block_settings = array() ) {
$theme_slug = \GFFormDisplay::get_form_theme_slug( $form );
if ( $theme_slug !== 'orbital' ) {
return array();
}
$base_url = plugins_url( '', __FILE__ );
return array(
'foundation' => array(
array( 'gravity_forms_ppcp_theme_foundation', "$base_url/assets/css/dist/theme-foundation.css" ),
),
'framework' => array(
array( 'gravity_forms_ppcp_theme_framework', "$base_url/assets/css/dist/theme-framework.css" ),
),
);
}
/**
* Styles to pass to the Stripe JS widget as part of its CSS properties object.
*
* @since 2.5
*
* @param $form_id
* @param $settings
* @param $block_settings
*
* @return array
*/
public function theme_layer_third_party_styles( $form_id, $settings, $block_settings ) {
if ( ! $this->has_paypal_field() ) {
return array();
}
$default_settings = \GFForms::get_service_container()->get( \Gravity_Forms\Gravity_Forms\Form_Display\GF_Form_Display_Service_Provider::BLOCK_STYLES_DEFAULTS );
$applied_settings = wp_parse_args( $block_settings, $default_settings );
if ( $applied_settings['theme'] !== 'orbital' ) {
return array();
}
/*
NOTE:
The Theme Framework CSS API properties with the "--gform-theme" prefix are deprecated, and
the CSS API properties with the "--gf" prefix are the updated properties.
Deprecated version (core): 2.8
End of support version (core): 2.9
Deprecated version (ppcp): 2.9.1
*/
if ( version_compare( GFForms::$version, '2.8.0-beta-1', '<' ) ) {
return array(
'input' => array(
'color' => '--gform-theme-control-color',
'font-family' => '--gform-theme-control-font-family',
'font-size' => '--gform-theme-control-font-size',
'font-style' => '--gform-theme-control-font-style',
'font-weight' => '--gform-theme-control-font-weight',
'letter-spacing' => '--gform-theme-control-letter-spacing',
'-webkit-font-smoothing' => '--gform-theme-control-font-smoothing',
'-moz-osx-font-smoothing' => '--gform-theme-control-font-smoothing',
),
':hover' => array(
'color' => '--gform-theme-control-color-hover',
),
':focus' => array(
'color' => '--gform-theme-control-color-focus',
),
':disabled' => array(
'color' => '--gform-theme-control-color-disabled',
),
'.invalid' => array(
'color' => '--gform-theme-color-danger',
),
'::placeholder' => array(
'color' => '--gform-theme-control-placeholder-color',
'fontFamily' => '--gform-theme-control-placeholder-font-family',
'fontSize' => '--gform-theme-control-placeholder-font-size',
'fontStyle' => '--gform-theme-control-placeholder-font-style',
'fontWeight' => '--gform-theme-control-placeholder-font-weight',
'letterSpacing' => '--gform-theme-control-placeholder-letter-spacing',
'opacity' => '--gform-theme-control-placeholder-opacity',
),
);
} else {
return array(
'input' => array(
'color' => '--gf-ctrl-color',
'font-family' => '--gf-ctrl-font-family',
'font-size' => '--gf-ctrl-font-size',
'font-style' => '--gf-ctrl-font-style',
'font-weight' => '--gf-ctrl-font-weight',
'letter-spacing' => '--gf-ctrl-letter-spacing',
'-webkit-font-smoothing' => '--gf-ctrl-font-smoothing',
'-moz-osx-font-smoothing' => '--gf-ctrl-font-smoothing',
),
':hover' => array(
'color' => '--gf-ctrl-color-hover',
),
':focus' => array(
'color' => '--gf-ctrl-color-focus',
),
':disabled' => array(
'color' => '--gf-ctrl-color-disabled',
),
'.invalid' => array(
'color' => '--gf-color-danger',
),
'::placeholder' => array(
'color' => '--gf-ctrl-placeholder-color',
'fontFamily' => '--gf-ctrl-placeholder-font-family',
'fontSize' => '--gf-ctrl-placeholder-font-size',
'fontStyle' => '--gf-ctrl-placeholder-font-style',
'fontWeight' => '--gf-ctrl-placeholder-font-weight',
'letterSpacing' => '--gf-ctrl-placeholder-letter-spacing',
'opacity' => '--gf-ctrl-placeholder-opacity',
),
);
}
}
// # PLUGIN SETTINGS -----------------------------------------------------------------------------------------------
/**
* Define plugin settings fields.
*
* @since 1.0
*
* @return array
*/
public function plugin_settings_fields() {
$is_onboarding_redirect = isset( $_GET['ppcp_onboarding'] );
return array(
array(
'title' => esc_html__( 'PayPal Account', 'gravityformsppcp' ),
'description' => $this->get_description( 'paypal_account' ),
'fields' => $is_onboarding_redirect ? $this->check_onboarding_status_field() : $this->api_settings_fields(),
),
);
}
/**
* Get the description for settings section.
*
* @since 1.0
*
* @param string $section The section name.
*
* @return string
*/
public function get_description( $section ) {