-
Notifications
You must be signed in to change notification settings - Fork 7
/
class-gf-authorizenet.php
1594 lines (1271 loc) · 62.6 KB
/
class-gf-authorizenet.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
// don't load directly
if ( ! defined( 'ABSPATH' ) ) {
die();
}
GFForms::include_payment_addon_framework();
class GFAuthorizeNet extends GFPaymentAddOn {
protected $_version = GF_AUTHORIZENET_VERSION;
protected $_min_gravityforms_version = '1.9.12';
protected $_slug = 'gravityformsauthorizenet';
protected $_path = 'gravityformsauthorizenet/authorizenet.php';
protected $_full_path = __FILE__;
protected $_title = 'Authorize.Net Add-On';
protected $_short_title = 'Authorize.Net';
protected $_requires_credit_card = true;
protected $_supports_callbacks = true;
// Members plugin integration
protected $_capabilities = array(
'gravityforms_authorizenet',
'gravityforms_authorizenet_uninstall',
'gravityforms_authorizenet_plugin_page'
);
// Permissions
protected $_capabilities_settings_page = 'gravityforms_authorizenet';
protected $_capabilities_form_settings = 'gravityforms_authorizenet';
protected $_capabilities_uninstall = 'gravityforms_authorizenet_uninstall';
protected $_capabilities_plugin_page = 'gravityforms_authorizenet_plugin_page';
// Automatic upgrade enabled
protected $_enable_rg_autoupgrade = true;
/**
* @var array $_args_for_deprecated_hooks Will hold a few arrays which are needed by some deprecated hooks, keeping them out of the $authorization array so that potentially sensitive data won't be exposed in logging statements.
*/
private $_args_for_deprecated_hooks = array();
private static $_instance = null;
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new GFAuthorizeNet();
}
return self::$_instance;
}
//----- SETTINGS PAGES ----------//
public function plugin_settings_fields() {
$description = '<p style="text-align: left;">' . sprintf( esc_html__( 'Authorize.Net is a payment gateway for merchants. Use Gravity Forms to collect payment information and automatically integrate to your Authorize.Net account. If you don\'t have an Authorize.Net account, you can %ssign up for one here.%s', 'gravityformsauthorizenet' ), '<a href="http://www.authorizenet.com" target="_blank">', '</a>' ) . '</p>';
return array(
array(
'title' => esc_html__( 'Authorize.Net Account Information', 'gravityformsauthorizenet' ),
'description' => $description,
'fields' => array(
array(
'name' => 'mode',
'label' => esc_html__( 'Mode', 'gravityformsauthorizenet' ),
'type' => 'radio',
'default_value' => 'test',
'choices' => array(
array(
'label' => esc_html__( 'Production', 'gravityformsauthorizenet' ),
'value' => 'production',
),
array(
'label' => esc_html__( 'Test', 'gravityformsauthorizenet' ),
'value' => 'test',
),
),
'horizontal' => true,
),
array(
'name' => 'loginId',
'label' => esc_html__( 'API Login ID', 'gravityformsauthorizenet' ),
'type' => 'api_login',
'class' => 'medium',
'feedback_callback' => array( $this, 'is_valid_plugin_key' ),
),
array(
'name' => 'transactionKey',
'label' => esc_html__( 'Transaction Key', 'gravityformsauthorizenet' ),
'type' => 'api_key',
'class' => 'medium',
'feedback_callback' => array( $this, 'is_valid_plugin_key' ),
),
),
),
array(
'title' => esc_html__( 'Automated Recurring Billing Setup', 'gravityformsauthorizenet' ),
'fields' => array(
array(
'name' => 'arb',
'label' => 'ARB',
'type' => 'checkbox',
'onchange' => "if(jQuery(this).prop('checked')){
jQuery('#gaddon-setting-row-automaticRetry').show();
} else {
jQuery('#gaddon-setting-row-automaticRetry').hide();
}",
'choices' => array(
array(
'label' => esc_html__( 'ARB is set up in my Authorize.Net account.', 'gravityformsauthorizenet' ),
'name' => 'arb'
)
),
),
array(
'name' => 'automaticRetry',
'label' => 'Automatic Retry',
'type' => 'checkbox',
'hidden' => ! $this->get_setting( 'arb' ),
'tooltip' => '<h6>' . esc_html__( 'Automatic Retry', 'gravityformsauthorizenet' ) . '</h6>' . esc_html__( 'Automatic Retry enhances Recurring Billing so you do not need to manually collect failed payments. With Automatic Retry, your customer\'s subscriptions will not terminate due to payment failures and will remain in a suspended status until you update the subscription\'s payment details. Once updated, Authorize.Net will automatically retry the failed payment in the subscription.' , 'gravityformsauthorizenet' ),
'choices' => array(
array(
'label' => esc_html__( 'Automatic Retry is turned on in my Authorize.Net account. To enable this feature in your Authorize.Net account, go to the Recurring Billing page under Tools and click on "Enable Automatic Retry" under Settings.', 'gravityformsauthorizenet' ),
'name' => 'automaticRetry'
)
),
),
),
array(
'type' => 'save',
'messages' => array( 'success' => esc_html__( 'Settings updated successfully', 'gravityformsauthorizenet' ) )
),
),
);
}
public function settings_api_login( $field, $echo = true ) {
$api_login_field = $this->settings_text( $field, false );
$caption = sprintf( esc_html__( 'You can find your unique %sAPI Login ID%s by clicking on the \'Account\' link at the Authorize.Net Merchant Interface. Then click \'API Login ID and Transaction Key\'. Your API Login ID will be displayed.', 'gravityformsauthorizenet' ), '<strong>', '</strong>' );
if ( $echo ) {
echo $api_login_field . '</br><small>' . $caption . '</small>';
}
return $api_login_field . '</br><small>' . $caption . '</small>';
}
public function settings_api_key( $field, $echo = true ) {
$api_key_field = $this->settings_text( $field, false );
$caption = sprintf( esc_html__( 'You can find your unique %sTransaction Key%s by clicking on the \'Account\' link at the Authorize.Net Merchant Interface. Then click \'API Login ID and Transaction Key\'. For security reasons, you cannot view your Transaction Key, but you will be able to generate a new one.', 'gravityformsauthorizenet' ), '<strong>', '</strong>' );
if ( $echo ) {
echo $api_key_field . '</br><small>' . $caption . '</small>';
}
return $api_key_field . '</br><small>' . $caption . '</small>';
}
public function is_valid_plugin_key() {
return $this->is_valid_key();
}
public function is_valid_custom_key() {
//get override settings
$apiSettingsEnabled = $this->get_setting( 'apiSettingsEnabled' );
if ( $apiSettingsEnabled ) {
$custom_settings['overrideMode'] = $this->get_setting( 'overrideMode' );
$custom_settings['overrideLogin'] = $this->get_setting( 'overrideLogin' );
$custom_settings['overrideKey'] = $this->get_setting( 'overrideKey' );
return $this->is_valid_key( $custom_settings );
}
return false;
}
public function is_valid_key( $settings = array() ) {
$auth = $this->get_aim( $settings );
$response = $auth->AuthorizeOnly();
$failure = $response->error;
$response_reason_code = $response->response_reason_code;
//13 - The merchant login ID or password is invalid or the account is inactive.
//103 -
if ( $failure && ( $response_reason_code == 13 || $response_reason_code == 103 ) ) {
$this->log_debug( __METHOD__ . '(): ' . $response->error_message );
return false;
} else {
return true;
}
}
private function get_aim( $local_api_settings = array() ) {
$this->include_api();
if ( ! empty( $local_api_settings ) ) {
$api_settings = array(
'login_id' => rgar( $local_api_settings, 'overrideLogin' ),
'transaction_key' => rgar( $local_api_settings, 'overrideKey' ),
'mode' => rgar( $local_api_settings, 'overrideMode' )
);
} else {
$api_settings = $this->get_api_settings( $local_api_settings );
}
$is_sandbox = $api_settings['mode'] == 'test';
if ( $is_sandbox ) {
$this->log_debug( __METHOD__ . '(): In test mode. Using the Authorize.net Sandbox.' );
}
$aim = new AuthorizeNetAIM( $api_settings['login_id'], $api_settings['transaction_key'] );
$aim->setSandbox( $is_sandbox );
return $aim;
}
private function include_api() {
if ( ! class_exists( 'AuthorizeNetRequest' ) ) {
require_once $this->get_base_path() . '/api/AuthorizeNet.php';
}
}
// function needed for cancel subscription and check_status where $this->current_feed is not available
private function get_local_api_settings( $feed ) {
if($feed['meta']['apiSettingsEnabled'])
{
$local_api_settings = array(
'login_id' => rgar( $feed['meta'], 'overrideLogin' ),
'transaction_key' => rgar( $feed['meta'], 'overrideKey' ),
'mode' => rgar( $feed['meta'], 'overrideMode' ));
}else{
$local_api_settings = array();
}
return $local_api_settings;
}
private function get_api_settings( $local_api_settings ) {
//for authorize.net, each feed can have its own login id and transaction key specified which overrides the master plugin one
//use the custom settings if found, otherwise use the master plugin settings
$apiSettingsEnabled = rgars( $this->current_feed, 'meta/apiSettingsEnabled' );
if ( $apiSettingsEnabled ) {
$login_id = $this->current_feed['meta']['overrideLogin'];
$transaction_key = $this->current_feed['meta']['overrideKey'];
$mode = $this->current_feed['meta']['overrideMode'];
return array( 'login_id' => $login_id, 'transaction_key' => $transaction_key, 'mode' => $mode );
} else {
$settings = $this->get_plugin_settings();
return array(
'login_id' => rgar( $settings, 'loginId' ),
'transaction_key' => rgar( $settings, 'transactionKey' ),
'mode' => rgar( $settings, 'mode' )
);
}
}
private function get_arb( $local_api_settings = array() ) {
if ( ! empty( $local_api_settings ) ) {
$this->log_debug( __METHOD__ . '(): Local API settings enabled.');
$api_settings = array(
'login_id' => rgar( $local_api_settings, 'login_id' ),
'transaction_key' => rgar( $local_api_settings, 'transaction_key' ),
'mode' => rgar( $local_api_settings, 'mode' )
);
} else {
$api_settings = $this->get_api_settings( $local_api_settings );
}
$this->include_api();
$is_sandbox = $api_settings['mode'] == 'test';
$arb = new AuthorizeNetARB( $api_settings['login_id'], $api_settings['transaction_key'] );
$arb->setSandbox( $is_sandbox );
return $arb;
}
//-------- Form Settings ---------
/**
* Prevent feeds being listed or created if the api keys aren't valid.
*
* @return bool
*/
public function can_create_feed() {
return $this->is_valid_plugin_key();
}
public function feed_settings_fields() {
$default_settings = parent::feed_settings_fields();
//remove default options before adding custom
$default_settings = parent::remove_field( 'options', $default_settings );
$fields = array(
array(
'name' => 'options',
'label' => esc_html__( 'Options', 'gravityformsauthorizenet' ),
'type' => 'options',
'tooltip' => '<h6>' . esc_html__( 'Options', 'gravityformsauthorizenet' ) . '</h6>' . esc_html__( 'Turn on or off the available Authorize.Net checkout options.', 'gravityformsauthorizenet' ),
),
);
//Add post fields if form has a post
$form = $this->get_current_form();
if ( GFCommon::has_post_field( $form['fields'] ) ) {
if ( $this->get_setting( 'transactionType' ) == 'subscription' ) {
$post_settings = array(
'name' => 'post_checkboxes',
'label' => esc_html__( 'Posts', 'gravityformsauthorizenet' ),
'type' => 'checkbox',
'tooltip' => '<h6>' . esc_html__( 'Posts', 'gravityformsauthorizenet' ) . '</h6>' . esc_html__( 'Enable this option if you would like to change the post status when a subscription is canceled.', 'gravityformsauthorizenet' ),
'choices' => array(
array(
'label' => esc_html__( 'Change post status when subscription is canceled.', 'gravityformsauthorizenet' ),
'name' => 'change_post_status',
'onChange' => 'var action = this.checked ? "draft" : ""; jQuery("#update_post_action").val(action);',
),
),
);
$fields[] = $post_settings;
}
}
$default_settings = $this->add_field_after( 'billingInformation', $fields, $default_settings );
$fields = array(
array(
'name' => 'apiSettingsEnabled',
'label' => esc_html__( 'API Settings', 'gravityformsauthorizenet' ),
'type' => 'checkbox',
'tooltip' => '<h6>' . esc_html__( 'API Settings', 'gravityformsauthorizenet' ) . '</h6>' . esc_html__( 'Override the settings provided on the Authorize.Net Settings page and use these instead for this feed.', 'gravityformsauthorizenet' ),
'onchange' => "if(jQuery(this).prop('checked')){
jQuery('#gaddon-setting-row-overrideMode').show();
jQuery('#gaddon-setting-row-overrideLogin').show();
jQuery('#gaddon-setting-row-overrideKey').show();
} else {
jQuery('#gaddon-setting-row-overrideMode').hide();
jQuery('#gaddon-setting-row-overrideLogin').hide();
jQuery('#gaddon-setting-row-overrideKey').hide();
jQuery('#overrideLogin').val('');
jQuery('#overrideKey').val('');
jQuery('i').removeClass('icon-check fa-check gf_valid');
}",
'choices' => array(
array(
'label' => esc_html__( 'Override Default Settings', 'gravityformsauthorizenet' ),
'name' => 'apiSettingsEnabled',
),
)
),
array(
'name' => 'overrideMode',
'label' => esc_html__( 'Mode', 'gravityformsauthorizenet' ),
'type' => 'radio',
'default_value' => 'test',
'hidden' => ! $this->get_setting( 'apiSettingsEnabled' ),
'tooltip' => '<h6>' . esc_html__( 'Mode', 'gravityformsauthorizenet' ) . '</h6>' . esc_html__( 'Select either Production or Test mode to override the chosen mode on the Authorize.Net Settings page.', 'gravityformsauthorizenet' ),
'choices' => array(
array(
'label' => esc_html__( 'Production', 'gravityformsauthorizenet' ),
'value' => 'production',
),
array(
'label' => esc_html__( 'Test', 'gravityformsauthorizenet' ),
'value' => 'test',
),
),
'horizontal' => true,
),
array(
'name' => 'overrideLogin',
'label' => esc_html__( 'API Login ID', 'gravityformsauthorizenet' ),
'type' => 'text',
'class' => 'medium',
'hidden' => ! $this->get_setting( 'apiSettingsEnabled' ),
'tooltip' => '<h6>' . esc_html__( 'API Login ID', 'gravityformsauthorizenet' ) . '</h6>' . esc_html__( 'Enter a new value to override the API Login ID on the Authorize.Net Settings page.', 'gravityformsauthorizenet' ),
'feedback_callback' => array( $this, 'is_valid_custom_key' ),
),
array(
'name' => 'overrideKey',
'label' => esc_html__( 'Transaction Key', 'gravityformsauthorizenet' ),
'type' => 'text',
'class' => 'medium',
'hidden' => ! $this->get_setting( 'apiSettingsEnabled' ),
'tooltip' => '<h6>' . esc_html__( 'Transaction Key', 'gravityformsauthorizenet' ) . '</h6>' . esc_html__( 'Enter a new value to override the Transaction Key on the Authorize.Net Settings page.', 'gravityformsauthorizenet' ),
'feedback_callback' => array( $this, 'is_valid_custom_key' ),
),
);
$default_settings = $this->add_field_after( 'conditionalLogic', $fields, $default_settings );
return $default_settings;
}
public function settings_options( $field, $echo = true ) {
$checkboxes = array(
'name' => 'options_checkboxes',
'type' => 'checkboxes',
'choices' => array(
array(
'label' => esc_html__( 'Send Authorize.Net email receipt.', 'gravityformsauthorizenet' ),
'name' => 'enableReceipt'
),
)
);
$html = $this->settings_checkbox( $checkboxes, false );
if ( $echo ) {
echo $html;
}
return $html;
}
public function checkbox_input_change_post_status( $choice, $attributes, $value, $tooltip ) {
$markup = $this->checkbox_input( $choice, $attributes, $value, $tooltip );
$dropdown_field = array(
'name' => 'update_post_action',
'choices' => array(
array( 'label' => '' ),
array( 'label' => esc_html__( 'Mark Post as Draft', 'gravityformsauthorizenet' ), 'value' => 'draft' ),
array( 'label' => esc_html__( 'Delete Post', 'gravityformsauthorizenet' ), 'value' => 'delete' ),
),
'onChange' => "var checked = jQuery(this).val() ? 'checked' : false; jQuery('#change_post_status').attr('checked', checked);",
);
$markup .= ' ' . $this->settings_select( $dropdown_field, false );
return $markup;
}
public function supported_billing_intervals() {
//authorize.net does not use years or weeks, override framework function
$billing_cycles = array(
'day' => array( 'label' => esc_html__( 'day(s)', 'gravityformsauthorizenet' ), 'min' => 7, 'max' => 365 ),
'month' => array( 'label' => esc_html__( 'month(s)', 'gravityformsauthorizenet' ), 'min' => 1, 'max' => 12 )
);
return $billing_cycles;
}
/**
* Append the phone field to the default billing_info_fields added by the framework.
*
* @return array
*/
public function billing_info_fields() {
$fields = parent::billing_info_fields();
$fields[] = array(
'name' => 'phone',
'label' => esc_html__( 'Phone', 'gravityformsauthorizenet' ),
'required' => false
);
return $fields;
}
/**
* Add supported notification events.
*
* @param array $form The form currently being processed.
*
* @return array
*/
public function supported_notification_events( $form ) {
if ( ! $this->has_feed( $form['id'] ) ) {
return false;
}
return array(
'complete_payment' => esc_html__( 'Payment Completed', 'gravityformsauthorizenet' ),
'create_subscription' => esc_html__( 'Subscription Created', 'gravityformsauthorizenet' ),
'cancel_subscription' => esc_html__( 'Subscription Canceled', 'gravityformsauthorizenet' ),
'expire_subscription' => esc_html__( 'Subscription Expired', 'gravityformsauthorizenet' ),
'add_subscription_payment' => esc_html__( 'Subscription Payment Added', 'gravityformsauthorizenet' ),
'fail_subscription_payment' => esc_html__( 'Subscription Payment Failed', 'gravityformsauthorizenet' ),
);
}
// used to upgrade old feeds into new version
public function get_old_feeds() {
global $wpdb;
$table_name = $wpdb->prefix . 'rg_authorizenet';
if ( ! $this->table_exists( $table_name ) ) {
return false;
}
$form_table_name = RGFormsModel::get_form_table_name();
$sql = "SELECT s.id, s.is_active, s.form_id, s.meta, f.title as form_title
FROM $table_name s
INNER JOIN $form_table_name f ON s.form_id = f.id";
$results = $wpdb->get_results( $sql, ARRAY_A );
$count = sizeof( $results );
$this->log_debug( __METHOD__ . "(): {$count} feed(s) found to copy." );
for ( $i = 0; $i < $count; $i ++ ) {
$results[ $i ]['meta'] = maybe_unserialize( $results[ $i ]['meta'] );
}
return $results;
}
public function convert_interval( $interval, $to_type ) {
//convert single character into long text for new feed settings or convert long text into single character for sending to paypal
//$to_type: text (change character to long text), OR char (change long text to character)
if ( empty( $interval ) ) {
return '';
}
$new_interval = '';
if ( $to_type == 'text' ) {
//convert single char to text
switch ( strtoupper( $interval ) ) {
case 'D' :
$new_interval = 'day';
break;
case 'W' :
$new_interval = 'week';
break;
case 'M' :
$new_interval = 'month';
break;
case 'Y' :
$new_interval = 'year';
break;
default :
$new_interval = $interval;
break;
}
} else {
//convert text to single char
switch ( strtolower( $interval ) ) {
case 'day' :
$new_interval = 'D';
break;
case 'week' :
$new_interval = 'W';
break;
case 'month' :
$new_interval = 'M';
break;
case 'year' :
$new_interval = 'Y';
break;
default :
$new_interval = $interval;
break;
}
}
return $new_interval;
}
public function upgrade( $previous_version ) {
if ( empty( $previous_version ) ) {
$previous_version = get_option( 'gf_authorizenet_version' );
}
$previous_is_pre_addon_framework = ! empty( $previous_version ) && version_compare( $previous_version, '2.0.dev1', '<' );
if ( $previous_is_pre_addon_framework ) {
$this->log_debug( __METHOD__ . '(): Copying over data to new table structure.' );
$old_feeds = $this->get_old_feeds();
if ( $old_feeds ) {
$this->log_debug( __METHOD__ . '(): Migrating feeds.' );
$counter = 1;
foreach ( $old_feeds as $old_feed ) {
$feed_name = 'Feed ' . $counter;
$form_id = $old_feed['form_id'];
$is_active = $old_feed['is_active'];
$customer_fields = rgar( $old_feed['meta'], 'customer_fields' );
$new_meta = array(
'feedName' => $feed_name,
'transactionType' => rgar( $old_feed['meta'], 'type' ),
'enableReceipt' => rgar( $old_feed['meta'], 'enable_receipt' ),
'change_post_status' => rgar( $old_feed['meta'], 'update_post_action' ) ? '1' : '0',
'update_post_action' => rgar( $old_feed['meta'], 'update_post_action' ),
'recurringAmount' => rgar( $old_feed['meta'], 'recurring_amount_field' ) == 'all' ? 'form_total' : rgar( $old_feed['meta'], 'recurring_amount_field' ),
'recurringTimes' => rgar( $old_feed['meta'], 'recurring_times' ),
'paymentAmount' => 'form_total',
//default to this for new field in framework version
'billingCycle_length' => rgar( $old_feed['meta'], 'billing_cycle_number' ),
'billingCycle_unit' => $this->convert_interval( rgar( $old_feed['meta'], 'billing_cycle_type' ), 'text' ),
'setupFee_enabled' => rgar( $old_feed['meta'], 'setup_fee_enabled' ),
'setupFee_product' => rgar( $old_feed['meta'], 'setup_fee_amount_field' ),
//trial_period_number is always set to 1 in the old version, no need to use trial_period_number in the database
'trial_enabled' => rgar( $old_feed['meta'], 'trial_period_enabled' ),
'trial_product' => 'enter_amount',
//default to this for new field in framework version
'trial_amount' => rgar( $old_feed['meta'], 'trial_amount' ),
'billingInformation_firstName' => rgar( $customer_fields, 'first_name' ),
'billingInformation_lastName' => rgar( $customer_fields, 'last_name' ),
'billingInformation_email' => rgar( $customer_fields, 'email' ),
'billingInformation_address' => rgar( $customer_fields, 'address1' ),
'billingInformation_address2' => rgar( $customer_fields, 'address2' ),
'billingInformation_city' => rgar( $customer_fields, 'city' ),
'billingInformation_state' => rgar( $customer_fields, 'state' ),
'billingInformation_zip' => rgar( $customer_fields, 'zip' ),
'billingInformation_country' => rgar( $customer_fields, 'country' ),
'apiSettingsEnabled' => rgar( $old_feed['meta'], 'api_settings_enabled' ),
'overrideMode' => rgar( $old_feed['meta'], 'api_mode' ),
'overrideLogin' => rgar( $old_feed['meta'], 'api_login' ),
'overrideKey' => rgar( $old_feed['meta'], 'api_key' ),
);
$optin_enabled = rgar( $old_feed['meta'], 'authorizenet_conditional_enabled' );
if ( $optin_enabled ) {
$new_meta['feed_condition_conditional_logic'] = 1;
$new_meta['feed_condition_conditional_logic_object'] = array(
'conditionalLogic' => array(
'actionType' => 'show',
'logicType' => 'all',
'rules' => array(
array(
'fieldId' => $old_feed['meta']['authorizenet_conditional_field_id'],
'operator' => $old_feed['meta']['authorizenet_conditional_operator'],
'value' => $old_feed['meta']['authorizenet_conditional_value'],
),
)
)
);
} else {
$new_meta['feed_condition_conditional_logic'] = 0;
}
$this->insert_feed( $form_id, $is_active, $new_meta );
$counter ++;
}
}
$old_settings = get_option( 'gf_authorizenet_settings' );
if ( ! empty( $old_settings ) ) {
$this->log_debug( __METHOD__ . '(): Copying plugin settings.' );
$new_settings = array(
'mode' => rgar( $old_settings, 'mode' ),
'loginId' => rgar( $old_settings, 'login_id' ),
'transactionKey' => rgar( $old_settings, 'transaction_key' ),
'arb' => rgar( $old_settings, 'arb_configured' ) == 'on' ? '1' : '0',
);
parent::update_plugin_settings( $new_settings );
}
//copy existing authorize.net transactions to new table
$this->copy_transactions();
}
}
public function copy_transactions() {
//copy transactions from the authorize.net transaction table to the addon payment transaction table
global $wpdb;
$old_table_name = $this->get_old_transaction_table_name();
if ( ! $this->table_exists( $old_table_name ) ) {
return false;
}
$this->log_debug( __METHOD__ . '(): Copying old Authorize.Net transactions into new table structure.' );
$new_table_name = $this->get_new_transaction_table_name();
$sql = "INSERT INTO {$new_table_name} (lead_id, transaction_type, transaction_id, is_recurring, amount, date_created)
SELECT entry_id, transaction_type, transaction_id, is_renewal, amount, date_created FROM {$old_table_name}";
$wpdb->query( $sql );
$this->log_debug( __METHOD__ . "(): Transactions: {$wpdb->rows_affected} rows were added." );
}
public function get_old_transaction_table_name() {
global $wpdb;
return $wpdb->prefix . 'rg_authorizenet_transaction';
}
public function get_new_transaction_table_name() {
global $wpdb;
return $wpdb->prefix . 'gf_addon_payment_transaction';
}
/**
* Get the Solution ID.
*
* @since 2.7.2
*
* @see https://developer.authorize.net/api/solution_id.html
*
* @param array $feed Feed settings.
* @return string $solution_id The solution ID.
*/
public function get_solution_id( $feed ) {
$local_settings = $this->get_local_api_settings( $feed );
$settings = $this->get_api_settings( $local_settings );
if ( 'test' == $settings['mode'] ) {
// Authorize.net's generic test Solution ID.
$solution_id = 'AAA100302';
} else {
$solution_id = 'AAA177921';
}
return $solution_id;
}
//------ AUTHORIZE AND CAPTURE SINGLE PAYMENT ------//
public function authorize( $feed, $submission_data, $form, $entry ) {
$original_transaction = $this->get_payment_transaction( $feed, $submission_data, $form, $entry );
$config = $this->get_config( $feed, $submission_data );
$form_data = $this->get_form_data( $submission_data, $form, $config );
$transaction = apply_filters( 'gform_authorizenet_transaction_pre_authorize', $original_transaction, $form_data, $config, $form );
$transaction = apply_filters( 'gform_authorizenet_transaction_pre_capture', $transaction, $form_data, $config, $form, $entry );
//Check if transaction is false after gform_authorizenet_transaction_pre_capture filter. If false, payment is not captured; run authorizeOnly transaction
if ( ! $transaction ) {
$this->log_debug( __METHOD__ . '(): Running authorization only. The gform_authorizenet_transaction_pre_capture filter was used to set the transaction to false.' );
$auth_amount = apply_filters( 'gform_authorizenet_amount_pre_authorize', $form_data['amount'] + floatval( $form_data['fee_amount'] ), $original_transaction, $form_data, $config, $form, $entry );
$response = $original_transaction->authorizeOnly( $auth_amount );
if ( $response->approved || $response->held ) {
$this->log_debug( __METHOD__ . "(): Authorization successful. Amount: {$response->amount}. Transaction Id: {$response->transaction_id}." );
$auth = array(
'is_authorized' => true,
'transaction_id' => $response->transaction_id,
);
$this->_args_for_deprecated_hooks = array(
'aim_response' => $response,
'config' => $config,
);
} else {
$this->log_error( __METHOD__ . '(): Authorization failed. Response => ' . print_r( $response->error_message, true ) );
// needed for filter backwards compatibility (gform_authorizenet_validation_message)
$error_message = $this->get_error_message( $_POST, $response, 'aim' );
$validation_result = $this->get_valiation_result( $form, $error_message );
$error_message = apply_filters( 'gform_authorizenet_validation_message', $error_message, $validation_result, $_POST, $response, 'aim' );
$auth = array(
'is_authorized' => false,
'transaction_id' => $response->transaction_id,
'error_message' => $error_message
);
}
return $auth;
}
//deprecated
$transaction = apply_filters( 'gform_authorizenet_before_single_payment', $transaction, $form_data, $config, $form );
$this->log_debug( __METHOD__ . '(): Capturing funds.' );
$response = $transaction->authorizeAndCapture();
if ( $response->approved || $response->held ) {
$this->log_debug( __METHOD__ . "(): Funds captured successfully. Amount: {$response->amount}. Transaction Id: {$response->transaction_id}." );
$auth = array(
'is_authorized' => true,
'transaction_id' => $response->transaction_id,
'captured_payment' => array(
'is_success' => true,
'error_message' => '',
'transaction_id' => $response->transaction_id,
'amount' => $response->amount
),
);
$this->_args_for_deprecated_hooks = array(
'aim_response' => $response,
'config' => $config,
);
} else {
if(!empty($response->error_message))
$this->log_error( __METHOD__ . '(): Funds could not be captured (error_message). Response => ' . print_r( $response->error_message, true ) );
else
$this->log_error( __METHOD__ . '(): Funds could not be captured (response_reason_text). Response => ' . $response->response_reason_text);
// needed for filter backwards compatibility (gform_authorizenet_validation_message)
$error_message = $this->get_error_message( $_POST, $response, 'aim' );
$validation_result = $this->get_valiation_result( $form, $error_message );
$error_message = apply_filters( 'gform_authorizenet_validation_message', $error_message, $validation_result, $_POST, $response, 'aim' );
$auth = array(
'is_authorized' => false,
'transaction_id' => $response->transaction_id,
'error_message' => $error_message,
'captured_payment' => array(
'is_success' => false,
)
);
}
return $auth;
}
public function get_payment_transaction( $feed, $submission_data, $form, $entry ) {
$transaction = $this->get_aim();
$feed_name = rgar( $feed['meta'], 'feedName' );
$this->log_debug( __METHOD__ . "(): Initializing new AuthorizeNetAIM object based on feed #{$feed['id']} - {$feed_name}." );
$transaction->amount = $submission_data['payment_amount'];
$transaction->card_num = $submission_data['card_number'];
$exp_date = str_pad( $submission_data['card_expiration_date'][0], 2, '0', STR_PAD_LEFT ) . '-' . $submission_data['card_expiration_date'][1];
$transaction->exp_date = $exp_date;
$transaction->card_code = $submission_data['card_security_code'];
$names = $this->get_first_last_name( $submission_data['card_name'] );
$transaction->first_name = $names['first_name'];
$transaction->last_name = $names['last_name'];
$transaction->address = trim( $submission_data['address'] . ' ' . $submission_data['address2'] );
$transaction->city = $submission_data['city'];
$transaction->state = $submission_data['state'];
$transaction->zip = $submission_data['zip'];
$transaction->country = $submission_data['country'];
$transaction->email = $submission_data['email'];
$transaction->description = $submission_data['form_title'];
$transaction->email_customer = $feed['meta']['enableReceipt'] == 1 ? 'true' : 'false';
$transaction->duplicate_window = 5;
$transaction->customer_ip = GFFormsModel::get_ip();
$transaction->invoice_num = empty( $invoice_number ) ? uniqid() : $invoice_number; //???
$transaction->phone = $submission_data['phone'];
$transaction->solution_id = $this->get_solution_id( $feed );
foreach ( $submission_data['line_items'] as $line_item ) {
$taxable = rgempty( 'taxable', $line_item ) ? 'Y' : $line_item['taxable'];
$transaction->addLineItem( $line_item['id'], $this->remove_spaces( $this->truncate( $line_item['name'], 31 ) ), $this->truncate( $line_item['description'], 255 ), $line_item['quantity'], GFCommon::to_number( $line_item['unit_price'] ), $taxable );
}
$this->log_debug( __METHOD__ . '(): $submission_data line_items => ' . print_r( $submission_data['line_items'], 1 ) );
return $transaction;
}
public function process_capture( $authorization, $feed, $submission_data, $form, $entry ) {
/**
* HOOK for backwards compatibility.
*
* @deprecated
*/
do_action( 'gform_authorizenet_post_capture', rgar( $authorization, 'is_authorized' ), rgars( $authorization, 'captured_payment/amount' ), $entry, $form, $this->_args_for_deprecated_hooks['config'], $this->_args_for_deprecated_hooks['aim_response'] );
return parent::process_capture( $authorization, $feed, $submission_data, $form, $entry );
}
public function subscribe( $feed, $submission_data, $form, $entry ) {
//Capture setup fee payment if needed
$fee_amount = $submission_data['setup_fee'];
$config = $this->get_config( $feed, $submission_data );
$form_data = $this->get_form_data( $submission_data, $form, $config );
$setup_fee_result = true;
$setup_payment_captured = false;
if ( ! empty( $fee_amount ) && $fee_amount > 0 ) {
//Getting transaction
$transaction = $this->get_payment_transaction( $feed, $submission_data, $form, $entry );
$transaction->amount = $fee_amount;
$transaction = apply_filters( 'gform_authorizenet_transaction_pre_capture_setup_fee', $transaction, $form_data, $config, $form, $entry );
//Capturing setup fee payment
$response = $transaction->authorizeAndCapture();
$this->log_debug( __METHOD__ . '(): Capturing setup fee payment. Response => ' . print_r( $response, true ) );
$setup_fee_result = $response->approved;
if ( $setup_fee_result ) {
$captured_payment = array(
'is_success' => true,
'transaction_id' => $response->transaction_id,
'amount' => $response->amount
);
$setup_payment_captured = true;
}
}
if ( $setup_fee_result ) {
//Create subscription.
$subscription = $this->get_subscription( $feed, $submission_data, $entry['id'] );
if ( has_filter( 'gform_authorizenet_subscription_pre_create' ) ) {
$this->log_debug( __METHOD__ . '(): Executing functions hooked to gform_authorizenet_subscription_pre_create.' );
$subscription = apply_filters( 'gform_authorizenet_subscription_pre_create', $subscription, $form_data, $config, $form, $entry );
}
//deprecated
if ( has_filter( 'gform_authorizenet_before_start_subscription' ) ) {
$this->log_debug( __METHOD__ . '(): Executing functions hooked to gform_authorizenet_before_start_subscription.' );
$subscription = apply_filters( 'gform_authorizenet_before_start_subscription', $subscription, $form_data, $config, $form );
}
if ( ! $subscription instanceof AuthorizeNet_Subscription ) {
return array( 'is_success' => false, 'error_message' => __( 'Unable to create subscription. Subscription object not available.', 'gravityformsauthorizenet' ) );
}
//Send subscription request.
$request = $this->get_arb();
$arb_response = $request->createSubscription( $subscription );
$this->log_debug( __METHOD__ . '(): Sending create subscription request. Response => ' . print_r( $arb_response, true ) );
if ( $arb_response->isOk() ) {
//Getting subscription ID
$subscription_id = $arb_response->getSubscriptionId();
$this->log_debug( __METHOD__ . "(): Subscription created successfully. Subscription Id: {$subscription_id}" );
$subscription_result = array(
'is_success' => true,
'subscription_id' => $subscription_id,
'amount' => $subscription->amount,
'subscription_trial_amount' => $subscription->trialAmount,
'subscription_start_date' => $subscription->startDate,
);
if ( $setup_payment_captured ) {
$subscription_result['captured_payment'] = $captured_payment;
}
$this->_args_for_deprecated_hooks = array(
'arb_subscription' => $subscription,
'arb_response' => $arb_response,
'config' => $config,
);
} else {
//Need to void setup fee transaction capture (if any) since subscription was not successfully created
$void_text = '';
if ( $setup_payment_captured ) {
//Void setup fee transaction
$void_transaction = $this->get_payment_transaction( $feed, $submission_data, $form, $entry );
$void_response = $transaction->void( $response->transaction_id );
if ( $void_response->approved ) {
$void_text = ' Initial setup fee transaction has been voided.';
}
}
// needed for filter backwards compatibility (gform_authorizenet_validation_message)
$error_message = $this->get_error_message( $_POST, $arb_response, 'arb' );
$validation_result = $this->get_valiation_result( $form, $error_message );
$error_message = apply_filters( 'gform_authorizenet_validation_message', $error_message, $validation_result, $_POST, $arb_response, 'arb' );
$this->log_debug( __METHOD__ . '(): There was an error creating Subscription.' . $void_text );
$subscription_result = array( 'is_success' => false, 'error_message' => $error_message );