forked from gravityplus/Gravity-Forms-Stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripe.php
2741 lines (2265 loc) · 107 KB
/
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
/*
Plugin Name: Gravity Forms Stripe Add-On
Plugin URI: http://naomicbush.github.com/Gravity-Forms-Stripe
Description: Use Stripe to process credit card payments on your site, easily and securely, with Gravity Forms
Version: 0.1.3
Author: Naomi C. Bush
Author URI: http://naomicbush.com
------------------------------------------------------------------------
Copyright 2012 Naomi C. Bush
last updated: April 18, 2012
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
//Version Control
$gravityforms_stripe_file = __FILE__;
if ( isset( $plugin ) ) {
$gravityforms_stripe_file = $plugin;
}
else if ( isset( $mu_plugin ) ) {
$gravityforms_stripe_file = $mu_plugin;
}
else if ( isset( $network_plugin ) ) {
$gravityforms_stripe_file = $network_plugin;
}
define( 'GRAVITYFORMS_STRIPE_FILE', $gravityforms_stripe_file );
define( 'GRAVITYFORMS_STRIPE_PATH', WP_PLUGIN_DIR . '/' . basename( dirname( $gravityforms_stripe_file ) ) );
add_action( 'init', array( 'GFStripe', 'init' ) );
//limits currency to US Dollars
add_filter( 'gform_currency', create_function( '', 'return "USD";' ) );
register_activation_hook( GRAVITYFORMS_STRIPE_FILE, array( 'GFStripe', 'add_permissions' ) );
class GFStripe {
private static $path = 'gravityforms-stripe/stripe.php';
private static $url = 'http://www.gravityforms.com';
private static $slug = 'gravityforms-stripe';
private static $version = '0.1';
private static $min_gravityforms_version = '1.6.3.3';
private static $transaction_response = '';
private static $log = null;
//Plugin starting point. Will load appropriate files
public static function init() {
self::$log = self::create_logger();
if ( basename( $_SERVER[ 'PHP_SELF' ] ) == 'plugins.php' ) {
//loading translations
load_plugin_textdomain( 'gravityforms-stripe', FALSE, '/gravityforms-stripe/languages' );
}
if ( ! self::is_gravityforms_supported() )
return;
if ( is_admin() ) {
//runs the setup when version changes
self::setup();
//loading translations
load_plugin_textdomain( 'gravityforms-stripe', FALSE, '/gravityforms-stripe/languages' );
//integrating with Members plugin
if ( function_exists( 'members_get_capabilities' ) )
add_filter( 'members_get_capabilities', array( 'GFStripe', 'members_get_capabilities' ) );
//creates the subnav left menu
add_filter( 'gform_addon_navigation', array( 'GFStripe', 'create_menu' ) );
//enables credit card field
add_filter( 'gform_enable_credit_card_field', '__return_true' );
if ( self::is_stripe_page() ) {
//enqueueing sack for AJAX requests
wp_enqueue_script( array( 'sack' ) );
//loading data lib
require_once( GRAVITYFORMS_STRIPE_PATH . '/data.php' );
//loading Gravity Forms tooltips
require_once( GFCommon::get_base_path() . '/tooltips.php' );
add_filter( 'gform_tooltips', array( 'GFStripe', 'tooltips' ) );
}
else if ( in_array( RG_CURRENT_PAGE, array( 'admin-ajax.php' ) ) ) {
//loading data class
require_once( GRAVITYFORMS_STRIPE_PATH . '/data.php' );
add_action( 'wp_ajax_gf_stripe_update_feed_active', array( 'GFStripe', 'update_feed_active' ) );
add_action( 'wp_ajax_gf_select_stripe_form', array( 'GFStripe', 'select_stripe_form' ) );
}
else if ( RGForms::get( 'page' ) == 'gf_settings' ) {
RGForms::add_settings_page( 'Stripe', array( 'GFStripe', 'settings_page' ), self::get_base_url() . '/images/stripe_wordpress_icon_32.png' );
add_filter( 'gform_currency_setting_message', create_function( '', "echo '<div class=\'gform_currency_message\'>Stripe only supports US Dollars.</div>';" ) );
add_filter( 'gform_currency_disabled', '__return_true' );
//loading Gravity Forms tooltips
require_once( GFCommon::get_base_path() . '/tooltips.php' );
add_filter( 'gform_tooltips', array( 'GFStripe', 'tooltips' ) );
}
}
else {
//loading data class
require_once( GRAVITYFORMS_STRIPE_PATH . '/data.php' );
add_action( 'gform_enqueue_scripts', array( 'GFStripe', 'load_stripe_js' ), '', 2 );
//remove SSL credit card warnings since credit card information never hits the server
add_filter( 'gform_field_content', array( 'GFStripe', 'gform_field_content' ), 10, 5 );
add_filter( 'gform_field_css_class', array( 'GFStripe', 'remove_ssl_warning_class' ), 10, 3 );
//handling post submission.
add_filter( 'gform_field_validation', array( 'GFStripe', 'gform_field_validation' ), 10, 4 );
add_filter( 'gform_get_form_filter', array( 'GFStripe', 'gform_get_form_filter' ), 10, 1 );
add_filter( 'gform_validation', array( 'GFStripe', 'stripe_validation' ), 10, 4 );
add_action( 'gform_after_submission', array( 'GFStripe', 'stripe_after_submission' ), 10, 2 );
}
}
public static function update_feed_active() {
check_ajax_referer( 'gf_stripe_update_feed_active', 'gf_stripe_update_feed_active' );
$id = $_POST[ "feed_id" ];
$feed = GFStripeData::get_feed( $id );
GFStripeData::update_feed( $id, $feed[ "form_id" ], $_POST[ "is_active" ], $feed[ "meta" ] );
}
//Creates Stripe left nav menu under Forms
public static function create_menu( $menus ) {
// Adding submenu if user has access
$permission = self::has_access( 'gravityforms_stripe' );
if ( ! empty( $permission ) )
$menus[ ] = array(
'name' => 'gf_stripe',
'label' => __( 'Stripe', 'gravityforms-stripe' ),
'callback' => array( 'GFStripe', 'stripe_page' ),
'permission' => $permission );
return $menus;
}
//Creates or updates database tables. Will only run when version changes
private static function setup() {
if ( get_option( 'gf_stripe_version' ) != self::$version ) {
require_once( GRAVITYFORMS_STRIPE_PATH . '/data.php' );
GFStripeData::update_table();
}
update_option( 'gf_stripe_version', self::$version );
}
private static function create_logger() {
if ( ! class_exists( 'KLogger' ) ) {
require_once( GRAVITYFORMS_STRIPE_PATH . '/KLogger.php' );
}
$settings = get_option( 'gf_stripe_settings' );
$log_level = rgempty( 'log_level', $settings ) ? KLogger::OFF : rgar( $settings, 'log_level' );
$log = new KLogger( GRAVITYFORMS_STRIPE_PATH . '/log.txt', $log_level );
return $log;
}
//Adds feed tooltips to the list of tooltips
public static function tooltips( $tooltips ) {
$stripe_tooltips = array(
'stripe_transaction_type' => '<h6>' . __( 'Transaction Type', 'gravityforms-stripe' ) . '</h6>' . __( 'Select which Stripe transaction type should be used. Products and Services, Donations or Subscription.', 'gravityforms-stripe' ),
'stripe_gravity_form' => '<h6>' . __( 'Gravity Form', 'gravityforms-stripe' ) . '</h6>' . __( 'Select which Gravity Forms you would like to integrate with Stripe.', 'gravityforms-stripe' ),
'stripe_customer' => '<h6>' . __( 'Customer', 'gravityforms-stripe' ) . '</h6>' . __( 'Map your Form Fields to the available Stripe customer information fields.', 'gravityforms-stripe' ),
'stripe_options' => '<h6>' . __( 'Options', 'gravityforms-stripe' ) . '</h6>' . __( 'Turn on or off the available Stripe checkout options.', 'gravityforms-stripe' ),
'stripe_recurring_amount' => '<h6>' . __( 'Recurring Amount', 'gravityforms-stripe' ) . '</h6>' . __( 'Select which field determines the recurring payment amount.', 'gravityforms-stripe' ),
'stripe_billing_cycle' => '<h6>' . __( 'Billing Cycle', 'gravityforms-stripe' ) . '</h6>' . __( 'Select your billing cycle. This determines how often the recurring payment should occur.', 'gravityforms-stripe' ),
'stripe_recurring_times' => '<h6>' . __( 'Recurring Times', 'gravityforms-stripe' ) . '</h6>' . __( 'Select how many times the recurring payment should be made. The default is to bill the customer until the subscription is canceled.', 'gravityforms-stripe' ),
'stripe_trial_period_enable' => '<h6>' . __( 'Trial Period', 'gravityforms-stripe' ) . '</h6>' . __( 'Enable a trial period. The users recurring payment will not begin until after this trial period.', 'gravityforms-stripe' ),
'stripe_trial_amount' => '<h6>' . __( 'Trial Amount', 'gravityforms-stripe' ) . '</h6>' . __( 'Enter the trial period amount or leave it blank for a free trial.', 'gravityforms-stripe' ),
'stripe_trial_period' => '<h6>' . __( 'Trial Recurring Times', 'gravityforms-stripe' ) . '</h6>' . __( 'Select the number of billing occurrences or payments in the trial period.', 'gravityforms-stripe' ),
'stripe_api' => '<h6>' . __( 'API', 'gravityforms-stripe' ) . '</h6>' . __( 'Select the Stripe API you would like to use. Select \'Live\' to use your Live API keys. Select \'Test\' to use your Test API keys.', 'gravityforms-stripe' ),
'stripe_test_secret_key' => '<h6>' . __( 'API Test Secret Key', 'gravityforms-stripe' ) . '</h6>' . __( 'Enter the API Test Secret Key for your Stripe account.', 'gravityforms-stripe' ),
'stripe_test_publishable_key' => '<h6>' . __( 'API Test Publishable Key', 'gravityforms-stripe' ) . '</h6>' . __( 'Enter the API Test Publishable Key for your Stripe account.', 'gravityforms-stripe' ),
'stripe_live_secret_key' => '<h6>' . __( 'API Live Secret Key', 'gravityforms-stripe' ) . '</h6>' . __( 'Enter the API Live Secret Key for your Stripe account.', 'gravityforms-stripe' ),
'stripe_live_publishable_key' => '<h6>' . __( 'API Live Publishable Key', 'gravityforms-stripe' ) . '</h6>' . __( 'Enter the API Live Publishable Key for your Stripe account.', 'gravityforms-stripe' ),
'stripe_conditional' => '<h6>' . __( 'Stripe Condition', 'gravityforms-stripe' ) . '</h6>' . __( 'When the Stripe condition is enabled, form submissions will only be sent to Stripe when the condition is met. When disabled all form submissions will be sent to Stripe.', 'gravityforms-stripe' )
);
return array_merge( $tooltips, $stripe_tooltips );
}
public static function stripe_page() {
$view = rgget( 'view' );
if ( $view == 'edit' )
self::edit_page( rgget( 'id' ) );
else if ( $view == 'stats' )
self::stats_page( rgget( 'id' ) );
else
self::list_page();
}
//Displays the stripe feeds list page
private static function list_page() {
if ( ! self::is_gravityforms_supported() ) {
die( __( sprintf( 'Stripe Add-On requires Gravity Forms %s. Upgrade automatically on the %sPlugin page%s.', self::$min_gravityforms_version, '<a href="plugins.php">', '</a>' ), 'gravityforms-stripe' ) );
}
if ( rgpost( 'action' ) == 'delete' ) {
check_admin_referer( 'list_action', 'gf_stripe_list' );
$id = absint( $_POST[ "action_argument" ] );
GFStripeData::delete_feed( $id );
?>
<div class="updated fade" style="padding:6px"><?php _e( 'Feed deleted.', 'gravityforms-stripe' ) ?></div>
<?php
}
else if ( ! empty( $_POST[ "bulk_action" ] ) ) {
check_admin_referer( 'list_action', 'gf_stripe_list' );
$selected_feeds = $_POST[ "feed" ];
if ( is_array( $selected_feeds ) ) {
foreach ( $selected_feeds as $feed_id )
GFStripeData::delete_feed( $feed_id );
}
?>
<div class="updated fade" style="padding:6px"><?php _e( 'Feeds deleted.', 'gravityforms-stripe' ) ?></div>
<?php
}
?>
<div class="wrap">
<img alt="<?php _e( 'Stripe Transactions', 'gravityforms-stripe' ) ?>"
src="<?php echo self::get_base_url()?>/images/stripe_wordpress_icon_32.png"
style="float:left; margin:15px 7px 0 0;"/>
<h2><?php
_e( 'Stripe Forms', 'gravityforms-stripe' );
?>
<a class="button add-new-h2"
href="admin.php?page=gf_stripe&view=edit&id=0"><?php _e( 'Add New', 'gravityforms-stripe' ) ?></a>
</h2>
<form id="feed_form" method="post">
<?php wp_nonce_field( 'list_action', 'gf_stripe_list' ) ?>
<input type="hidden" id="action" name="action"/>
<input type="hidden" id="action_argument" name="action_argument"/>
<div class="tablenav">
<div class="alignleft actions" style="padding:8px 0 7px 0;">
<label class="hidden" for="bulk_action"><?php _e( 'Bulk action', 'gravityforms-stripe' ) ?></label>
<select name="bulk_action" id="bulk_action">
<option value=''> <?php _e( 'Bulk action', 'gravityforms-stripe' ) ?> </option>
<option value='delete'><?php _e( 'Delete', 'gravityforms-stripe' ) ?></option>
</select>
<?php
echo '<input type="submit" class="button" value="' . __( 'Apply', 'gravityforms-stripe' ) . '" onclick="if( jQuery(\'#bulk_action\').val() == \'delete\' && !confirm(\'' . __( 'Delete selected feeds? ', 'gravityforms-stripe' ) . __( '\'Cancel\' to stop, \'OK\' to delete.', 'gravityforms-stripe' ) . '\')) { return false; } return true;"/>';
?>
</div>
</div>
<table class="widefat fixed" cellspacing="0">
<thead>
<tr>
<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox"/></th>
<th scope="col" id="active" class="manage-column check-column"></th>
<th scope="col" class="manage-column"><?php _e( 'Form', 'gravityforms-stripe' ) ?></th>
<th scope="col" class="manage-column"><?php _e( 'Transaction Type', 'gravityforms-stripe' ) ?></th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox"/></th>
<th scope="col" id="active" class="manage-column check-column"></th>
<th scope="col" class="manage-column"><?php _e( 'Form', 'gravityforms-stripe' ) ?></th>
<th scope="col" class="manage-column"><?php _e( 'Transaction Type', 'gravityforms-stripe' ) ?></th>
</tr>
</tfoot>
<tbody class="list:user user-list">
<?php
$settings = GFStripeData::get_feeds();
$is_valid = self::is_valid_key();
if ( ! $is_valid[ 0 ] ) {
?>
<tr>
<td colspan="4" style="padding:20px;">
<?php echo sprintf( __( "To get started, please configure your %sStripe Settings%s.", 'gravityforms-stripe' ), '<a href="admin.php?page=gf_settings&addon=Stripe">', '</a>' ); ?>
</td>
</tr>
<?php
}
else if ( is_array( $settings ) && sizeof( $settings ) > 0 ) {
foreach ( $settings as $setting ) {
?>
<tr class='author-self status-inherit' valign="top">
<th scope="row" class="check-column"><input type="checkbox" name="feed[]"
value="<?php echo $setting[ "id" ] ?>"/></th>
<td><img
src="<?php echo self::get_base_url() ?>/images/active<?php echo intval( $setting[ "is_active" ] ) ?>.png"
alt="<?php echo $setting[ "is_active" ] ? __( 'Active', 'gravityforms-stripe' ) : __( 'Inactive', 'gravityforms-stripe' );?>"
title="<?php echo $setting[ "is_active" ] ? __( 'Active', 'gravityforms-stripe' ) : __( 'Inactive', 'gravityforms-stripe' );?>"
onclick="ToggleActive(this, <?php echo $setting[ 'id' ] ?>); "/></td>
<td class="column-title">
<a href="admin.php?page=gf_stripe&view=edit&id=<?php echo $setting[ "id" ] ?>"
title="<?php _e( 'Edit', 'gravityforms-stripe' ) ?>"><?php echo $setting[ "form_title" ] ?></a>
<div class="row-actions">
<span class="edit">
<a title="<?php _e( 'Edit', 'gravityforms-stripe' )?>"
href="admin.php?page=gf_stripe&view=edit&id=<?php echo $setting[ "id" ] ?>"
title="<?php _e( 'Edit', 'gravityforms-stripe' ) ?>"><?php _e( 'Edit', 'gravityforms-stripe' ) ?></a>
|
</span>
<span>
<a title="<?php _e( 'View Stats', 'gravityforms-stripe' )?>"
href="admin.php?page=gf_stripe&view=stats&id=<?php echo $setting[ "id" ] ?>"
title="<?php _e( 'View Stats', 'gravityforms-stripe' ) ?>"><?php _e( 'Stats', 'gravityforms-stripe' ) ?></a>
|
</span>
<span>
<a title="<?php _e( 'View Entries', 'gravityforms-stripe' )?>"
href="admin.php?page=gf_entries&view=entries&id=<?php echo $setting[ "form_id" ] ?>"
title="<?php _e( 'View Entries', 'gravityforms-stripe' ) ?>"><?php _e( 'Entries', 'gravityforms-stripe' ) ?></a>
|
</span>
<span>
<a title="<?php _e( "Delete", "gravityforms-stripe" ) ?>"
href="javascript: if(confirm('<?php _e( 'Delete this feed? ', 'gravityforms-stripe' ) ?> <?php _e( "\'Cancel\' to stop, \'OK\' to delete.", 'gravityforms-stripe' ) ?>')){ DeleteSetting(<?php echo $setting[ "id" ] ?>);}"><?php _e( 'Delete', 'gravityforms-stripe' )?></a>
</span>
</div>
</td>
<td class="column-date">
<?php
switch ( $setting[ "meta" ][ "type" ] ) {
case 'product' :
_e( 'Product and Services', 'gravityforms-stripe' );
break;
case 'subscription' :
_e( 'Subscription', 'gravityforms-stripe' );
break;
}
?>
</td>
</tr>
<?php
}
}
else {
?>
<tr>
<td colspan="4" style="padding:20px;">
<?php echo sprintf( __( "You don't have any Stripe feeds configured. Let's go %screate one%s!", 'gravityforms-stripe' ), '<a href="admin.php?page=gf_stripe&view=edit&id=0">', '</a>' ); ?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</form>
</div>
<script type="text/javascript">
function DeleteSetting(id) {
jQuery("#action_argument").val(id);
jQuery("#action").val("delete");
jQuery("#feed_form")[0].submit();
}
function ToggleActive(img, feed_id) {
var is_active = img.src.indexOf("active1.png") >= 0
if (is_active) {
img.src = img.src.replace("active1.png", "active0.png");
jQuery(img).attr('title', '<?php _e( 'Inactive', 'gravityforms-stripe' ) ?>').attr('alt', '<?php _e( 'Inactive', 'gravityforms-stripe' ) ?>');
}
else {
img.src = img.src.replace("active0.png", "active1.png");
jQuery(img).attr('title', '<?php _e( 'Active', 'gravityforms-stripe' ) ?>').attr('alt', '<?php _e( 'Active', 'gravityforms-stripe' ) ?>');
}
var mysack = new sack("<?php echo admin_url( "admin-ajax.php" )?>");
mysack.execute = 1;
mysack.method = 'POST';
mysack.setVar("action", "gf_stripe_update_feed_active");
mysack.setVar("gf_stripe_update_feed_active", "<?php echo wp_create_nonce( 'gf_stripe_update_feed_active' ) ?>");
mysack.setVar("feed_id", feed_id);
mysack.setVar("is_active", is_active ? 0 : 1);
mysack.encVar("cookie", document.cookie, false);
mysack.onError = function () {
alert('<?php _e( 'Ajax error while updating feed', 'gravityforms-stripe' ) ?>')
};
mysack.runAJAX();
return true;
}
</script>
<?php
}
public static function settings_page() {
/*if(!class_exists("RGAuthorizeNetUpgrade"))
require_once("plugin-upgrade.php");*/
if ( isset( $_POST[ "uninstall" ] ) ) {
check_admin_referer( 'uninstall', 'gf_stripe_uninstall' );
self::uninstall();
?>
<div class="updated fade"
style="padding:20px;"><?php _e( sprintf( "Gravity Forms Stripe Add-On has been successfully uninstalled. It can be re-activated from the %splugins page%s.", "<a href='plugins.php'>", "</a>" ), 'gravityforms-stripe' )?></div>
<?php
return;
}
else if ( isset( $_POST[ "gf_stripe_submit" ] ) ) {
check_admin_referer( 'update', 'gf_stripe_update' );
$settings = array(
'test_secret_key' => rgpost( 'gf_stripe_test_secret_key' ),
'test_publishable_key' => rgpost( 'gf_stripe_test_publishable_key' ),
'live_secret_key' => rgpost( 'gf_stripe_live_secret_key' ),
'live_publishable_key' => rgpost( 'gf_stripe_live_publishable_key' ),
'mode' => rgpost( 'gf_stripe_mode' ),
'log_level' => rgpost( 'gf_stripe_log_level' )
);
update_option( 'gf_stripe_settings', $settings );
}
else {
$settings = get_option( 'gf_stripe_settings' );
}
$is_valid = self::is_valid_key();
$message = array();
if ( $is_valid[ 0 ] )
$message[ 0 ] = 'Valid API key.';
else {
foreach ( $is_valid[ 1 ] as $key => $value ) {
if ( ! empty( $settings[ $key ] ) ) {
if ( ! $value ) {
$message[ 1 ][ $key ] = 'Invalid API key. Please try again.';
}
else {
$message[ 1 ][ $key ] = 'Valid API key.';
}
}
}
}
?>
<style>
.valid_credentials {
color: green;
}
.invalid_credentials {
color: red;
}
.size-1 {
width: 400px;
}
</style>
<form method="post" action="">
<?php wp_nonce_field( 'update', 'gf_stripe_update' ) ?>
<h3><?php _e( 'Stripe Account Information', 'gravityforms-stripe' ) ?></h3>
<p style="text-align: left;">
<?php _e( sprintf( "Stripe is a payment gateway for merchants. Use Gravity Forms to collect payment information and automatically integrate to your client's Stripe account. If you don't have a Stripe account, you can %ssign up for one here%s", "<a href='http://www.stripe.com' target='_blank'>", "</a>" ), 'gravityforms-stripe' ) ?>
</p>
<table class="form-table">
<tr>
<th scope="row" nowrap="nowrap"><label
for="gf_stripe_mode"><?php _e( 'API Mode', 'gravityforms-stripe' ); ?> <?php gform_tooltip( 'stripe_api' ) ?></label>
</th>
<td width="88%">
<input type="radio" name="gf_stripe_mode" id="gf_stripe_mode_live"
value="live" <?php echo rgar( $settings, 'mode' ) != 'test' ? "checked='checked'" : '' ?>/>
<label class="inline" for="gf_stripe_mode_live"><?php _e( 'Live', 'gravityforms-stripe' ); ?></label>
<input type="radio" name="gf_stripe_mode" id="gf_stripe_mode_test"
value="test" <?php echo rgar( $settings, 'mode' ) == 'test' ? "checked='checked'" : '' ?>/>
<label class="inline" for="gf_stripe_mode_test"><?php _e( 'Test', 'gravityforms-stripe' ); ?></label>
</td>
</tr>
<tr>
<th scope="row" nowrap="nowrap"><label
for="gf_stripe_test_secret_key"><?php _e( 'Test Secret Key', 'gravityforms-stripe' ); ?> <?php gform_tooltip( 'stripe_test_secret_key' ) ?></label>
</th>
<td width="88%">
<input class="size-1" id="gf_stripe_test_secret_key" name="gf_stripe_test_secret_key"
value="<?php echo esc_attr( rgar( $settings, 'test_secret_key' ) ) ?>"/>
<img
src="<?php echo self::get_base_url() ?>/images/<?php echo $is_valid[ 1 ][ 'test_secret_key' ] ? 'tick.png' : 'stop.png' ?>"
border="0"
alt="<?php array_key_exists( 0, $message ) ? $message[ 0 ] : $message[ 1 ][ 'test_secret_key' ] ?>"
title="<?php echo array_key_exists( 0, $message ) ? $message[ 0 ] : $message[ 1 ][ 'test_secret_key' ] ?>"
style="display:<?php echo ( empty( $message[ 0 ] ) && empty( $message[ 1 ][ 'test_secret_key' ] ) ) ? 'none;' : 'inline;' ?>"/>
<br/>
<small><?php _e( "You can find your <strong>Test Secret Key</strong> by clicking on 'Your Account' in the top right corner of the Stripe Account Dashboard. Choose 'Account Settings' then 'API Keys'. Your API keys will be displayed.", 'gravityforms-stripe' ) ?></small>
</td>
</tr>
<tr>
<th scope="row" nowrap="nowrap"><label
for="gf_stripe_test_publishable_key"><?php _e( 'Test Publishable Key', 'gravityforms-stripe' ); ?> <?php gform_tooltip( 'stripe_test_publishable_key' ) ?></label>
</th>
<td width="88%">
<input class="size-1" id="gf_stripe_test_publishable_key" name="gf_stripe_test_publishable_key"
value="<?php echo esc_attr( rgar( $settings, 'test_publishable_key' ) ) ?>"/>
<img
src="<?php echo self::get_base_url() ?>/images/<?php echo $is_valid[ 1 ][ 'test_publishable_key' ] ? 'tick.png' : 'stop.png' ?>"
border="0"
alt="<?php array_key_exists( 0, $message ) ? $message[ 0 ] : $message[ 1 ][ 'test_publishable_key' ] ?>"
title="<?php echo array_key_exists( 0, $message ) ? $message[ 0 ] : $message[ 1 ][ 'test_publishable_key' ] ?>"
style="display:<?php echo ( empty( $message[ 0 ] ) && empty( $message[ 1 ][ 'test_publishable_key' ] ) ) ? 'none;' : 'inline;' ?>"/>
<br/>
<small><?php _e( "You can find your <strong>Test Publishable Key</strong> by clicking on 'Your Account' in the top right corner of the Stripe Account Dashboard. Choose 'Account Settings' then 'API Keys'. Your API keys will be displayed.", "gravityforms-stripe" ) ?></small>
</td>
</tr>
<tr>
<th scope="row" nowrap="nowrap"><label
for="gf_stripe_live_secret_key"><?php _e( 'Live Secret Key', 'gravityforms-stripe' ); ?> <?php gform_tooltip( 'stripe_live_secret_key' ) ?></label>
</th>
<td width="88%">
<input class="size-1" id="gf_stripe_live_secret_key" name="gf_stripe_live_secret_key"
value="<?php echo esc_attr( rgar( $settings, 'live_secret_key' ) ) ?>"/>
<img
src="<?php echo self::get_base_url() ?>/images/<?php echo $is_valid[ 1 ][ 'live_secret_key' ] ? 'tick.png' : 'stop.png' ?>"
border="0"
alt="<?php array_key_exists( 0, $message ) ? $message[ 0 ] : $message[ 1 ][ 'live_secret_key' ] ?>"
title="<?php echo array_key_exists( 0, $message ) ? $message[ 0 ] : $message[ 1 ][ 'live_secret_key' ] ?>"
style="display:<?php echo ( empty( $message[ 0 ] ) && empty( $message[ 1 ][ 'live_secret_key' ] ) ) ? 'none;' : 'inline;' ?>"/>
<br/>
<small><?php _e( "You can find your <strong>Live Secret Key</strong> by clicking on 'Your Account' in the top right corner of the Stripe Account Dashboard. Choose 'Account Settings' then 'API Keys'. Your API keys will be displayed.", "gravityforms-stripe" ) ?></small>
</td>
</tr>
<tr>
<th scope="row" nowrap="nowrap"><label
for="gf_stripe_live_publishable_key"><?php _e( 'Live Publishable Key', 'gravityforms-stripe' ); ?> <?php gform_tooltip( 'stripe_live_publishable_key' ) ?></label>
</th>
<td width="88%">
<input class="size-1" id="gf_stripe_live_publishable_key" name="gf_stripe_live_publishable_key"
value="<?php echo esc_attr( rgar( $settings, 'live_publishable_key' ) ) ?>"/>
<img
src="<?php echo self::get_base_url() ?>/images/<?php echo $is_valid[ 1 ][ 'live_publishable_key' ] ? 'tick.png' : 'stop.png' ?>"
border="0"
alt="<?php array_key_exists( 0, $message ) ? $message[ 0 ] : $message[ 1 ][ 'live_publishable_key' ] ?>"
title="<?php echo array_key_exists( 0, $message ) ? $message[ 0 ] : $message[ 1 ][ 'live_publishable_key' ] ?>"
style="display:<?php echo ( empty( $message[ 0 ] ) && empty( $message[ 1 ][ 'live_publishable_key' ] ) ) ? 'none;' : 'inline;' ?>"/>
<br/>
<small><?php _e( "You can find your <strong>Live Publishable Key</strong> by clicking on 'Your Account' in the top right corner of the Stripe Account Dashboard. Choose 'Account Settings' then 'API Keys'. Your API keys will be displayed.", "gravityforms-stripe" ) ?></small>
</td>
</tr>
<tr style="display:<?php echo rgempty( 'debug', $_GET ) ? 'none' : 'block' ?>;">
<td colspan="2">
<h3><?php _e( 'Debugging Settings', 'gravityforms-stripe' ) ?></h3>
</td>
</tr>
<tr style="display:<?php echo rgempty( 'debug', $_GET ) ? 'none' : 'block' ?>;">
<th scope="row" nowrap="nowrap"><label
for="gf_stripe_log_level"><?php _e( 'Logging', 'gravityforms-stripe' ); ?></label></th>
<td width="88%">
<select id="gf_stripe_log_level" name="gf_stripe_log_level">
<option
value="<?php echo KLogger::OFF ?>" <?php echo rgempty( 'log_level', $settings ) ? "selected='selected'" : "" ?>><?php _e( 'Off', 'gravityforms-stripe' ) ?></option>
<option
value="<?php echo KLogger::DEBUG ?>" <?php echo rgar( $settings, 'log_level' ) == KLogger::DEBUG ? "selected='selected'" : "" ?>><?php _e( 'Log all messages', 'gravityforms-stripe' ) ?></option>
<option
value="<?php echo KLogger::ERROR ?>" <?php echo rgar( $settings, 'log_level' ) == KLogger::ERROR ? "selected='selected'" : "" ?>><?php _e( 'Log errors only', 'gravityforms-stripe' ) ?></option>
</select>
<?php
if ( file_exists( GRAVITYFORMS_STRIPE_PATH . '/log.txt' ) ) {
?>
<a href="<?php echo self::get_base_url() . '/log.txt' ?>"
target="_blank"><?php _e( 'view log', 'gravityforms-stripe' ) ?></a>
<?php
}
?>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="gf_stripe_submit" class="button-primary"
value="<?php _e( 'Save Settings', 'gravityforms-stripe' ) ?>"/></td>
</tr>
</table>
</form>
<form action="" method="post">
<?php wp_nonce_field( 'uninstall', 'gf_stripe_uninstall' ) ?>
<?php if ( GFCommon::current_user_can_any( 'gravityforms_stripe_uninstall' ) ) { ?>
<div class="hr-divider"></div>
<h3><?php _e( 'Uninstall Stripe Add-On', 'gravityforms-stripe' ) ?></h3>
<div class="delete-alert"><?php _e( 'Warning! This operation deletes ALL Stripe Feeds.', 'gravityforms-stripe' ) ?>
<?php
$uninstall_button = '<input type="submit" name="uninstall" value="' . __( 'Uninstall Stripe Add-On', 'gravityforms-stripe' ) . '" class="button" onclick="return confirm(\'' . __( "Warning! ALL Stripe Feeds will be deleted. This cannot be undone. \'OK\' to delete, \'Cancel\' to stop", 'gravityforms-stripe' ) . '\');"/>';
echo apply_filters( 'gform_stripe_uninstall_button', $uninstall_button );
?>
</div>
<?php } ?>
</form>
<?php
}
private static function is_valid_key() {
self::include_api();
$settings = get_option( 'gf_stripe_settings' );
$year = date( 'Y' ) + 1;
$valid_keys = array(
'test_secret_key' => false,
'test_publishable_key' => false,
'live_secret_key' => false,
'live_publishable_key' => false
);
$valid = false;
$flag_false = 0;
foreach ( $valid_keys as $key => $value ) {
if ( ! empty( $settings[ $key ] ) ) {
try {
Stripe::setApiKey( $settings[ $key ] );
Stripe_Token::create( array(
'card' => array(
'number' => '4242424242424242',
'exp_month' => 3,
'exp_year' => $year,
'cvc' => 314
),
'currency' => 'usd' ) );
$valid_keys[ $key ] = true;
}
catch ( Exception $e ) {
$class = get_class( $e );
if ( $class == 'Stripe_CardError' ) {
$valid_keys[ $key ] = true;
}
else {
$flag_false ++;
}
}
}
else {
$flag_false ++;
}
}
if ( $flag_false == 0 ) {
$valid = true;
}
return array( $valid, $valid_keys );
}
private static function get_test_secret_key() {
$settings = get_option( 'gf_stripe_settings' );
$test_secret_key = $settings[ 'test_secret_key' ];
return $test_secret_key;
}
private static function get_test_publishable_key() {
$settings = get_option( 'gf_stripe_settings' );
$test_publishable_key = $settings[ 'test_publishable_key' ];
return $test_publishable_key;
}
private static function get_live_secret_key() {
$settings = get_option( 'gf_stripe_settings' );
$live_secret_key = $settings[ 'live_secret_key' ];
return $live_secret_key;
}
private static function get_live_publishable_key() {
$settings = get_option( 'gf_stripe_settings' );
$live_publishable_key = $settings[ 'live_publishable_key' ];
return $live_publishable_key;
}
private static function include_api() {
if ( ! class_exists( 'Stripe' ) )
require_once( GRAVITYFORMS_STRIPE_PATH . '/api/lib/Stripe.php' );
}
private static function get_product_field_options( $productFields, $selectedValue ) {
$options = "<option value=''>" . __( "Select a product", "gravityforms-stripe" ) . "</option>";
foreach ( $productFields as $field ) {
$label = GFCommon::truncate_middle( $field[ "label" ], 30 );
$selected = $selectedValue == $field[ "id" ] ? "selected='selected'" : "";
$options .= "<option value='{$field["id"]}' {$selected}>{$label}</option>";
}
return $options;
}
private static function stats_page() {
?>
<style>
.stripe_graph_container {
clear: both;
padding-left: 5px;
min-width: 789px;
margin-right: 50px;
}
.stripe_message_container {
clear: both;
padding-left: 5px;
text-align: center;
padding-top: 120px;
border: 1px solid #CCC;
background-color: #FFF;
width: 100%;
height: 160px;
}
.stripe_summary_container {
margin: 30px 60px;
text-align: center;
min-width: 740px;
margin-left: 50px;
}
.stripe_summary_item {
width: 160px;
background-color: #FFF;
border: 1px solid #CCC;
padding: 14px 8px;
margin: 6px 3px 6px 0;
display: -moz-inline-stack;
display: inline-block;
zoom: 1;
*display: inline;
text-align: center;
}
.stripe_summary_value {
font-size: 20px;
margin: 5px 0;
font-family: Georgia, "Times New Roman", "Bitstream Charter", Times, serif
}
.stripe_summary_title {
}
#stripe_graph_tooltip {
border: 4px solid #b9b9b9;
padding: 11px 0 0 0;
background-color: #f4f4f4;
text-align: center;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
-khtml-border-radius: 4px;
}
#stripe_graph_tooltip .tooltip_tip {
width: 14px;
height: 14px;
background-image: url(<?php echo self::get_base_url() ?>/images/tooltip_tip.png);
background-repeat: no-repeat;
position: absolute;
bottom: -14px;
left: 68px;
}
.stripe_tooltip_date {
line-height: 130%;
font-weight: bold;
font-size: 13px;
color: #21759B;
}
.stripe_tooltip_sales {
line-height: 130%;
}
.stripe_tooltip_revenue {
line-height: 130%;
}
.stripe_tooltip_revenue .stripe_tooltip_heading {
}
.stripe_tooltip_revenue .stripe_tooltip_value {
}
.stripe_trial_disclaimer {
clear: both;
padding-top: 20px;
font-size: 10px;
}
</style>
<script type="text/javascript" src="<?php echo self::get_base_url() ?>/flot/jquery.flot.min.js"></script>
<script type="text/javascript" src="<?php echo self::get_base_url() ?>/js/currency.js"></script>
<div class="wrap">
<img alt="<?php _e( 'Stripe', 'gravityforms-stripe' ) ?>" style="margin: 15px 7px 0pt 0pt; float: left;"
src="<?php echo self::get_base_url() ?>/images/stripe_wordpress_icon_32.png"/>
<h2><?php _e( 'Stripe Stats', 'gravityforms-stripe' ) ?></h2>
<form method="post" action="">
<ul class="subsubsub">
<li><a class="<?php echo ( ! RGForms::get( 'tab' ) || RGForms::get( 'tab' ) == 'daily' ) ? 'current' : '' ?>"
href="?page=gf_stripe&view=stats&id=<?php echo $_GET[ "id" ] ?>"><?php _e( 'Daily', 'gravityforms' ); ?></a>
|
</li>
<li><a class="<?php echo RGForms::get( 'tab' ) == 'weekly' ? 'current' : ''?>"
href="?page=gf_stripe&view=stats&id=<?php echo $_GET[ "id" ] ?>&tab=weekly"><?php _e( 'Weekly', 'gravityforms' ); ?></a>
|
</li>
<li><a class="<?php echo RGForms::get( 'tab' ) == 'monthly' ? 'current' : ''?>"
href="?page=gf_stripe&view=stats&id=<?php echo $_GET[ "id" ] ?>&tab=monthly"><?php _e( 'Monthly', 'gravityforms' ); ?></a>
</li>
</ul>
<?php
$config = GFStripeData::get_feed( RGForms::get( 'id' ) );
switch ( RGForms::get( 'tab' ) ) {
case 'monthly' :
$chart_info = self::monthly_chart_info( $config );
break;
case 'weekly' :
$chart_info = self::weekly_chart_info( $config );
break;
default :
$chart_info = self::daily_chart_info( $config );
break;
}
if ( ! $chart_info[ "series" ] ) {
?>
<div
class="stripe_message_container"><?php _e( 'No payments have been made yet.', 'gravityforms-stripe' ) ?> <?php echo $config[ "meta" ][ "trial_period_enabled" ] && empty( $config[ "meta" ][ "trial_amount" ] ) ? " **" : ""?></div>
<?php
}
else {
?>
<div class="stripe_graph_container">
<div id="graph_placeholder" style="width:100%;height:300px;"></div>
</div>
<script type="text/javascript">
var stripe_graph_tooltips = <?php echo $chart_info[ "tooltips" ]?>;
jQuery.plot(jQuery("#graph_placeholder"), <?php echo $chart_info[ "series" ] ?>, <?php echo $chart_info[ "options" ] ?>);
jQuery(window).resize(function () {
jQuery.plot(jQuery("#graph_placeholder"), <?php echo $chart_info[ "series" ] ?>, <?php echo $chart_info[ "options" ] ?>);
});
var previousPoint = null;
jQuery("#graph_placeholder").bind("plothover", function (event, pos, item) {
startShowTooltip(item);
});
jQuery("#graph_placeholder").bind("plotclick", function (event, pos, item) {
startShowTooltip(item);
});
function startShowTooltip(item) {
if (item) {
if (!previousPoint || previousPoint[0] != item.datapoint[0]) {
previousPoint = item.datapoint;
jQuery("#stripe_graph_tooltip").remove();
var x = item.datapoint[0].toFixed(2),
y = item.datapoint[1].toFixed(2);
showTooltip(item.pageX, item.pageY, stripe_graph_tooltips[item.dataIndex]);
}
}
else {
jQuery("#stripe_graph_tooltip").remove();
previousPoint = null;
}
}
function showTooltip(x, y, contents) {
jQuery('<div id="stripe_graph_tooltip">' + contents + '<div class="tooltip_tip"></div></div>').css({
position:'absolute',
display:'none',
opacity:0.90,
width:'150px',
height:'<?php echo $config[ "meta" ][ "type" ] == "subscription" ? "75px" : "60px";?>',
top:y - <?php echo $config[ "meta" ][ "type" ] == "subscription" ? "100" : "89";?>,
left:x - 79
}).appendTo("body").fadeIn(200);
}
function convertToMoney(number) {
var currency = getCurrentCurrency();
return currency.toMoney(number);
}
function formatWeeks(number) {
number = number + "";
return "<?php _e( "Week ", "gravityforms-stripe" ) ?>" + number.substring(number.length - 2);
}
function getCurrentCurrency() {
<?php
if ( ! class_exists( 'RGCurrency' ) )
require_once( ABSPATH . '/' . PLUGINDIR . '/gravityforms/currency.php' );
$current_currency = RGCurrency::get_currency( GFCommon::get_currency() );
?>
var currency = new Currency(<?php echo GFCommon::json_encode( $current_currency )?>);
return currency;
}
</script>
<?php
}
$payment_totals = RGFormsModel::get_form_payment_totals( $config[ "form_id" ] );
$transaction_totals = GFStripeData::get_transaction_totals( $config[ "form_id" ] );
switch ( $config[ "meta" ][ "type" ] ) {
case 'product' :
$total_sales = $payment_totals[ "orders" ];
$sales_label = __( 'Total Orders', 'gravityforms-stripe' );
break;
case 'donation' :
$total_sales = $payment_totals[ "orders" ];
$sales_label = __( 'Total Donations', 'gravityforms-stripe' );
break;
case 'subscription' :
$total_sales = $payment_totals[ "active" ];
$sales_label = __( 'Active Subscriptions', 'gravityforms-stripe' );
break;
}
$total_revenue = empty( $transaction_totals[ "payment" ][ "revenue" ] ) ? 0 : $transaction_totals[ "payment" ][ "revenue" ];
?>
<div class="stripe_summary_container">
<div class="stripe_summary_item">
<div class="stripe_summary_title"><?php _e( 'Total Revenue', 'gravityforms-stripe' )?></div>
<div class="stripe_summary_value"><?php echo GFCommon::to_money( $total_revenue ) ?></div>
</div>
<div class="stripe_summary_item">
<div class="stripe_summary_title"><?php echo $chart_info[ "revenue_label" ]?></div>
<div class="stripe_summary_value"><?php echo $chart_info[ "revenue" ] ?></div>
</div>
<div class="stripe_summary_item">
<div class="stripe_summary_title"><?php echo $sales_label?></div>
<div class="stripe_summary_value"><?php echo $total_sales ?></div>
</div>
<div class="stripe_summary_item">
<div class="stripe_summary_title"><?php echo $chart_info[ "sales_label" ] ?></div>
<div class="stripe_summary_value"><?php echo $chart_info[ "sales" ] ?></div>
</div>
</div>
<?php
if ( ! $chart_info[ "series" ] && $config[ "meta" ][ "trial_period_enabled" ] && empty( $config[ "meta" ][ "trial_amount" ] ) ) {
?>
<div
class="stripe_trial_disclaimer"><?php _e( '** Free trial transactions will only be reflected in the graph after the first payment is made (i.e. after trial period ends)', 'gravityforms-stripe' ) ?></div>
<?php
}
?>