forked from vippsas/vipps-recurring-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woo-vipps-recurring.php
executable file
·861 lines (719 loc) · 29.2 KB
/
woo-vipps-recurring.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
<?php
/**
* Plugin Name: Vipps Recurring Payments Gateway for WooCommerce
* Description: Offer recurring payments with Vipps for WooCommerce Subscriptions
* Author: Everyday AS
* Author URI: https://everyday.no
* Version: 1.15.0
* Requires at least: 4.4
* Tested up to: 6.2
* WC tested up to: 7.2
* Text Domain: woo-vipps-recurring
* Domain Path: /languages
*/
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.Files.FileName
define( 'WC_VIPPS_RECURRING_VERSION', '1.15.0' );
add_action( 'plugins_loaded', 'woocommerce_gateway_vipps_recurring_init' );
/**
* Polyfills
*/
if ( ! function_exists( 'array_key_first' ) ) {
function array_key_first( array $arr ) {
foreach ( $arr as $key => $unused ) {
return $key;
}
return null;
}
}
if ( ! function_exists( 'array_key_last' ) ) {
function array_key_last( array $array ) {
end( $array );
return key( $array );
}
}
/**
* Activation hooks
*/
register_activation_hook( __FILE__, 'woocommerce_gateway_vipps_recurring_activate' );
function woocommerce_gateway_vipps_recurring_activate() {
add_option( 'woo-vipps-recurring-version', WC_VIPPS_RECURRING_VERSION );
}
/**
* Initialize our plugin
*/
function woocommerce_gateway_vipps_recurring_init() {
$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
$active_site_plugins = apply_filters( 'active_sitewide_plugins', get_site_option( 'active_sitewide_plugins' ) );
if ( $active_site_plugins ) {
$active_plugins = array_merge( $active_plugins, array_keys( $active_site_plugins ) );
}
if ( ! in_array( 'woocommerce/woocommerce.php', $active_plugins, true ) ) {
return;
}
load_plugin_textdomain( 'woo-vipps-recurring', false, plugin_basename( __DIR__ ) . '/languages' );
if ( ! class_exists( 'WC_Vipps_Recurring' ) ) {
/*
* Required minimums and constants
*/
define( 'WC_VIPPS_RECURRING_MIN_PHP_VER', '7.4.0' );
define( 'WC_VIPPS_RECURRING_MIN_WC_VER', '3.0.0' );
define( 'WC_VIPPS_RECURRING_MAIN_FILE', __FILE__ );
define( 'WC_VIPPS_RECURRING_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
define( 'WC_VIPPS_RECURRING_PLUGIN_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
/*
* Amount of days to retry a payment when creating a charge in the Vipps API
*/
if ( ! defined( 'WC_VIPPS_RECURRING_RETRY_DAYS' ) ) {
define( 'WC_VIPPS_RECURRING_RETRY_DAYS', 4 );
}
/*
* Whether to put the plugin into test mode. This is only useful for developers.
*/
if ( ! defined( 'WC_VIPPS_RECURRING_TEST_MODE' ) ) {
define( 'WC_VIPPS_RECURRING_TEST_MODE', false );
}
class WC_Vipps_Recurring {
/**
* The reference the *Singleton* instance of this class
*/
private static ?WC_Vipps_Recurring $instance = null;
public WC_Vipps_Recurring_Admin_Notices $notices;
public WC_Gateway_Vipps_Recurring $gateway;
/**
* Returns the *Singleton* instance of this class.
*
* @return WC_Vipps_Recurring
*/
public static function get_instance(): WC_Vipps_Recurring {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Private clone method to prevent cloning of the instance of the
* *Singleton* instance.
*
* @return void
*/
private function __clone() {
}
/**
* Private un-serialize method to prevent un-serializing of the *Singleton*
* instance.
*
* @return void
*/
public function __wakeup() {
}
/**
* Protected constructor to prevent creating a new instance of the
* *Singleton* via the `new` operator from outside of this class.
*/
private function __construct() {
add_action( 'admin_init', [ $this, 'install' ] );
$this->init();
}
/**
* Init the plugin after plugins_loaded so environment variables are set.
*
* @since 1.0.0
* @version 4.0.0
*/
public function init() {
require_once __DIR__ . '/includes/wc-vipps-recurring-helper.php';
require_once __DIR__ . '/includes/wc-vipps-recurring-logger.php';
require_once __DIR__ . '/includes/wc-gateway-vipps-recurring.php';
require_once __DIR__ . '/includes/wc-vipps-recurring-admin-notices.php';
$this->notices = WC_Vipps_Recurring_Admin_Notices::get_instance( __FILE__ );
$this->gateway = WC_Gateway_Vipps_Recurring::get_instance();
add_action( 'wp_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ] );
if ( is_admin() ) {
add_action( 'admin_init', [ $this, 'admin_init' ] );
add_action( 'admin_menu', [ $this, 'admin_menu' ] );
add_action( 'wp_ajax_vipps_recurring_force_check_charge_statuses', [
$this,
'wp_ajax_vipps_recurring_force_check_charge_statuses'
] );
}
// add our gateway
add_filter( 'woocommerce_payment_gateways', [ $this, 'add_gateways' ] );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), [
$this,
'plugin_action_links'
] );
// add custom cron schedules for Vipps charge polling
add_filter( 'cron_schedules', [
$this,
'woocommerce_vipps_recurring_add_cron_schedules'
] );
// testing code
// if ( WC_VIPPS_RECURRING_TEST_MODE ) {
// $agreement = new WC_Vipps_Agreement( [
// "start" => "2022-09-29T09:48:02Z",
// "stop" => null,
// "status" => "ACTIVE",
// "pricing" => [
// "type" => "LEGACY",
// "currency" => "NOK",
// "amount" => 14900
// ],
// "productName" => "This is a name of a really long product wh...",
// "productDescription" => "[På vent] This is a name of a really long product which will be truncated",
// "interval" => [
// "unit" => "MONTH",
// "count" => 1,
// "text" => "every month"
// ],
// "campaign" => [
// "price" => 12665,
// "end" => "2022-10-29T09:47:45Z",
// "explanation" => "Original price 149 kr
//starts October 29",
// "type" => "LEGACY_CAMPAIGN"
// ],
// "sub" => null,
// "userinfoUrl" => null,
// "merchantAgreementUrl" => "https://8e9f-141-0-97-106.eu.ngrok.io/my-account/",
// "id" => "agr_GqnvsHY"
// ] );
//
// die(var_dump($agreement->to_array()));
// add_action( 'wp_loaded', [
// $this,
// 'check_order_statuses'
// ] );
// }
// end testing code
// schedule recurring payment charge status checking event
if ( ! wp_next_scheduled( 'woocommerce_vipps_recurring_check_order_statuses' ) ) {
wp_schedule_event( time(), 'one_minute', 'woocommerce_vipps_recurring_check_order_statuses' );
}
add_action( 'woocommerce_vipps_recurring_check_order_statuses', [
$this,
'check_order_statuses'
] );
// schedule checking if gateway change went through
if ( ! wp_next_scheduled( 'woocommerce_vipps_recurring_check_gateway_change_request' ) ) {
wp_schedule_event( time(), 'one_minute', 'woocommerce_vipps_recurring_check_gateway_change_request' );
}
add_action( 'woocommerce_vipps_recurring_check_gateway_change_request', [
$this,
'check_gateway_change_agreement_statuses'
] );
// schedule checking for updating payment details
if ( ! wp_next_scheduled( 'woocommerce_vipps_recurring_update_subscription_details_in_app' ) ) {
wp_schedule_event( time(), 'one_minute', 'woocommerce_vipps_recurring_update_subscription_details_in_app' );
}
add_action( 'woocommerce_vipps_recurring_update_subscription_details_in_app', [
$this,
'update_subscription_details_in_app'
] );
// Add custom product settings for Vipps Recurring.
add_filter( 'woocommerce_product_data_tabs', [ $this, 'woocommerce_product_data_tabs' ] );
add_filter( 'woocommerce_product_data_panels', [ $this, 'woocommerce_product_data_panels' ] );
add_filter( 'woocommerce_process_product_meta', [ $this, 'woocommerce_process_product_meta' ] );
// Disable this gateway unless we're purchasing at least one subscription product.
add_filter( 'woocommerce_available_payment_gateways', [ $this, 'maybe_disable_gateway' ] );
}
/**
* Admin only dashboard
*/
public function admin_init() {
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
// styling
add_action( 'admin_head', [ $this, 'admin_head' ] );
if ( ! class_exists( 'WC_Subscriptions' ) ) {
// translators: %s link to WooCommerce Subscription's purchase page
$notice = sprintf( esc_html__( 'Vipps recurring payments requires WooCommerce Subscriptions to be installed and active. You can purchase and download %s here.', 'woo-vipps-recurring' ), '<a href="https://woocommerce.com/products/woocommerce-subscriptions/" target="_blank">WooCommerce Subscriptions</a>' );
$this->notices->error( $notice );
return;
}
// add capture button if order is not captured
add_action( 'woocommerce_order_item_add_action_buttons', [
$this,
'order_item_add_action_buttons'
] );
add_action( 'save_post', [ $this, 'save_order' ], 10, 3 );
if ( $this->gateway->test_mode ) {
$notice = __( 'Vipps Recurring Payments is currently in test mode - no real transactions will occur. Disable this in your wp_config when you are ready to go live!', 'woo-vipps-recurring' );
$this->notices->warning( $notice );
}
// Load correct list table classes for current screen.
add_action( 'current_screen', [ $this, 'setup_screen' ] );
if ( isset( $_REQUEST['statuses_checked'] ) ) {
$this->notices->success( __( 'Successfully checked the status of these charges', 'woo-vipps-recurring' ) );
}
// Show Vipps Login notice for a maximum of 10 days
// 1636066799 = 04-11-2021 23:59:59 UTC
// if ( ! class_exists( 'VippsWooLogin' ) && time() < 1636066799 ) {
// $vipps_login_plugin_url = 'https://wordpress.org/plugins/login-with-vipps';
// if ( get_locale() === 'nb_NO' ) {
// $vipps_login_plugin_url = 'https://nb.wordpress.org/plugins/login-with-vipps';
// }
//
// $this->notices->campaign(
// /* translators: %1$s URL to login-with-vipps, %2$s translation for "here" */
// sprintf( __( 'Login with Vipps is available for WooCommerce. Super-easy and safer login for your customers - no more usernames and passwords. Get started <a href="%1$s" target="_blank">%2$s</a>!', 'woo-vipps-recurring' ), $vipps_login_plugin_url, __( 'here', 'woo-vipps-recurring' ) ),
// 'login_promotion',
// true,
// 'assets/images/vipps-logg-inn-neg.png',
// 'login-promotion'
// );
// }
}
/**
* Upgrade routines
*/
public function upgrade() {
global $wpdb;
$version = get_option( 'woo-vipps-recurring-version' );
// Update 1.8.1: add back _vipps_recurring_pending_charge and _charge_id
if ( version_compare( $version, '1.8.1', '<' ) ) {
$results = $wpdb->get_results( "SELECT wp_posts.id FROM (
SELECT DISTINCT post_id as id FROM wp_postmeta as m
WHERE EXISTS (SELECT * FROM wp_postmeta WHERE post_id = m.post_id AND meta_key = '_vipps_recurring_failed_charge_reason')
AND NOT EXISTS (SELECT * FROM wp_postmeta WHERE post_id = m.post_id AND meta_key = '_vipps_recurring_pending_charge')
) as lookup
JOIN wp_posts ON (wp_posts.id = lookup.id)
ORDER BY wp_posts.post_date DESC", ARRAY_A );
WC_Vipps_Recurring_Logger::log( sprintf( 'Running 1.8.1 update, affecting orders with IDs: %s', implode( ',', array_map( function ( $item ) {
return $item['id'];
}, $results ) ) ) );
foreach ( $results as $row ) {
$order = wc_get_order( $row['id'] );
WC_Vipps_Recurring_Helper::set_order_charge_not_failed( $order, WC_Vipps_Recurring_Helper::get_transaction_id_for_order( $order ) );
$order->save();
}
}
// Update 1.8.2: migrate failed statuses to subscription too
if ( version_compare( $version, '1.8.2', '<' ) ) {
$results = $wpdb->get_results( "SELECT wp_posts.id
FROM (
SELECT DISTINCT post_id as id
FROM wp_postmeta as m
WHERE EXISTS(SELECT *
FROM wp_postmeta
WHERE post_id = m.post_id
AND meta_key = '_vipps_recurring_failed_charge_reason')
) as lookup
JOIN wp_posts ON (wp_posts.id = lookup.id)
ORDER BY wp_posts.post_date DESC", ARRAY_A );
WC_Vipps_Recurring_Logger::log( sprintf( 'Running 1.8.2 update, affecting orders with IDs: %s', implode( ',', array_map( function ( $item ) {
return $item['id'];
}, $results ) ) ) );
foreach ( $results as $row ) {
$order = wc_get_order( $row['id'] );
$subscriptions = WC_Vipps_Recurring_Helper::get_subscriptions_for_order( $order );
$subscription = $subscriptions[ array_key_first( $subscriptions ) ];
if ( ! $subscription ) {
continue;
}
$failure_reason = WC_Vipps_Recurring_Helper::get_meta( $order, WC_Vipps_Recurring_Helper::META_CHARGE_FAILED_REASON );
if ( $failure_reason ) {
WC_Vipps_Recurring_Helper::update_meta_data( $subscription, WC_Vipps_Recurring_Helper::META_SUBSCRIPTION_LATEST_FAILED_CHARGE_REASON, $failure_reason );
}
$failure_description = WC_Vipps_Recurring_Helper::get_meta( $order, WC_Vipps_Recurring_Helper::META_CHARGE_FAILED_DESCRIPTION );
if ( $failure_description ) {
WC_Vipps_Recurring_Helper::update_meta_data( $subscription, WC_Vipps_Recurring_Helper::META_SUBSCRIPTION_LATEST_FAILED_CHARGE_DESCRIPTION, $failure_description );
}
$subscription->save();
}
}
// if ( version_compare( $version, '1.13.0', '<' ) ) {
// $subscriptions = wcs_get_subscriptions( [
// 'payment_method' => 'kco'
// ] );
//
// WC_Vipps_Recurring_Logger::log( sprintf( 'Running 1.13.0 update, affecting subscriptions with IDs: %s', implode( ',', array_map( function ( $item ) {
// return $item->get_id();
// }, $subscriptions ) ) ) );
//
// foreach ( $subscriptions as $subscription ) {
// if ( $subscription->get_payment_method() === 'kco' && ! empty( WC_Vipps_Recurring_Helper::get_agreement_id_from_order( $subscription ) ) ) {
// $subscription->set_payment_method( 'vipps_recurring' );
// $subscription->save();
// }
// }
// }
if ( $version !== WC_VIPPS_RECURRING_VERSION ) {
update_option( 'woo-vipps-recurring-version', WC_VIPPS_RECURRING_VERSION );
}
}
/**
* Inject admin ahead
*/
public function admin_head() {
$smile_icon = plugins_url( 'assets/images/vipps-icon-smile.png', __FILE__ );
?>
<style>
#woocommerce-product-data ul.wc-tabs li.wc_vipps_recurring_options a:before {
background-image: url( <?php echo $smile_icon ?> );
}
</style>
<?php
}
public function gateway_should_be_active() {
global $wp;
$is_gateway_change = count( WC()->cart->get_cart_contents() ) === 0
&& is_checkout()
&& isset( $wp->query_vars['order-pay'] );
$has_subscription_product = $is_gateway_change;
foreach ( WC()->cart->get_cart_contents() as $values ) {
$product = wc_get_product( $values['product_id'] );
if ( $product->is_type( [ 'subscription', 'variable-subscription' ] ) ) {
$has_subscription_product = true;
}
}
return apply_filters( 'wc_vipps_recurring_cart_has_subscription_product', $has_subscription_product, WC()->cart->get_cart_contents() );
}
public function maybe_disable_gateway( $methods ) {
if ( is_admin() || ! is_checkout() ) {
return $methods;
}
$has_subscription_product = $this->gateway_should_be_active();
if ( ! $has_subscription_product ) {
unset( $methods['vipps_recurring'] );
}
return $methods;
}
/**
* @return string
*/
public function handle_check_statuses_bulk_action(): string {
$sendback = remove_query_arg( [ 'orders' ], wp_get_referer() );
if ( isset( $_GET['orders'] ) ) {
$order_ids = $_GET['orders'];
foreach ( $order_ids as $order_id ) {
// check charge status
$this->gateway->check_charge_status( $order_id );
}
$sendback = add_query_arg( 'statuses_checked', 1, $sendback );
}
return $sendback;
}
/**
* Setup the screen for our special setting and action tables
*/
public function setup_screen() {
global $wc_vipps_recurring_list_table_pending_charges,
$wc_vipps_recurring_list_table_failed_charges;
$screen_id = false;
if ( function_exists( 'get_current_screen' ) ) {
$screen = get_current_screen();
$screen_id = isset( $screen, $screen->id ) ? $screen->id : '';
}
if ( ! empty( $_REQUEST['screen'] ) ) {
$screen_id = wc_clean( wp_unslash( $_REQUEST['screen'] ) );
}
if ( $screen_id === 'settings_page_woo-vipps-recurring' ) {
include_once 'includes/admin/list-tables/wc-vipps-recurring-list-table-pending-charges.php';
include_once 'includes/admin/list-tables/wc-vipps-recurring-list-table-failed-charges.php';
$wc_vipps_recurring_list_table_pending_charges = new WC_Vipps_Recurring_Admin_List_Pending_Charges( [
'screen' => $screen_id . '_pending-charges'
] );
$wc_vipps_recurring_list_table_failed_charges = new WC_Vipps_Recurring_Admin_List_Failed_Charges( [
'screen' => $screen_id . '_failed-charges'
] );
}
if ( $wc_vipps_recurring_list_table_pending_charges
&& $wc_vipps_recurring_list_table_pending_charges->current_action()
&& $wc_vipps_recurring_list_table_pending_charges->current_action() === 'check_status' ) {
$sendback = $this->handle_check_statuses_bulk_action();
wp_redirect( $sendback );
}
if ( $wc_vipps_recurring_list_table_failed_charges
&& $wc_vipps_recurring_list_table_failed_charges->current_action()
&& $wc_vipps_recurring_list_table_failed_charges->current_action() === 'check_status' ) {
$sendback = $this->handle_check_statuses_bulk_action();
wp_redirect( $sendback );
}
// Ensure the table handler is only loaded once. Prevents multiple loads if a plugin calls check_ajax_referer many times.
remove_action( 'current_screen', [ $this, 'setup_screen' ] );
}
/**
* Make admin menu entry
*/
public function admin_menu() {
add_options_page(
__( 'Vipps Recurring Payments', 'woo-vipps-recurring' ),
__( 'Vipps Recurring Payments', 'woo-vipps-recurring' ),
'manage_options',
'woo-vipps-recurring',
[ $this, 'admin_menu_page_html' ]
);
}
/**
* Admin menu page HTML
*/
public function admin_menu_page_html() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
include __DIR__ . '/includes/pages/admin/vipps-recurring-admin-menu-page.php';
}
/**
* Force check status of all pending charges
*/
public function wp_ajax_vipps_recurring_force_check_charge_statuses(): void {
try {
/* translators: amount of orders checked */
echo sprintf( __( 'Done. Checked the status of %s orders', 'woo-vipps-recurring' ), count( $this->check_order_statuses( - 1 ) ) );
} catch ( Exception $e ) {
echo __( 'Failed to finish checking the status of all orders. Please try again.', 'woo-vipps-recurring' );
}
wp_die();
}
/**
* @param $tabs
*
* @return mixed
*/
public function woocommerce_product_data_tabs( $tabs ) {
$tabs['wc_vipps_recurring'] = [
'label' => __( 'Vipps Recurring Payments', 'woo-vipps-recurring' ),
'target' => 'wc_vipps_recurring_product_data',
'priority' => 100,
];
return $tabs;
}
/**
* Tab content
*/
public function woocommerce_product_data_panels(): void {
echo '<div id="wc_vipps_recurring_product_data" class="panel woocommerce_options_panel hidden">';
woocommerce_wp_checkbox( [
'id' => WC_Vipps_Recurring_Helper::META_PRODUCT_DIRECT_CAPTURE,
'value' => get_post_meta( get_the_ID(), WC_Vipps_Recurring_Helper::META_PRODUCT_DIRECT_CAPTURE, true ),
'label' => __( 'Capture payment instantly', 'woo-vipps-recurring' ),
'description' => __( 'Capture payment instantly even if the product is not virtual. Please make sure you are following Norwegian law when using this option.', 'woo-vipps-recurring' ),
'desc_tip' => true,
] );
woocommerce_wp_select( [
'id' => WC_Vipps_Recurring_Helper::META_PRODUCT_DESCRIPTION_SOURCE,
'value' => get_post_meta( get_the_ID(), WC_Vipps_Recurring_Helper::META_PRODUCT_DESCRIPTION_SOURCE, true ) ?: 'title',
'label' => __( 'Description source', 'woo-vipps-recurring' ),
'description' => __( 'Where we should source the agreement description from. Displayed in the Vipps app.', 'woo-vipps-recurring' ),
'desc_tip' => true,
'options' => [
'none' => __( 'None', 'woo-vipps-recurring' ),
'short_description' => __( 'Product short description', 'woo-vipps-recurring' ),
'custom' => __( 'Custom', 'woo-vipps-recurring' )
]
] );
woocommerce_wp_text_input( [
'id' => WC_Vipps_Recurring_Helper::META_PRODUCT_DESCRIPTION_TEXT,
'value' => get_post_meta( get_the_ID(), WC_Vipps_Recurring_Helper::META_PRODUCT_DESCRIPTION_TEXT, true ),
'label' => __( 'Custom description', 'woo-vipps-recurring' ),
'description' => __( 'If the description source is set to "custom" this text will be used.', 'woo-vipps-recurring' ),
'placeholder' => __( 'Max 100 characters', 'woo-vipps-recurring' ),
'desc_tip' => true,
] );
echo '</div>';
}
/**
* Save our custom fields
*
* @param $post_id
*/
public function woocommerce_process_product_meta( $post_id ) {
$capture_instantly = isset( $_POST[ WC_Vipps_Recurring_Helper::META_PRODUCT_DIRECT_CAPTURE ] ) ? 'yes' : 'no';
update_post_meta( $post_id, WC_Vipps_Recurring_Helper::META_PRODUCT_DIRECT_CAPTURE, $capture_instantly );
update_post_meta( $post_id, WC_Vipps_Recurring_Helper::META_PRODUCT_DESCRIPTION_SOURCE, $_POST[ WC_Vipps_Recurring_Helper::META_PRODUCT_DESCRIPTION_SOURCE ] );
update_post_meta( $post_id, WC_Vipps_Recurring_Helper::META_PRODUCT_DESCRIPTION_TEXT, $_POST[ WC_Vipps_Recurring_Helper::META_PRODUCT_DESCRIPTION_TEXT ] ?? '' );
}
/**
* @param $order
*/
public function order_item_add_action_buttons( $order ): void {
$this->order_item_add_capture_button( $order );
}
/**
* @param $order
*/
public function order_item_add_capture_button( $order ): void {
if ( $order->get_type() !== 'shop_order' ) {
return;
}
$payment_method = WC_Vipps_Recurring_Helper::get_payment_method( $order );
if ( $payment_method !== $this->gateway->id ) {
// If this is not the payment method, an agreement would not be available.
return;
}
$order_status = $order->get_status();
$show_capture_button = ( ! in_array( $order_status, $this->gateway->statuses_to_attempt_capture, true ) )
&& ! (int) WC_Vipps_Recurring_Helper::is_charge_captured_for_order( $order )
&& ! (int) WC_Vipps_Recurring_Helper::is_charge_failed_for_order( $order )
&& ! wcs_order_contains_renewal( $order )
&& ! (int) WC_Vipps_Recurring_Helper::get_meta( $order, WC_Vipps_Recurring_Helper::META_ORDER_ZERO_AMOUNT );
if ( ! apply_filters( 'wc_vipps_recurring_show_capture_button', $show_capture_button, $order ) ) {
return;
}
$is_captured = WC_Vipps_Recurring_Helper::is_charge_captured_for_order( $order );
if ( $show_capture_button && ! $is_captured ) {
$logo = plugins_url( 'assets/images/vipps-logo-negative-rgb-transparent.png', __FILE__ );
print '<button type="button" onclick="document.getElementById(\'docapture\').value=1;document.post.submit();" style="background-color:#ff5b24;border-color:#ff5b24;color:#ffffff" class="button vipps-button generate-items"><img border="0" style="display:inline;height:2ex;vertical-align:text-bottom" class="inline" alt="0" src="' . $logo . '"/> ' . __( 'Capture payment', 'woo-vipps-recurring' ) . '</button>';
print '<input id="docapture" type="hidden" name="do_capture_vipps_recurring" value="0">';
}
}
/**
* @param $postid
* @param $post
*
* @throws Exception
*/
public function save_order( $postid, $post ): void {
if ( $post->post_type !== 'shop_order' ) {
return;
}
$order = wc_get_order( $postid );
$payment_method = WC_Vipps_Recurring_Helper::get_payment_method( $order );
if ( $payment_method !== $this->gateway->id ) {
// If this is not the payment method, an agreement would not be available.
return;
}
if ( isset( $_POST['do_capture_vipps_recurring'] ) && $_POST['do_capture_vipps_recurring'] ) {
$this->gateway->capture_payment( $order );
}
}
/**
* Check charge statuses scheduled action
*
* @param int|null $limit
*
* @return array
*/
public function check_order_statuses( $limit = '' ): array {
if ( empty( $limit ) ) {
$limit = $this->gateway->check_charges_amount;
}
$options = [
'limit' => $limit,
'type' => 'shop_order',
'meta_key' => WC_Vipps_Recurring_Helper::META_CHARGE_PENDING,
'meta_compare' => '=',
'meta_value' => 1,
'return' => 'ids',
'payment_method' => $this->gateway->id
];
if ( $this->gateway->check_charges_sort_order === 'rand' ) {
$options['orderby'] = 'rand';
} else {
$options['orderby'] = 'post_date';
$options['order'] = $this->gateway->check_charges_sort_order;
}
remove_all_filters( 'posts_orderby' );
$order_ids = wc_get_orders( $options );
foreach ( $order_ids as $order_id ) {
// check charge status
$this->gateway->check_charge_status( $order_id );
}
return $order_ids;
}
/**
* Check the status of gateway change requests
*/
public function check_gateway_change_agreement_statuses() {
$posts = get_posts( [
'post_type' => 'shop_subscription',
'post_status' => [ 'wc-active', 'wc-pending', 'wc-on-hold' ],
'meta_key' => WC_Vipps_Recurring_Helper::META_SUBSCRIPTION_WAITING_FOR_GATEWAY_CHANGE,
'meta_compare' => '=',
'meta_value' => 1,
'return' => 'ids',
] );
foreach ( $posts as $post ) {
// check charge status
$this->gateway->maybe_process_gateway_change( $post->ID );
}
}
/**
* Update a subscription's details in the app
*/
public function update_subscription_details_in_app() {
$posts = get_posts( [
'limit' => 5,
'post_type' => 'shop_subscription',
'post_status' => [ 'wc-active', 'wc-pending-cancel', 'wc-cancelled', 'wc-on-hold' ],
'meta_key' => WC_Vipps_Recurring_Helper::META_SUBSCRIPTION_UPDATE_IN_APP,
'meta_compare' => '=',
'meta_value' => 1,
'return' => 'ids',
] );
foreach ( $posts as $post ) {
// check charge status
$this->gateway->maybe_update_subscription_details_in_app( $post->ID );
}
}
/**
* Adds plugin action links.
*
* @since 1.0.0
* @version 4.0.0
*/
public function plugin_action_links( $links ): array {
$plugin_links = [
'<a href="admin.php?page=wc-settings&tab=checkout§ion=vipps_recurring">' . esc_html__( 'Settings', 'woo-vipps-recurring' ) . '</a>',
];
return array_merge( $plugin_links, $links );
}
/**
* Handles upgrade routines.
*
* @since 3.1.0
* @version 3.1.0
*/
public function install() {
$this->gateway->ensure_cancelled_order_page();
$this->upgrade();
}
/**
* Add the gateways to WooCommerce.
*
* @param $methods
*
* @return array
* @since 1.0.0
* @version 4.0.0
*/
public function add_gateways( $methods ): array {
if ( function_exists( 'wcs_create_renewal_order' ) && class_exists( 'WC_Subscriptions_Order' ) ) {
$methods[] = WC_Gateway_Vipps_Recurring::get_instance();
}
return $methods;
}
/**
* Enqueue our CSS and other assets.
*/
public function wp_enqueue_scripts() {
wp_enqueue_style( 'woo-vipps-recurring', plugins_url( 'assets/css/vipps-recurring.css', __FILE__ ), [],
filemtime( __DIR__ . '/assets/css/vipps-recurring.css' ) );
}
/**
* Enqueue our CSS and other assets.
*/
public function admin_enqueue_scripts() {
wp_enqueue_style( 'woo-vipps-recurring', plugins_url( 'assets/css/vipps-recurring-admin.css', __FILE__ ), [],
filemtime( __DIR__ . '/assets/css/vipps-recurring-admin.css' ) );
wp_enqueue_script( 'woo-vipps-recurring', plugins_url( 'assets/js/vipps-recurring-admin.js', __FILE__ ), [],
filemtime( __DIR__ . '/assets/js/vipps-recurring-admin.js' ) );
}
/**
* @param $schedules
*
* @return mixed
*/
public function woocommerce_vipps_recurring_add_cron_schedules( $schedules ) {
$schedules['one_minute'] = [
'interval' => 60,
'display' => esc_html__( 'Every One Minute' ),
];
return $schedules;
}
}
global $vipps_recurring;
$vipps_recurring = WC_Vipps_Recurring::get_instance();
require_once __DIR__ . '/includes/wc-vipps-recurring-compatibility.php';
}
}