-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass-gf-paystack.php
2043 lines (1716 loc) · 71 KB
/
class-gf-paystack.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 payment add-on framework.
GFForms::include_payment_addon_framework();
/**
* Class GFPaystack
*
* Primary class to manage the Paystack add-on.
*
* @since 1.0
*
* @uses GFPaymentAddOn
*/
class GFPaystack extends GFPaymentAddOn
{
/**
* Contains an instance of this class, if available.
*
* @since 1.0
* @access private
*
* @used-by GFPaystack::get_instance()
*
* @var object $_instance If available, contains an instance of this class.
*/
private static $_instance = null;
/**
* Defines the version of the Paystack Add-On.
*
* @since 1.0
* @access protected
*
* @used-by GFPaystack::scripts()
*
* @var string $_version Contains the version, defined from paystack.php
*/
protected $_version = GF_PAYSTACK_VERSION;
/**
* Defines the minimum Gravity Forms version required.
*
* @since 1.0
* @access protected
*
* @var string $_min_gravityforms_version The minimum version required.
*/
protected $_min_gravityforms_version = '2.0';
/**
* Defines the plugin slug.
*
* @since 1.0
* @access protected
*
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gravityformspaystack';
/**
* Defines the main plugin file.
*
* @since 1.0
* @access protected
*
* @var string $_path The path to the main plugin file, relative to the plugins folder.
*/
protected $_path = 'gravityformspaystack/paystack.php';
/**
* Defines the full path to this class file.
*
* @since 1.0
* @access protected
*
* @var string $_full_path The full path.
*/
protected $_full_path = __FILE__;
/**
* Defines the URL where this Add-On can be found.
*
* @since 1.0
* @access protected
*
* @var string $_url The URL of the Add-On.
*/
protected $_url = 'https://paystack.com/docs/libraries-and-plugins/plugins#wordpress';
/**
* Defines the title of this Add-On.
*
* @since 1.0
* @access protected
*
* @var string $_title The title of the Add-On.
*/
protected $_title = 'Paystack Add-On for Gravity Forms';
/**
* Defines the short title of the Add-On.
*
* @since 1.0
* @access protected
*
* @var string $_short_title The short title.
*/
protected $_short_title = 'Paystack';
/**
* Defines if user will not be able to create feeds for a form until a credit card field has been added.
*
* @since 1.0
* @access protected
*
* @var bool $_requires_credit_card false.
*/
protected $_requires_credit_card = false;
/**
* Defines if callbacks/webhooks/IPN will be enabled and the appropriate database table will be created.
*
* @since 1.0
* @access protected
*
* @var bool $_supports_callbacks true
*/
protected $_supports_callbacks = true;
/**
* Paystack requires monetary amounts to be formatted as the smallest unit for the currency being used e.g. kobo, pesewas, cent
*
* @since 1.10.1
* @access protected
*
* @var bool $_requires_smallest_unit true
*/
protected $_requires_smallest_unit = true;
/**
* Defines the capability needed to access the Add-On settings page.
*
* @since 1.0
* @access protected
* @var string $_capabilities_settings_page The capability needed to access the Add-On settings page.
*/
protected $_capabilities_settings_page = 'gravityforms_paystack';
/**
* 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_paystack';
/**
* 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_paystack_uninstall';
/**
* Defines the capabilities needed for the Paystack Add-On
*
* @since 1.0
* @access protected
* @var array $_capabilities The capabilities needed for the Add-On
*/
protected $_capabilities = array('gravityforms_paystack', 'gravityforms_paystack_uninstall');
/**
* Holds the custom meta key currently being processed. Enables the key to be passed to the gform_paystack_field_value filter.
*
* @since 1.0
* @access protected
*
* @used-by GFPaystack::maybe_override_field_value()
*
* @var string $_current_meta_key The meta key currently being processed.
*/
protected $_current_meta_key = '';
/**
* Enable rate limits to log card errors etc.
*
* @since 1.0
*
* @var bool
*/
protected $_enable_rate_limits = true;
/**
* Paystack API Wrapper
*
* @var GFPaystackApi
*/
protected $paystack_api;
/**
* Get an instance of this class.
*
* @since 1.0
* @access public
*
* @uses GFPaystack
* @uses GFPaystack::$_instance
*
* @return object GFPaystack
*/
public static function get_instance()
{
if (null === self::$_instance) {
self::$_instance = new GFPaystack();
}
return self::$_instance;
}
/**
* Load the Paystack credit card field.
*
* @since 1.0
*/
public function pre_init()
{
// For form confirmation redirection, this must be called in `wp`,
// or confirmation redirect to a page would throw PHP fatal error.
// Run before calling parent method. We don't want to run anything else before displaying thank you page.
add_action('wp', array($this, 'maybe_thankyou_page'), 5);
parent::pre_init();
}
public function init()
{
parent::init();
add_filter('gform_currencies', array($this, 'add_paystack_currencies'));
}
// # PLUGIN SETTINGS -----------------------------------------------------------------------------------------------
/**
* Configures the settings which should be rendered on the add-on settings tab.
*
* @access public
*
* @used-by GFAddOn::maybe_save_plugin_settings()
* @used-by GFAddOn::plugin_settings_page()
* @uses GFPaystack::api_settings_fields()
* @uses GFPaystack::get_webhooks_section_description()
*
* @return array Plugin settings fields to add.
*/
public function plugin_settings_fields()
{
$fields = array(
array(
'title' => esc_html__('Configuration', 'gravityformspaystack'),
'fields' => $this->api_settings_fields(),
),
);
return $fields;
}
/**
* Define the settings which appear in the Paystack API section.
*
* @access public
*
* @used-by GFPaystack::plugin_settings_fields()
*
* @return array The API settings fields.
*/
public function api_settings_fields()
{
$api_mode = '';
if ($this->is_detail_page() && empty($api_mode)) {
$api_mode = $this->get_plugin_setting('api_mode');
}
$fields = array(
array(
'name' => 'api_mode',
'label' => esc_html__('Mode', 'gravityformspaystack'),
'type' => 'radio',
'default_value' => $api_mode,
'choices' => array(
array(
'label' => esc_html__('Live', 'gravityformspaystack'),
'value' => 'live',
),
array(
'label' => esc_html__('Test', 'gravityformspaystack'),
'value' => 'test',
),
),
'horizontal' => true,
)
);
$credentials_fields = array(
array(
'name' => 'test_public_key',
'label' => esc_html__('Test Public Key', 'gravityformspaystack'),
'type' => 'text',
'class' => 'medium',
),
array(
'name' => 'test_secret_key',
'label' => esc_html__('Test Secret Key', 'gravityformspaystack'),
'type' => 'text',
'class' => 'medium',
),
array(
'name' => 'live_public_key',
'label' => esc_html__('Live Public Key', 'gravityformspaystack'),
'type' => 'text',
'class' => 'medium',
),
array(
'name' => 'live_secret_key',
'label' => esc_html__('Live Secret Key', 'gravityformspaystack'),
'type' => 'text',
'class' => 'medium',
),
);
$fields = array_merge($fields, $credentials_fields);
$webhook_fields = array(
array(
'name' => 'webhooks_enabled',
'label' => esc_html__('Webhooks Enabled?', 'gravityformspaystack'),
'type' => 'checkbox',
'horizontal' => true,
'required' => ($this->get_current_feed_id() || !isset($_GET['fid'])) ? true : false,
'description' => $this->get_webhooks_section_description(),
'dependency' => $this->is_detail_page() ? array($this, 'is_feed_paystack_connect_enabled') : false,
'choices' => array(
array(
'label' => esc_html__('I have enabled the Gravity Forms webhook URL in my Paystack account.', 'gravityformspaystack'),
'value' => 1,
'name' => 'webhooks_enabled',
),
),
)
);
$fields = array_merge($fields, $webhook_fields);
return $fields;
}
/**
* Define the markup to be displayed for the webhooks section description.
*
* @since Unknown
* @access public
*
* @used-by GFPaystack::plugin_settings_fields()
* @uses GFPaystack::get_webhook_url()
*
* @return string HTML formatted webhooks description.
*/
public function get_webhooks_section_description()
{
ob_start();
?>
<a href="javascript:void(0);" onclick="tb_show('Webhook Instructions', '#TB_inline?width=500&inlineId=paystack-webhooks-instructions', '');" onkeypress="tb_show('Webhook Instructions', '#TB_inline?width=500&inlineId=paystack-webhooks-instructions', '');">
<?php esc_html_e('View Instructions', 'gravityformspaystack'); ?>
</a>
</p>
<div id="paystack-webhooks-instructions" style="display:none;">
<ol class="paystack-webhooks-instructions">
<li>
<p><?php esc_html_e('Click the following link and log in to access your Paystack Webhooks management page:', 'gravityformspaystack'); ?> </p>
<a href="https://dashboard.paystack.com/#/settings/developer" target="_blank">https://dashboard.paystack.com/#/settings/developer</a>
</li>
<li>
<p>
<?php esc_html_e('Enter the following URL in the "Webhook URL" field:', 'gravityformspaystack'); ?>
<br />
<code><?php echo $this->get_webhook_url($this->get_current_feed_id()); ?></code>
</p>
</li>
<li><?php esc_html_e('Save Changes.', 'gravityformspaystack'); ?></li>
</ol>
</div>
<?php
return ob_get_clean();
}
// # FEED SETTINGS -------------------------------------------------------------------------------------------------
/**
* Configures the settings which should be rendered on the feed edit page.
*
* @access public
*
* @uses GFPaymentAddOn::feed_settings_fields()
* @uses GFAddOn::replace_field()
* @uses GFAddOn::get_setting()
* @uses GFAddOn::add_field_after()
* @uses GFAddOn::remove_field()
* @uses GFAddOn::add_field_before()
*
* @return array The feed settings.
*/
public function feed_settings_fields()
{
// Get default payment feed settings fields.
$default_settings = parent::feed_settings_fields();
$fields = array(
array(
'name' => 'mode',
'label' => esc_html__('Mode', 'gravityformspaystack'),
'type' => 'radio',
'choices' => array(
array('id' => 'gf_paystack_live_mode', 'label' => esc_html__('Live', 'gravityformspaystack'), 'value' => 'live'),
array('id' => 'gf_paystack_test_mode', 'label' => esc_html__('Test', 'gravityformspaystack'), 'value' => 'test'),
),
'horizontal' => true,
'default_value' => 'live',
'tooltip' => '<h6>' . esc_html__('Mode', 'gravityformspaystack') . '</h6>' . esc_html__('Select Live to receive payments in production. Select Test for testing purposes when using the Paystack development sandbox.', 'gravityformspaystack')
),
);
$default_settings = parent::add_field_after('feedName', $fields, $default_settings);
// Add donation to transaction type drop down
// $transaction_type = parent::get_field('transactionType', $default_settings);
// $choices = $transaction_type['choices'];
// $add_donation = true;
// foreach ($choices as $choice) {
// // Add donation option if it does not already exist
// if ($choice['value'] == 'donation') {
// $add_donation = false;
// }
// }
// if ($add_donation) {
// // Add donation transaction type
// $choices[] = array('label' => __('Donations', 'gravityformspaystack'), 'value' => 'donation');
// }
// $transaction_type['choices'] = $choices;
// $default_settings = $this->replace_field('transactionType', $transaction_type, $default_settings);
// Add send invoice field if the feed transaction type is a subscription.
if ($this->get_setting('transactionType') === 'subscription') {
$invoice_settings = array(
'name' => 'sendInvoices',
'label' => esc_html__('Send Invoices', 'gravityformspaystack'),
'type' => 'checkbox',
'choices' => array(
array(
'label' => 'Check if you want invoices to be sent to your customers',
'name' => 'sendInvoices',
),
),
'tooltip' => '<h6>' . esc_html__('Send Invoices', 'gravityformspaystack') . '</h6>' . esc_html__('If enabled, Paystack can send an email invoice to customers.', 'gravityformspaystack'),
);
$default_settings = $this->add_field_before('setupFee', $invoice_settings, $default_settings);
}
// hide default display of setup fee, not used by Paystack
$default_settings = parent::remove_field('setupFee', $default_settings);
// // Prepare trial period field.
// $trial_period_field = array(
// 'name' => 'trialPeriod',
// 'label' => esc_html__('Trial Period', 'gravityformspaystack'),
// 'style' => 'width:40px;text-align:center;',
// 'type' => 'trial_period',
// 'after_input' => ' ' . esc_html__('days', 'gravityformspaystack'),
// 'validation_callback' => array($this, 'validate_trial_period'),
// );
// // Add trial period field.
// $default_settings = $this->add_field_after('trial', $trial_period_field, $default_settings);
// Hide default display of trial, not used by Paystack
$default_settings = parent::remove_field('trial', $default_settings);
// Add subscription name field.
$plan_name_field = array(
'name' => 'planName',
'label' => esc_html__('Plan Name', 'gravityformspaystack'),
'type' => 'text',
'class' => 'medium merge-tag-support mt-hide_all_fields mt-position-right',
'tooltip' => '<h6>' . esc_html__('Plan Name', 'gravityformspaystack') . '</h6>' . esc_html__('Enter a name for the subscription. It will be displayed on the payment form as well as the Paystack dashboard.', 'gravityformspaystack'),
);
$default_settings = $this->add_field_before('recurringAmount', $plan_name_field, $default_settings);
// Customer information fields.
$customer_info_field = array(
'name' => 'customerInformation',
'label' => esc_html__('Customer Information', 'gravityformspaystack'),
'type' => 'field_map',
'field_map' => array(
array(
'name' => 'email',
'label' => esc_html__('Email Address', 'gravityformspaystack'),
'required' => true,
'field_type' => array('email', 'hidden'),
'tooltip' => '<h6>' . esc_html__('Email', 'gravityformspaystack') . '</h6>' . esc_html__('You can specify an email field and it will be sent to the Paystack screen as the customer\'s email.', 'gravityformspaystack'),
),
array(
'name' => 'first_name',
'label' => esc_html__('First Name', 'gravityformspaystack'),
'required' => ($this->get_setting('transactionType') == 'subscription') ? true : false,
),
array(
'name' => 'last_name',
'label' => esc_html__('Last Name', 'gravityformspaystack'),
'required' => ($this->get_setting('transactionType') == 'subscription') ? true : false,
),
array(
'name' => 'phone',
'label' => esc_html__('Phone Number', 'gravityformspaystack'),
'required' => false,
),
),
);
if ($this->get_setting('transactionType') == 'subscription') {
$customer_info_field['field_map'][] = array(
'name' => 'recurring_times',
'label' => esc_html__('Recurring Times', 'gravityformspaystack'),
'required' => false,
'tooltip' => '<h6>' . esc_html__('Recurring Times', 'gravityformspaystack') . '</h6>' . esc_html__('(OPTIONAL). Setting this will allow users to select then number of times a charge should happen during a subscrption to a plan. This will override the "Recurring Times" option under "Subscription Settings" section above. It is recommended to create a select field. Values can be "2-100" or "0" for indefinite. In case an empty value or 1 is passed, the plugin will fall back to the other aforementioned "Recurring Times" option.', 'gravityformspaystack'),
);
}
// Add customer information fields.
$default_settings = $this->add_field_before('billingInformation', $customer_info_field, $default_settings);
// Prepare meta data field.
$custom_meta = array(
array(
'name' => 'metaData',
'label' => esc_html__('Metadata', 'gravityformspaystack'),
'type' => 'dynamic_field_map',
'limit' => 20,
'tooltip' => '<h6>' . esc_html__('Metadata', 'gravityformspaystack') . '</h6>' . esc_html__('You may send custom meta information to Paystack. A maximum of 20 custom keys may be sent.', 'gravityformspaystack'),
'validation_callback' => array($this, 'validate_custom_meta'),
),
);
// Add meta data field.
$default_settings = $this->add_field_after('billingInformation', $custom_meta, $default_settings);
// hide default display of billing Information if transaction type is donation
if ($this->get_setting('transactionType') === 'donation') {
$default_settings = parent::remove_field('billingInformation', $default_settings);
}
return $default_settings;
}
/**
* Define the markup for the billing_cycle type field.
*
* @access public
*
* @return array The feed settings.
*/
public function settings_billing_cycle($field, $echo = true)
{
$intervals = $this->supported_billing_intervals();
$choices = array();
foreach ($intervals as $unit => $interval) {
if (!empty($interval)) {
$choices[] = array('value' => $unit, 'label' => $interval['label']);
}
}
//Interval drop down
$interval_field = array(
'name' => $field['name'] . '_unit',
'type' => 'select',
'onchange' => "loadBillingLength('" . esc_attr($field['name']) . "')",
'choices' => $choices,
);
$html = ' ' . $this->settings_select($interval_field, false);
$html .= "<script type='text/javascript'>var " . $field['name'] . '_intervals = ' . json_encode($intervals) . ';</script>';
if ($echo) {
echo $html;
}
return $html;
}
/**
* Define the choices available in the billing cycle dropdowns.
*
* @access public
*
* @used-by GFPaymentAddOn::settings_billing_cycle()
*
* @return array Billing intervals that are supported.
*/
public function supported_billing_intervals()
{
return array(
'hourly' => array('label' => esc_html__('Hourly', 'gravityformspaystack')),
'daily' => array('label' => esc_html__('Daily', 'gravityformspaystack')),
'weekly' => array('label' => esc_html__('Weekly', 'gravityformspaystack')),
'monthly' => array('label' => esc_html__('Monthly', 'gravityformspaystack')),
'annually' => array('label' => esc_html__('Annually', 'gravityformspaystack')),
'biannually' => array('label' => esc_html__('Biannually', 'gravityformspaystack')),
);
}
/**
* Define the markup for the trial type field.
*
* @since Unknown
* @access public
*
* @uses GFAddOn::settings_checkbox()
*
* @param array $field The field properties.
* @param bool|true $echo Should the setting markup be echoed. Defaults to true.
*
* @return string|void The HTML markup if $echo is set to false. Void otherwise.
*/
public function settings_trial($field, $echo = true)
{
// Prepare enabled field settings.
$enabled_field = array(
'name' => $field['name'] . '_checkbox',
'type' => 'checkbox',
'horizontal' => true,
'choices' => array(
array(
'label' => esc_html__('Enabled', 'gravityformspaystack'),
'name' => $field['name'] . '_enabled',
'value' => '1',
'onchange' => "if(jQuery(this).prop('checked')){
jQuery('#gaddon-setting-row-trialPeriod').show('slow');
} else {
jQuery('#gaddon-setting-row-trialPeriod').hide('slow');
jQuery('#trialPeriod').val( '' );
}",
),
),
);
// Get checkbox markup.
$html = $this->settings_checkbox($enabled_field, false);
// Echo setting markup, if enabled.
if ($echo) {
echo $html;
}
return $html;
}
/**
* Define the markup for the trial_period type field.
*
* @since Unknown
* @access public
*
* @uses GFAddOn::settings_text()
* @uses GFAddOn::field_failed_validation()
* @uses GFAddOn::get_error_icon()
*
* @param array $field The field properties.
* @param bool|true $echo Should the setting markup be echoed. Defaults to true.
*
* @return string|void The HTML markup if $echo is set to false. Void otherwise.
*/
public function settings_trial_period($field, $echo = true)
{
// Get text input markup.
$html = $this->settings_text($field, false);
// Prepare validation placeholder name.
$validation_placeholder = array('name' => 'trialValidationPlaceholder');
// Add validation indicator.
if ($this->field_failed_validation($validation_placeholder)) {
$html .= ' ' . $this->get_error_icon($validation_placeholder);
}
// If trial is not enabled and setup fee is enabled, hide field.
$html .= '
<script type="text/javascript">
if( ! jQuery( "#trial_enabled" ).is( ":checked" ) || jQuery( "#setupFee_enabled" ).is( ":checked" ) ) {
jQuery( "#trial_enabled" ).prop( "checked", false );
jQuery( "#gaddon-setting-row-trialPeriod" ).hide();
}
</script>';
// Echo setting markup, if enabled.
if ($echo) {
echo $html;
}
return $html;
}
/**
* Prepare fields for field mapping in feed settings.
*
* @return array $fields
*/
public function billing_info_fields()
{
$fields = array(
array(
'name' => 'address_line1',
'label' => __('Address', 'gravityformsconstantcontact'),
'required' => false,
'field_type' => array('address'),
),
array(
'name' => 'address_line2',
'label' => __('Address 2', 'gravityformsconstantcontact'),
'required' => false,
'field_type' => array('address'),
),
array(
'name' => 'address_city',
'label' => __('City', 'gravityformsconstantcontact'),
'required' => false,
'field_type' => array('address'),
),
array(
'name' => 'address_state',
'label' => __('State', 'gravityformsconstantcontact'),
'required' => false,
'field_type' => array('address'),
),
array(
'name' => 'address_zip',
'label' => __('Zip', 'gravityformsconstantcontact'),
'required' => false,
'field_type' => array('address'),
),
array(
'name' => 'address_country',
'label' => __('Country', 'gravityformsconstantcontact'),
'required' => false,
'field_type' => array('address'),
),
);
return $fields;
}
/**
* Validate the custom_meta type field.
*
* @access public
*
* @uses GFAddOn::get_posted_settings()
* @uses GFAddOn::set_field_error()
*
* @param array $field The field properties. Not used.
*
* @return void
*/
public function validate_custom_meta($field)
{
/*
* Number of keys is limited to 20.
* Interface should control this, validating just in case.
* Key names have maximum length of 40 characters.
*/
// Get metadata from posted settings.
$settings = $this->get_posted_settings();
$meta_data = $settings['metaData'];
// If metadata is not defined, return.
if (empty($meta_data)) {
return;
}
// Get number of metadata items.
$meta_count = count($meta_data);
// If there are more than 20 metadata keys, set field error.
if ($meta_count > 20) {
$this->set_field_error(array(esc_html__('You may only have 20 custom keys.'), 'gravityformspaystack'));
return;
}
// Loop through metadata and check the key name length (custom_key).
foreach ($meta_data as $meta) {
if (empty($meta['custom_key']) && !empty($meta['value'])) {
$this->set_field_error(array('name' => 'metaData'), esc_html__("A field has been mapped to a custom key without a name. Please enter a name for the custom key, remove the metadata item, or return the corresponding drop down to 'Select a Field'.", 'gravityformspaystack'));
break;
} else if (strlen($meta['custom_key']) > 40) {
$this->set_field_error(array('name' => 'metaData'), sprintf(esc_html__('The name of custom key %s is too long. Please shorten this to 40 characters or less.', 'gravityformspaystack'), $meta['custom_key']));
break;
}
}
}
/**
* Prevent the 'options' checkboxes setting being included on the feed.
*
* @access public
*
* @used-by GFPaymentAddOn::other_settings_fields()
*
* @return false
*/
public function option_choices()
{
return false;
}
/**
* Add supported notification events.
*
* @access public
*
* @used-by GFFeedAddOn::notification_events()
* @uses GFFeedAddOn::has_feed()
*
* @param array $form The form currently being processed.
*
* @return array|false The supported notification events. False if feed cannot be found within $form.
*/
public function supported_notification_events($form)
{
// If this form does not have a Paystack feed, return false.
if (!$this->has_feed($form['id'])) {
return false;
}
// Return Paystack notification events.
return array(
'complete_payment' => esc_html__('Payment Completed', 'gravityformspaystack'),
'refund_payment' => esc_html__('Payment Refunded', 'gravityformspaystack'),
'fail_payment' => esc_html__('Payment Failed', 'gravityformspaystack'),
'create_subscription' => esc_html__('Subscription Created', 'gravityformspaystack'),
'cancel_subscription' => esc_html__('Subscription Canceled', 'gravityformspaystack'),
'add_subscription_payment' => esc_html__('Subscription Payment Added', 'gravityformspaystack'),
'fail_subscription_payment' => esc_html__('Subscription Payment Failed', 'gravityformspaystack'),
);
}
// # PAYSTACK TRANSACTIONS -------------------------------------------------------------------------------------------
/**
* Useful when developing a payment gateway that processes the payment outside of the website (i.e. Paystack Redirect).
*
* @since Unknown
* @access public
*
* @used-by GFPaymentAddOn::entry_post_save()
*
* @param array $feed Active payment feed containing all the configuration data.
* @param array $submission_data Contains form field data submitted by the user as well as payment information (i.e. payment amount, setup fee, line items, etc...).
* @param array $form Current form array containing all form settings.
* @param array $entry Current entry array containing entry information (i.e data submitted by users).
*
* @return void|string Return a full URL (including http:// or https://) to the payment processor.
*/
public function redirect_url($feed, $submission_data, $form, $entry)
{
// Don't process redirect url if request is a Paystack return
if (!rgempty('gf_paystack_return', $_GET)) {
return false;
}
// Getting mode (Live (Production) or Test (Sandbox))
$mode = $feed['meta']['mode'];
// Setup the Paystack API
$this->paystack_api($mode);
// Get the transaction type
$transaction_type = $feed['meta']['transactionType'];
// Getting the product status
$is_product = $feed['meta']['transactionType'] === 'product';
// Getting the subscription status
$is_subscription = $feed['meta']['transactionType'] === 'subscription';
// URL that will listen to callback from Paystack
$page_url = get_bloginfo('url');
$ids_query = "ids={$entry['id']}|{$feed['id']}|{$form['id']}";
$ids_query .= '&hash=' . wp_hash($ids_query);
$return_url = add_query_arg('gf_paystack_return', base64_encode($ids_query), $page_url);
// $setup_fee = rgar($submission_data, 'setup_fee');
// $trial_amount = rgar($submission_data, 'trial');
$payment_amount = rgar($submission_data, 'payment_amount');
// Currency
$currency = rgar($entry, 'currency');
// Customer Info
$customer_info = $this->get_fields_meta_data($feed, $entry, $this->get_customer_fields());
// Get feed custom metadata
$custom_data = $this->get_paystack_meta_data($feed, $entry, $form);
// $custom_data[] = [
// 'display_name' => 'Plugin Name',
// 'variable_name' => 'plugin_name',
// 'value' => $this->paystack_api->plugin_name
// ];
$custom_data[] = [
'display_name' => 'Plugin Name',
'variable_name' => 'plugin_name',
'value' => 'pstk-gravityforms'
];
// Generate transaction reference
$reference = uniqid("gf-{$entry['id']}-");
// Updating lead's payment_status to Processing
GFAPI::update_entry_property($entry['id'], 'payment_status', 'Processing');
// Prepare transaction data
$args = array(
'email' => $customer_info['email'],
'currency' => $currency,
'amount' => $this->get_amount_export($payment_amount, $currency),
'reference' => $reference,
'callback_url' => $return_url,
'description' => sprintf(__('%s (transaction: %s)', 'paystack'), $feed['meta']['feedName'], $reference),
'metadata' => array(
'entry_id' => $entry['id'],
'site_url' => esc_url(get_site_url()),
'ip_address' => $_SERVER['REMOTE_ADDR'],
'custom_fields' => $custom_data
)
);
if ($is_product) {
$line_items = rgar($submission_data, 'line_items');
$discounts = rgar($submission_data, 'discounts');
// Create a Payment Request & Send Invoice
}
if ($is_subscription) {
$plan = $this->create_plan($feed, $payment_amount, $currency);
if (is_wp_error($plan)) {
return false;
}
$args['plan'] = $plan['plan_code'];
$submitted_recurring_times = rgar($submission_data, 'recurring_times');
$invoice_limit = !empty($submitted_recurring_times) && $submitted_recurring_times !== '1' ? $submitted_recurring_times : null;
if (!empty($invoice_limit) || $invoice_limit !== 'Infinite') {
$args['invoice_limit'] = (int) $invoice_limit;
}
$args['channels'] = ['card'];
gform_update_meta($entry['id'], 'paystack_plan_code', $plan['plan_code']);
}
// Initialize the charge on Paystack's servers - this will be used to charge the user's card
$response = (object) $this->paystack_api->send_request("transaction/initialize/", $args);
$this->log_debug(__METHOD__ . "(): Initialize Paystack Transaction:" . print_r($response, true));
if (!$response->status) {
return false;
}