-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathclass-wc-gateway-paystack.php
More file actions
1792 lines (1318 loc) · 57.1 KB
/
Copy pathclass-wc-gateway-paystack.php
File metadata and controls
1792 lines (1318 loc) · 57.1 KB
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
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WC_Gateway_Paystack extends WC_Payment_Gateway_CC {
/**
* Is test mode active?
*
* @var bool
*/
public $testmode;
/**
* Should orders be marked as complete after payment?
*
* @var bool
*/
public $autocomplete_order;
/**
* Paystack payment page type.
*
* @var string
*/
public $payment_page;
/**
* Paystack test public key.
*
* @var string
*/
public $test_public_key;
/**
* Paystack test secret key.
*
* @var string
*/
public $test_secret_key;
/**
* Paystack live public key.
*
* @var string
*/
public $live_public_key;
/**
* Paystack live secret key.
*
* @var string
*/
public $live_secret_key;
/**
* Should we save customer cards?
*
* @var bool
*/
public $saved_cards;
/**
* Should Paystack split payment be enabled.
*
* @var bool
*/
public $split_payment;
/**
* Should the cancel & remove order button be removed on the pay for order page.
*
* @var bool
*/
public $remove_cancel_order_button;
/**
* Paystack sub account code.
*
* @var string
*/
public $subaccount_code;
/**
* Who bears Paystack charges?
*
* @var string
*/
public $charges_account;
/**
* A flat fee to charge the sub account for each transaction.
*
* @var string
*/
public $transaction_charges;
/**
* Should custom metadata be enabled?
*
* @var bool
*/
public $custom_metadata;
/**
* Should the order id be sent as a custom metadata to Paystack?
*
* @var bool
*/
public $meta_order_id;
/**
* Should the customer name be sent as a custom metadata to Paystack?
*
* @var bool
*/
public $meta_name;
/**
* Should the billing email be sent as a custom metadata to Paystack?
*
* @var bool
*/
public $meta_email;
/**
* Should the billing phone be sent as a custom metadata to Paystack?
*
* @var bool
*/
public $meta_phone;
/**
* Should the billing address be sent as a custom metadata to Paystack?
*
* @var bool
*/
public $meta_billing_address;
/**
* Should the shipping address be sent as a custom metadata to Paystack?
*
* @var bool
*/
public $meta_shipping_address;
/**
* Should the order items be sent as a custom metadata to Paystack?
*
* @var bool
*/
public $meta_products;
/**
* API public key
*
* @var string
*/
public $public_key;
/**
* API secret key
*
* @var string
*/
public $secret_key;
/**
* Constructor
*/
public function __construct() {
$this->id = 'paystack';
$this->method_title = __( 'Paystack', 'woo-paystack' );
$this->method_description = sprintf( __( 'Paystack provide merchants with the tools and services needed to accept online payments from local and international customers using Mastercard, Visa, Verve Cards and Bank Accounts. <a href="%1$s" target="_blank">Sign up</a> for a Paystack account, and <a href="%2$s" target="_blank">get your API keys</a>.', 'woo-paystack' ), 'https://paystack.com', 'https://dashboard.paystack.com/#/settings/developer' );
$this->has_fields = true;
$this->payment_page = $this->get_option( 'payment_page' );
$this->supports = array(
'products',
'refunds',
'tokenization',
'subscriptions',
'multiple_subscriptions',
'subscription_cancellation',
'subscription_suspension',
'subscription_reactivation',
'subscription_amount_changes',
'subscription_date_changes',
'subscription_payment_method_change',
'subscription_payment_method_change_customer',
);
// Load the form fields
$this->init_form_fields();
// Load the settings
$this->init_settings();
// Get setting values
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = $this->get_option( 'testmode' ) === 'yes' ? true : false;
$this->autocomplete_order = $this->get_option( 'autocomplete_order' ) === 'yes' ? true : false;
$this->test_public_key = $this->get_option( 'test_public_key' );
$this->test_secret_key = $this->get_option( 'test_secret_key' );
$this->live_public_key = $this->get_option( 'live_public_key' );
$this->live_secret_key = $this->get_option( 'live_secret_key' );
$this->saved_cards = $this->get_option( 'saved_cards' ) === 'yes' ? true : false;
$this->split_payment = $this->get_option( 'split_payment' ) === 'yes' ? true : false;
$this->remove_cancel_order_button = $this->get_option( 'remove_cancel_order_button' ) === 'yes' ? true : false;
$this->subaccount_code = $this->get_option( 'subaccount_code' );
$this->charges_account = $this->get_option( 'split_payment_charge_account' );
$this->transaction_charges = $this->get_option( 'split_payment_transaction_charge' );
$this->custom_metadata = $this->get_option( 'custom_metadata' ) === 'yes' ? true : false;
$this->meta_order_id = $this->get_option( 'meta_order_id' ) === 'yes' ? true : false;
$this->meta_name = $this->get_option( 'meta_name' ) === 'yes' ? true : false;
$this->meta_email = $this->get_option( 'meta_email' ) === 'yes' ? true : false;
$this->meta_phone = $this->get_option( 'meta_phone' ) === 'yes' ? true : false;
$this->meta_billing_address = $this->get_option( 'meta_billing_address' ) === 'yes' ? true : false;
$this->meta_shipping_address = $this->get_option( 'meta_shipping_address' ) === 'yes' ? true : false;
$this->meta_products = $this->get_option( 'meta_products' ) === 'yes' ? true : false;
$this->public_key = $this->testmode ? $this->test_public_key : $this->live_public_key;
$this->secret_key = $this->testmode ? $this->test_secret_key : $this->live_secret_key;
// Hooks
add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
add_action(
'woocommerce_update_options_payment_gateways_' . $this->id,
array(
$this,
'process_admin_options',
)
);
add_action( 'woocommerce_receipt_' . $this->id, array( $this, 'receipt_page' ) );
// Payment listener/API hook.
add_action( 'woocommerce_api_wc_gateway_paystack', array( $this, 'verify_paystack_transaction' ) );
// Webhook listener/API hook.
add_action( 'woocommerce_api_tbz_wc_paystack_webhook', array( $this, 'process_webhooks' ) );
add_filter( 'woocommerce_gateway_' . $this->id . '_settings_values', array( $this, 'update_onboarding_settings' ) );
// Check if the gateway can be used.
if ( ! $this->is_valid_for_use() ) {
$this->enabled = false;
}
}
/**
* Check if this gateway is enabled and available in the user's country.
*/
public function is_valid_for_use() {
if ( ! in_array( get_woocommerce_currency(), apply_filters( 'woocommerce_paystack_supported_currencies', array( 'NGN', 'USD', 'ZAR', 'GHS' ) ) ) ) {
$this->msg = sprintf( __( 'Paystack does not support your store currency. Kindly set it to either NGN (₦), GHS (₵), USD ($) or ZAR (R) <a href="%s">here</a>', 'woo-paystack' ), admin_url( 'admin.php?page=wc-settings&tab=general' ) );
return false;
}
return true;
}
/**
* Display paystack payment icon.
*/
public function get_icon() {
$base_location = wc_get_base_location();
if ( 'GH' === $base_location['country'] ) {
$icon = '<img src="' . WC_HTTPS::force_https_url( plugins_url( 'assets/images/paystack-gh.png', WC_PAYSTACK_MAIN_FILE ) ) . '" alt="Paystack Payment Options" />';
} elseif ( 'ZA' === $base_location['country'] ) {
$icon = '<img src="' . WC_HTTPS::force_https_url( plugins_url( 'assets/images/paystack-za.png', WC_PAYSTACK_MAIN_FILE ) ) . '" alt="Paystack Payment Options" />';
} else {
$icon = '<img src="' . WC_HTTPS::force_https_url( plugins_url( 'assets/images/paystack-wc.png', WC_PAYSTACK_MAIN_FILE ) ) . '" alt="Paystack Payment Options" />';
}
return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
}
/**
* Check if Paystack merchant details is filled.
*/
public function admin_notices() {
if ( $this->enabled == 'no' ) {
return;
}
// Check required fields.
if ( ! ( $this->public_key && $this->secret_key ) ) {
echo '<div class="error"><p>' . sprintf( __( 'Please enter your Paystack merchant details <a href="%s">here</a> to be able to use the Paystack WooCommerce plugin.', 'woo-paystack' ), admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=paystack' ) ) . '</p></div>';
return;
}
}
/**
* Check if Paystack gateway is enabled.
*
* @return bool
*/
public function is_available() {
if ( 'yes' == $this->enabled ) {
if ( ! ( $this->public_key && $this->secret_key ) ) {
return false;
}
return true;
}
return false;
}
/**
* Admin Panel Options.
*/
public function admin_options() {
?>
<h2><?php _e( 'Paystack', 'woo-paystack' ); ?>
<?php
if ( function_exists( 'wc_back_link' ) ) {
wc_back_link( __( 'Return to payments', 'woo-paystack' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) );
}
?>
</h2>
<h4>
<strong><?php printf( __( 'Optional: To avoid situations where bad network makes it impossible to verify transactions, set your webhook URL <a href="%1$s" target="_blank" rel="noopener noreferrer">here</a> to the URL below<span style="color: red"><pre><code>%2$s</code></pre></span>', 'woo-paystack' ), 'https://dashboard.paystack.co/#/settings/developer', WC()->api_request_url( 'Tbz_WC_Paystack_Webhook' ) ); ?></strong>
</h4>
<?php
if ( $this->is_valid_for_use() ) {
echo '<table class="form-table">';
$this->generate_settings_html();
echo '</table>';
} else {
?>
<div class="inline error"><p><strong><?php _e( 'Paystack Payment Gateway Disabled', 'woo-paystack' ); ?></strong>: <?php echo $this->msg; ?></p></div>
<?php
}
}
/**
* Initialise Gateway Settings Form Fields.
*/
public function init_form_fields() {
$form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woo-paystack' ),
'label' => __( 'Enable Paystack', 'woo-paystack' ),
'type' => 'checkbox',
'description' => __( 'Enable Paystack as a payment option on the checkout page.', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'title' => array(
'title' => __( 'Title', 'woo-paystack' ),
'type' => 'text',
'description' => __( 'This controls the payment method title which the user sees during checkout.', 'woo-paystack' ),
'default' => __( 'Debit/Credit Cards', 'woo-paystack' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Description', 'woo-paystack' ),
'type' => 'textarea',
'description' => __( 'This controls the payment method description which the user sees during checkout.', 'woo-paystack' ),
'default' => __( 'Make payment using your debit and credit cards', 'woo-paystack' ),
'desc_tip' => true,
),
'testmode' => array(
'title' => __( 'Test mode', 'woo-paystack' ),
'label' => __( 'Enable Test Mode', 'woo-paystack' ),
'type' => 'checkbox',
'description' => __( 'Test mode enables you to test payments before going live. <br />Once the LIVE MODE is enabled on your Paystack account uncheck this.', 'woo-paystack' ),
'default' => 'yes',
'desc_tip' => true,
),
'payment_page' => array(
'title' => __( 'Payment Option', 'woo-paystack' ),
'type' => 'select',
'description' => __( 'Popup shows the payment popup on the page while Redirect will redirect the customer to Paystack to make payment.', 'woo-paystack' ),
'default' => '',
'desc_tip' => false,
'options' => array(
'' => __( 'Select One', 'woo-paystack' ),
'inline' => __( 'Popup', 'woo-paystack' ),
'redirect' => __( 'Redirect', 'woo-paystack' ),
),
),
'test_secret_key' => array(
'title' => __( 'Test Secret Key', 'woo-paystack' ),
'type' => 'text',
'description' => __( 'Enter your Test Secret Key here', 'woo-paystack' ),
'default' => '',
),
'test_public_key' => array(
'title' => __( 'Test Public Key', 'woo-paystack' ),
'type' => 'text',
'description' => __( 'Enter your Test Public Key here.', 'woo-paystack' ),
'default' => '',
),
'live_secret_key' => array(
'title' => __( 'Live Secret Key', 'woo-paystack' ),
'type' => 'text',
'description' => __( 'Enter your Live Secret Key here.', 'woo-paystack' ),
'default' => '',
),
'live_public_key' => array(
'title' => __( 'Live Public Key', 'woo-paystack' ),
'type' => 'text',
'description' => __( 'Enter your Live Public Key here.', 'woo-paystack' ),
'default' => '',
),
'autocomplete_order' => array(
'title' => __( 'Autocomplete Order After Payment', 'woo-paystack' ),
'label' => __( 'Autocomplete Order', 'woo-paystack' ),
'type' => 'checkbox',
'class' => 'wc-paystack-autocomplete-order',
'description' => __( 'If enabled, the order will be marked as complete after successful payment', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'remove_cancel_order_button' => array(
'title' => __( 'Remove Cancel Order & Restore Cart Button', 'woo-paystack' ),
'label' => __( 'Remove the cancel order & restore cart button on the pay for order page', 'woo-paystack' ),
'type' => 'checkbox',
'description' => '',
'default' => 'no',
),
'split_payment' => array(
'title' => __( 'Split Payment', 'woo-paystack' ),
'label' => __( 'Enable Split Payment', 'woo-paystack' ),
'type' => 'checkbox',
'description' => '',
'class' => 'woocommerce_paystack_split_payment',
'default' => 'no',
'desc_tip' => true,
),
'subaccount_code' => array(
'title' => __( 'Subaccount Code', 'woo-paystack' ),
'type' => 'text',
'description' => __( 'Enter the subaccount code here.', 'woo-paystack' ),
'class' => 'woocommerce_paystack_subaccount_code',
'default' => '',
),
'split_payment_transaction_charge' => array(
'title' => __( 'Split Payment Transaction Charge', 'woo-paystack' ),
'type' => 'number',
'description' => __( 'A flat fee to charge the subaccount for this transaction, in Naira (₦). This overrides the split percentage set when the subaccount was created. Ideally, you will need to use this if you are splitting in flat rates (since subaccount creation only allows for percentage split). e.g. 100 for a ₦100 flat fee.', 'woo-paystack' ),
'class' => __( 'woocommerce_paystack_split_payment_transaction_charge', 'woo-paystack' ),
'default' => '',
'custom_attributes' => array(
'min' => 1,
'step' => 0.1,
),
'desc_tip' => false,
),
'split_payment_charge_account' => array(
'title' => __( 'Paystack Charges Bearer', 'woo-paystack' ),
'type' => 'select',
'description' => __( 'Who bears Paystack charges?', 'woo-paystack' ),
'class' => 'woocommerce_paystack_split_payment_charge_account',
'default' => '',
'desc_tip' => false,
'options' => array(
'' => __( 'Select One', 'woo-paystack' ),
'account' => __( 'Account', 'woo-paystack' ),
'subaccount' => __( 'Subaccount', 'woo-paystack' ),
),
),
'custom_gateways' => array(
'title' => __( 'Additional Paystack Gateways', 'woo-paystack' ),
'type' => 'select',
'description' => __( 'Create additional custom Paystack based gateways. This allows you to create additional Paystack gateways using custom filters. You can create a gateway that accepts only verve cards, a gateway that accepts only bank payment, a gateway that accepts a specific bank issued cards.', 'woo-paystack' ),
'default' => '',
'desc_tip' => true,
'options' => array(
'' => __( 'Select One', 'woo-paystack' ),
'1' => __( '1 gateway', 'woo-paystack' ),
'2' => __( '2 gateways', 'woo-paystack' ),
'3' => __( '3 gateways', 'woo-paystack' ),
'4' => __( '4 gateways', 'woo-paystack' ),
'5' => __( '5 gateways', 'woo-paystack' ),
),
),
'saved_cards' => array(
'title' => __( 'Saved Cards', 'woo-paystack' ),
'label' => __( 'Enable Payment via Saved Cards', 'woo-paystack' ),
'type' => 'checkbox',
'description' => __( 'If enabled, users will be able to pay with a saved card during checkout. Card details are saved on Paystack servers, not on your store.<br>Note that you need to have a valid SSL certificate installed.', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'custom_metadata' => array(
'title' => __( 'Custom Metadata', 'woo-paystack' ),
'label' => __( 'Enable Custom Metadata', 'woo-paystack' ),
'type' => 'checkbox',
'class' => 'wc-paystack-metadata',
'description' => __( 'If enabled, you will be able to send more information about the order to Paystack.', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'meta_order_id' => array(
'title' => __( 'Order ID', 'woo-paystack' ),
'label' => __( 'Send Order ID', 'woo-paystack' ),
'type' => 'checkbox',
'class' => 'wc-paystack-meta-order-id',
'description' => __( 'If checked, the Order ID will be sent to Paystack', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'meta_name' => array(
'title' => __( 'Customer Name', 'woo-paystack' ),
'label' => __( 'Send Customer Name', 'woo-paystack' ),
'type' => 'checkbox',
'class' => 'wc-paystack-meta-name',
'description' => __( 'If checked, the customer full name will be sent to Paystack', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'meta_email' => array(
'title' => __( 'Customer Email', 'woo-paystack' ),
'label' => __( 'Send Customer Email', 'woo-paystack' ),
'type' => 'checkbox',
'class' => 'wc-paystack-meta-email',
'description' => __( 'If checked, the customer email address will be sent to Paystack', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'meta_phone' => array(
'title' => __( 'Customer Phone', 'woo-paystack' ),
'label' => __( 'Send Customer Phone', 'woo-paystack' ),
'type' => 'checkbox',
'class' => 'wc-paystack-meta-phone',
'description' => __( 'If checked, the customer phone will be sent to Paystack', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'meta_billing_address' => array(
'title' => __( 'Order Billing Address', 'woo-paystack' ),
'label' => __( 'Send Order Billing Address', 'woo-paystack' ),
'type' => 'checkbox',
'class' => 'wc-paystack-meta-billing-address',
'description' => __( 'If checked, the order billing address will be sent to Paystack', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'meta_shipping_address' => array(
'title' => __( 'Order Shipping Address', 'woo-paystack' ),
'label' => __( 'Send Order Shipping Address', 'woo-paystack' ),
'type' => 'checkbox',
'class' => 'wc-paystack-meta-shipping-address',
'description' => __( 'If checked, the order shipping address will be sent to Paystack', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
'meta_products' => array(
'title' => __( 'Product(s) Purchased', 'woo-paystack' ),
'label' => __( 'Send Product(s) Purchased', 'woo-paystack' ),
'type' => 'checkbox',
'class' => 'wc-paystack-meta-products',
'description' => __( 'If checked, the product(s) purchased will be sent to Paystack', 'woo-paystack' ),
'default' => 'no',
'desc_tip' => true,
),
);
if ( 'NGN' !== get_woocommerce_currency() ) {
unset( $form_fields['custom_gateways'] );
}
$this->form_fields = $form_fields;
}
/**
* Payment form on checkout page
*/
public function payment_fields() {
if ( $this->description ) {
echo wpautop( wptexturize( $this->description ) );
}
if ( ! is_ssl() ) {
return;
}
if ( $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards && is_user_logged_in() ) {
$this->tokenization_script();
$this->saved_payment_methods();
$this->save_payment_method_checkbox();
}
}
/**
* Outputs scripts used for paystack payment.
*/
public function payment_scripts() {
if ( ! is_checkout_pay_page() ) {
return;
}
if ( $this->enabled === 'no' ) {
return;
}
$order_key = urldecode( $_GET['key'] );
$order_id = absint( get_query_var( 'order-pay' ) );
$order = wc_get_order( $order_id );
$payment_method = method_exists( $order, 'get_payment_method' ) ? $order->get_payment_method() : $order->payment_method;
if ( $this->id !== $payment_method ) {
return;
}
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'paystack', 'https://js.paystack.co/v1/inline.js', array( 'jquery' ), WC_PAYSTACK_VERSION, false );
wp_enqueue_script( 'wc_paystack', plugins_url( 'assets/js/paystack' . $suffix . '.js', WC_PAYSTACK_MAIN_FILE ), array( 'jquery', 'paystack' ), WC_PAYSTACK_VERSION, false );
$paystack_params = array(
'key' => $this->public_key,
);
if ( is_checkout_pay_page() && get_query_var( 'order-pay' ) ) {
$email = method_exists( $order, 'get_billing_email' ) ? $order->get_billing_email() : $order->billing_email;
$amount = $order->get_total() * 100;
$txnref = $order_id . '_' . time();
$the_order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$the_order_key = method_exists( $order, 'get_order_key' ) ? $order->get_order_key() : $order->order_key;
$currency = method_exists( $order, 'get_currency' ) ? $order->get_currency() : $order->order_currency;
if ( $the_order_id == $order_id && $the_order_key == $order_key ) {
$paystack_params['email'] = $email;
$paystack_params['amount'] = $amount;
$paystack_params['txnref'] = $txnref;
$paystack_params['currency'] = $currency;
}
if ( $this->split_payment ) {
$paystack_params['subaccount_code'] = $this->subaccount_code;
$paystack_params['charges_account'] = $this->charges_account;
if ( empty( $this->transaction_charges ) ) {
$paystack_params['transaction_charges'] = '';
} else {
$paystack_params['transaction_charges'] = $this->transaction_charges * 100;
}
}
if ( $this->custom_metadata ) {
if ( $this->meta_order_id ) {
$paystack_params['meta_order_id'] = $order_id;
}
if ( $this->meta_name ) {
$first_name = method_exists( $order, 'get_billing_first_name' ) ? $order->get_billing_first_name() : $order->billing_first_name;
$last_name = method_exists( $order, 'get_billing_last_name' ) ? $order->get_billing_last_name() : $order->billing_last_name;
$paystack_params['meta_name'] = $first_name . ' ' . $last_name;
}
if ( $this->meta_email ) {
$paystack_params['meta_email'] = $email;
}
if ( $this->meta_phone ) {
$billing_phone = method_exists( $order, 'get_billing_phone' ) ? $order->get_billing_phone() : $order->billing_phone;
$paystack_params['meta_phone'] = $billing_phone;
}
if ( $this->meta_products ) {
$line_items = $order->get_items();
$products = '';
foreach ( $line_items as $item_id => $item ) {
$name = $item['name'];
$quantity = $item['qty'];
$products .= $name . ' (Qty: ' . $quantity . ')';
$products .= ' | ';
}
$products = rtrim( $products, ' | ' );
$paystack_params['meta_products'] = $products;
}
if ( $this->meta_billing_address ) {
$billing_address = $order->get_formatted_billing_address();
$billing_address = esc_html( preg_replace( '#<br\s*/?>#i', ', ', $billing_address ) );
$paystack_params['meta_billing_address'] = $billing_address;
}
if ( $this->meta_shipping_address ) {
$shipping_address = $order->get_formatted_shipping_address();
$shipping_address = esc_html( preg_replace( '#<br\s*/?>#i', ', ', $shipping_address ) );
if ( empty( $shipping_address ) ) {
$billing_address = $order->get_formatted_billing_address();
$billing_address = esc_html( preg_replace( '#<br\s*/?>#i', ', ', $billing_address ) );
$shipping_address = $billing_address;
}
$paystack_params['meta_shipping_address'] = $shipping_address;
}
}
update_post_meta( $order_id, '_paystack_txn_ref', $txnref );
}
wp_localize_script( 'wc_paystack', 'wc_paystack_params', $paystack_params );
}
/**
* Load admin scripts.
*/
public function admin_scripts() {
if ( 'woocommerce_page_wc-settings' !== get_current_screen()->id ) {
return;
}
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$paystack_admin_params = array(
'plugin_url' => WC_PAYSTACK_URL,
);
wp_enqueue_script( 'wc_paystack_admin', plugins_url( 'assets/js/paystack-admin' . $suffix . '.js', WC_PAYSTACK_MAIN_FILE ), array(), WC_PAYSTACK_VERSION, true );
wp_localize_script( 'wc_paystack_admin', 'wc_paystack_admin_params', $paystack_admin_params );
}
/**
* Process the payment.
*
* @param int $order_id
*
* @return array|void
*/
public function process_payment( $order_id ) {
if ( 'redirect' === $this->payment_page ) {
return $this->process_redirect_payment_option( $order_id );
} elseif ( isset( $_POST[ 'wc-' . $this->id . '-payment-token' ] ) && 'new' !== $_POST[ 'wc-' . $this->id . '-payment-token' ] ) {
$token_id = wc_clean( $_POST[ 'wc-' . $this->id . '-payment-token' ] );
$token = WC_Payment_Tokens::get( $token_id );
if ( $token->get_user_id() !== get_current_user_id() ) {
wc_add_notice( 'Invalid token ID', 'error' );
return;
} else {
$status = $this->process_token_payment( $token->get_token(), $order_id );
if ( $status ) {
$order = wc_get_order( $order_id );
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
);
}
}
} else {
if ( is_user_logged_in() && isset( $_POST[ 'wc-' . $this->id . '-new-payment-method' ] ) && true === (bool) $_POST[ 'wc-' . $this->id . '-new-payment-method' ] && $this->saved_cards ) {
update_post_meta( $order_id, '_wc_paystack_save_card', true );
}
$order = wc_get_order( $order_id );
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true ),
);
}
}
/**
* Process a redirect payment option payment.
*
* @since 5.7
* @param int $order_id
* @return array|void
*/
public function process_redirect_payment_option( $order_id ) {
$order = wc_get_order( $order_id );
$email = method_exists( $order, 'get_billing_email' ) ? $order->get_billing_email() : $order->billing_email;
$amount = $order->get_total() * 100;
$txnref = $order_id . '_' . time();
$currency = method_exists( $order, 'get_currency' ) ? $order->get_currency() : $order->order_currency;
$callback_url = WC()->api_request_url( 'WC_Gateway_Paystack' );
$payment_channels = $this->get_gateway_payment_channels( $order );
$paystack_params = array(
'amount' => $amount,
'email' => $email,
'currency' => $currency,
'reference' => $txnref,
'callback_url' => $callback_url,
);
if ( ! empty( $payment_channels ) ) {
$paystack_params['channels'] = $payment_channels;
}
if ( $this->split_payment ) {
$paystack_params['subaccount'] = $this->subaccount_code;
$paystack_params['bearer'] = $this->charges_account;
if ( empty( $this->transaction_charges ) ) {
$paystack_params['transaction_charge'] = '';
} else {
$paystack_params['transaction_charge'] = $this->transaction_charges * 100;
}
}
$paystack_params['metadata']['custom_fields'] = $this->get_custom_fields( $order_id );
$paystack_params['metadata']['cancel_action'] = wc_get_cart_url();
update_post_meta( $order_id, '_paystack_txn_ref', $txnref );
$paystack_url = 'https://api.paystack.co/transaction/initialize/';
$headers = array(
'Authorization' => 'Bearer ' . $this->secret_key,
'Content-Type' => 'application/json',
);
$args = array(
'headers' => $headers,
'timeout' => 60,
'body' => json_encode( $paystack_params ),
);
$request = wp_remote_post( $paystack_url, $args );
if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) {
$paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
return array(
'result' => 'success',
'redirect' => $paystack_response->data->authorization_url,
);
} else {
wc_add_notice( __( 'Unable to process payment try again', 'woo-paystack' ), 'error' );
return;
}
}
/**
* Process a token payment.
*
* @param $token
* @param $order_id
*
* @return bool
*/
public function process_token_payment( $token, $order_id ) {
if ( $token && $order_id ) {
$order = wc_get_order( $order_id );
$email = method_exists( $order, 'get_billing_email' ) ? $order->get_billing_email() : $order->billing_email;
$order_amount = method_exists( $order, 'get_total' ) ? $order->get_total() : $order->order_total;
$order_amount = $order_amount * 100;
$paystack_url = 'https://api.paystack.co/transaction/charge_authorization';
$headers = array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->secret_key,
);
$metadata['custom_fields'] = $this->get_custom_fields( $order_id );
$body = array(
'email' => $email,
'amount' => $order_amount,
'metadata' => $metadata,
'authorization_code' => $token,
);
$args = array(
'body' => json_encode( $body ),
'headers' => $headers,
'timeout' => 60,
);
$request = wp_remote_post( $paystack_url, $args );
if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) {
$paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
if ( 'success' == $paystack_response->data->status ) {
$order = wc_get_order( $order_id );
if ( in_array( $order->get_status(), array( 'processing', 'completed', 'on-hold' ) ) ) {
wp_redirect( $this->get_return_url( $order ) );
exit;
}