class-wc-payments-product-service.php
26.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
<?php
/**
* Class WC_Payments_Product_Service
*
* @package WooCommerce\Payments
*/
use WCPay\Exceptions\API_Exception;
use WCPay\Logger;
defined( 'ABSPATH' ) || exit;
/**
* Class handling any subscription product functionality
*/
class WC_Payments_Product_Service {
use WC_Payments_Subscriptions_Utilities;
/**
* The product meta key used to store the product data we last sent to WC Pay as a hash. Used to compare current WC product data with WC Pay data.
*
* @const string
*/
const PRODUCT_HASH_KEY = '_wcpay_product_hash';
/**
* The live product meta key used to store the product's ID in WC Pay.
*
* @const string
*/
const LIVE_PRODUCT_ID_KEY = '_wcpay_product_id_live';
/**
* The testmode product meta key used to store the product's ID in WC Pay.
*
* @const string
*/
const TEST_PRODUCT_ID_KEY = '_wcpay_product_id_test';
/**
* The product price meta key used to store the price data we last sent to WC Pay as a hash. Used to compare current WC product price data with WC Pay data.
*
* @const string
*/
const PRICE_HASH_KEY = '_wcpay_product_price_hash';
/**
* The product meta key used to store the live product's WC Pay Price object ID.
*
* @const string
*/
const LIVE_PRICE_ID_KEY = '_wcpay_product_price_id_live';
/**
* The product meta key used to store the testmode product's WC Pay Price object ID.
*
* @const string
*/
const TEST_PRICE_ID_KEY = '_wcpay_product_price_id_test';
/**
* Client for making requests to the WooCommerce Payments API
*
* @var WC_Payments_API_Client
*/
private $payments_api_client;
/**
* The list of products we need to update at the end of each request.
*
* @var array
*/
private $products_to_update = [];
/**
* Constructor.
*
* @param WC_Payments_API_Client $payments_api_client Payments API client.
*/
public function __construct( WC_Payments_API_Client $payments_api_client ) {
$this->payments_api_client = $payments_api_client;
add_action( 'shutdown', [ $this, 'create_or_update_products' ] );
add_action( 'wp_trash_post', [ $this, 'maybe_archive_product' ] );
add_action( 'untrashed_post', [ $this, 'maybe_unarchive_product' ] );
add_filter( 'woocommerce_duplicate_product_exclude_meta', [ $this, 'exclude_meta_wcpay_product' ] );
$this->add_product_update_listeners();
}
/**
* Gets the WC Pay product hash associated with a WC product.
*
* @param WC_Product $product The product to get the hash for.
* @return string The product's hash or an empty string.
*/
public static function get_wcpay_product_hash( WC_Product $product ) : string {
return $product->get_meta( self::PRODUCT_HASH_KEY, true );
}
/**
* Gets the WC Pay product ID associated with a WC product.
*
* @param WC_Product $product The product to get the WC Pay ID for.
* @param bool|null $test_mode Is WC Pay in test/dev mode.
*
* @return string The WC Pay product ID or an empty string.
*/
public function get_wcpay_product_id( WC_Product $product, $test_mode = null ) : string {
// If the subscription product doesn't have a WC Pay product ID, create one.
if ( ! self::has_wcpay_product_id( $product, $test_mode ) ) {
$is_current_environment = null === $test_mode || WC_Payments::get_gateway()->is_in_test_mode() === $test_mode;
// Only create a new wcpay product if we're trying to fetch a wcpay product ID in the current environment.
if ( $is_current_environment ) {
WC_Payments_Subscriptions::get_product_service()->create_product( $product );
}
}
return $product->get_meta( self::get_wcpay_product_id_option( $test_mode ), true );
}
/**
* Gets the WC Pay product ID associated with a WC product.
*
* @param string $type The item type to create a product for.
* @return string The item's WCPay product id.
*/
public function get_wcpay_product_id_for_item( string $type ) : string {
$sanitized_type = self::sanitize_option_key( $type );
$option_key_name = self::get_wcpay_product_id_option() . '_' . $sanitized_type;
if ( ! get_option( $option_key_name ) ) {
$this->create_product_for_item_type( $sanitized_type );
}
return get_option( $option_key_name );
}
/**
* Sanitize option key string to replace space with underscore, and remove special characters.
*
* @param string $type Non sanitized input.
* @return string Sanitized output.
*/
public static function sanitize_option_key( string $type ) {
return sanitize_key( str_replace( ' ', '_', trim( $type ) ) );
}
/**
* Check if the WC product has a WC Pay product ID.
*
* @param WC_Product $product The product to get the WC Pay ID for.
* @param bool|null $test_mode Is WC Pay in test/dev mode.
*
* @return bool The WC Pay product ID or an empty string.
*/
public static function has_wcpay_product_id( WC_Product $product, $test_mode = null ) : bool {
return (bool) $product->get_meta( self::get_wcpay_product_id_option( $test_mode ) );
}
/**
* Prevents duplicate WC Pay product IDs and hashes when duplicating a subscription product.
*
* @param array $meta_keys The keys to exclude from the duplicate.
* @return array Keys to exclude.
*/
public static function exclude_meta_wcpay_product( $meta_keys ) {
return array_merge(
$meta_keys,
[
self::PRODUCT_HASH_KEY,
self::LIVE_PRODUCT_ID_KEY,
self::TEST_PRODUCT_ID_KEY,
self::PRICE_HASH_KEY,
self::LIVE_PRICE_ID_KEY,
self::TEST_PRICE_ID_KEY,
]
);
}
/**
* Schedules a subscription product to be created or updated in WC Pay on shutdown.
*
* @since 3.2.0
*
* @param int $product_id The ID of the product to handle.
*/
public function maybe_schedule_product_create_or_update( int $product_id ) {
// Skip products which have already been scheduled or aren't subscriptions.
$product = wc_get_product( $product_id );
if ( ! $product || isset( $this->products_to_update[ $product_id ] ) || ! WC_Subscriptions_Product::is_subscription( $product ) ) {
return;
}
foreach ( $this->get_products_to_update( $product ) as $product_to_update ) {
// Skip products already scheduled.
if ( isset( $this->products_to_update[ $product_to_update->get_id() ] ) ) {
continue;
}
// Skip product variations that don't have a price set.
if ( $product_to_update->is_type( 'subscription_variation' ) && '' === $product_to_update->get_price() ) {
continue;
}
if ( ! self::has_wcpay_product_id( $product_to_update ) || $this->product_needs_update( $product_to_update ) ) {
$this->products_to_update[ $product_to_update->get_id() ] = $product_to_update->get_id();
}
}
}
/**
* Creates and updates all products which have been scheduled for an update.
*
* Hooked onto shutdown so all products which have been changed in the current request can be updated once.
*
* @since 3.2.0
*/
public function create_or_update_products() {
foreach ( $this->products_to_update as $product_id ) {
$product = wc_get_product( $product_id );
if ( ! $product ) {
continue;
}
$this->update_products( $product );
}
}
/**
* Creates a product in WC Pay.
*
* @param WC_Product $product The product to create.
*/
public function create_product( WC_Product $product ) {
try {
$product_data = $this->get_product_data( $product );
// Validate that we have enough data to create the product.
$this->validate_product_data( $product_data );
$wcpay_product = $this->payments_api_client->create_product( $product_data );
$this->remove_product_update_listeners();
$this->set_wcpay_product_hash( $product, $this->get_product_hash( $product ) );
$this->set_wcpay_product_id( $product, $wcpay_product['wcpay_product_id'] );
$this->add_product_update_listeners();
} catch ( \Exception $e ) {
Logger::log( sprintf( 'There was a problem creating the product #%s in WC Pay: %s', $product->get_id(), $e->getMessage() ) );
}
}
/**
* Create a generic item product in WC Pay.
*
* @param string $type The item type to create a product for.
*/
public function create_product_for_item_type( string $type ) {
try {
$wcpay_product = $this->payments_api_client->create_product(
[
'description' => 'N/A',
'name' => ucfirst( $type ),
]
);
update_option( self::get_wcpay_product_id_option() . '_' . $type, $wcpay_product['wcpay_product_id'] );
} catch ( API_Exception $e ) {
Logger::log( 'There was a problem creating the product on WCPay Server: ' . $e->getMessage() );
}
}
/**
* Updates related products in WC Pay when a WC Product is updated.
*
* @param WC_Product $product The product to update.
*/
public function update_products( WC_Product $product ) {
if ( ! WC_Subscriptions_Product::is_subscription( $product ) ) {
return;
}
$wcpay_product_ids = $this->get_all_wcpay_product_ids( $product );
$test_mode = WC_Payments::get_gateway()->is_in_test_mode();
// If the current environment doesn't have a product ID, make sure we create one.
if ( ! isset( $wcpay_product_ids[ $test_mode ? 'test' : 'live' ] ) ) {
$this->create_product( $product );
}
// Return when there's no products to update.
if ( empty( $wcpay_product_ids ) ) {
return;
}
if ( ! $this->product_needs_update( $product ) ) {
return;
}
$data = $this->get_product_data( $product );
$this->remove_product_update_listeners();
try {
// Validate that we have enough data to create the product.
$this->validate_product_data( $data );
// Update all versions of WCPay Products that need updating.
foreach ( $wcpay_product_ids as $environment => $wcpay_product_id ) {
$data['test_mode'] = 'live' !== $environment;
$this->payments_api_client->update_product( $wcpay_product_id, $data );
}
$this->set_wcpay_product_hash( $product, $this->get_product_hash( $product ) );
} catch ( \Exception $e ) {
Logger::log( sprintf( 'There was a problem updating the product #%s in WC Pay: %s', $product->get_id(), $e->getMessage() ) );
}
$this->add_product_update_listeners();
}
/**
* Archives a subscription product in WC Pay.
*
* @since 3.2.0
*
* @param int $post_id The ID of the post to handle. Only subscription product IDs will be archived in WC Pay.
*/
public function maybe_archive_product( int $post_id ) {
$product = wc_get_product( $post_id );
if ( $product && WC_Subscriptions_Product::is_subscription( $product ) ) {
foreach ( $this->get_products_to_update( $product ) as $product ) {
$this->archive_product( $product );
}
}
}
/**
* Unarchives a subscription product in WC Pay.
*
* @since 3.2.0
*
* @param int $post_id The ID of the post to handle. Only Subscription product post IDs will be unarchived in WC Pay.
*/
public function maybe_unarchive_product( int $post_id ) {
$product = wc_get_product( $post_id );
if ( $product && WC_Subscriptions_Product::is_subscription( $product ) ) {
foreach ( $this->get_products_to_update( $product ) as $product ) {
$this->unarchive_product( $product );
}
}
}
/**
* Archives all related WCPay products (live and test) when a product is trashed/deleted in WC.
*
* @param WC_Product $product The product to archive.
*/
public function archive_product( WC_Product $product ) {
$wcpay_product_ids = $this->get_all_wcpay_product_ids( $product );
if ( empty( $wcpay_product_ids ) ) {
return;
}
foreach ( $wcpay_product_ids as $environment => $wcpay_product_id ) {
try {
$this->delete_all_wcpay_price_ids( $product );
$this->payments_api_client->update_product(
$wcpay_product_id,
[
'active' => 'false',
'test_mode' => 'live' !== $environment,
]
);
} catch ( API_Exception $e ) {
Logger::log( 'There was a problem archiving the ' . $environment . ' product in WC Pay: ' . $e->getMessage() );
}
}
}
/**
* Unarchives all related WCPay products (live and test) when a product in WC is untrashed.
*
* @param WC_Product $product The product unarchive.
*/
public function unarchive_product( WC_Product $product ) {
$wcpay_product_ids = $this->get_all_wcpay_product_ids( $product );
if ( empty( $wcpay_product_ids ) ) {
return;
}
foreach ( $wcpay_product_ids as $environment => $wcpay_product_id ) {
try {
$this->payments_api_client->update_product(
$wcpay_product_id,
[
'active' => 'true',
'test_mode' => 'live' !== $environment,
]
);
} catch ( API_Exception $e ) {
Logger::log( 'There was a problem unarchiving the ' . $environment . 'product in WC Pay: ' . $e->getMessage() );
}
}
}
/**
* Archives a WC Pay price object.
*
* @param string $wcpay_price_id The price object's ID to archive.
* @param bool|null $test_mode Is WC Pay in test/dev mode.
*/
public function archive_price( string $wcpay_price_id, $test_mode = null ) {
$data = [ 'active' => 'false' ];
if ( null !== $test_mode ) {
$data['test_mode'] = $test_mode;
}
$this->payments_api_client->update_price( $wcpay_price_id, $data );
}
/**
* Prevents the subscription interval to be greater than 1 for yearly subscriptions.
*
* @param int $product_id Post ID of the product.
*/
public function limit_subscription_product_intervals( $product_id ) {
if ( $this->is_subscriptions_plugin_active() ) {
return;
}
// Skip products that aren't subscriptions.
$product = wc_get_product( $product_id );
if (
! $product ||
! WC_Subscriptions_Product::is_subscription( $product ) ||
empty( $_POST['_wcsnonce'] ) ||
! wp_verify_nonce( sanitize_key( $_POST['_wcsnonce'] ), 'wcs_subscription_meta' )
) {
return;
}
// If we don't have both the period and the interval, there's nothing to do here.
if ( empty( $_REQUEST['_subscription_period'] ) || empty( $_REQUEST['_subscription_period_interval'] ) ) {
return;
}
$period = sanitize_text_field( wp_unslash( $_REQUEST['_subscription_period'] ) );
$interval = absint( wp_unslash( $_REQUEST['_subscription_period_interval'] ) );
// Prevent WC Subs Core from saving the interval when it's invalid.
if ( ! $this->is_valid_billing_cycle( $period, $interval ) ) {
$new_interval = $this->get_period_interval_limit( $period );
$_REQUEST['_subscription_period_interval'] = (string) $new_interval;
/* translators: %1$s Opening strong tag, %2$s Closing strong tag, %3$s The subscription renewal interval (every x time) */
wcs_add_admin_notice( sprintf( __( '%1$sThere was an issue saving your product!%2$s A subscription product\'s billing period cannot be longer than one year. We have updated this product to renew every %3$s.', 'woocommerce-payments' ), '<strong>', '</strong>', wcs_get_subscription_period_strings( $new_interval, $period ) ), 'error' );
}
}
/**
* Prevents the subscription interval to be greater than 1 for yearly subscription variations.
*
* @param int $product_id Post ID of the variation.
* @param int $index Variation index in the incoming array.
*/
public function limit_subscription_variation_intervals( $product_id, $index ) {
if ( $this->is_subscriptions_plugin_active() ) {
return;
}
// Skip products that aren't subscriptions.
$product = wc_get_product( $product_id );
$admin_notice_sent = false;
if (
! $product ||
! WC_Subscriptions_Product::is_subscription( $product ) ||
empty( $_POST['_wcsnonce_save_variations'] ) ||
! wp_verify_nonce( sanitize_key( $_POST['_wcsnonce_save_variations'] ), 'wcs_subscription_variations' )
) {
return;
}
// If we don't have both the period and the interval, there's nothing to do here.
if ( empty( $_POST['variable_subscription_period'][ $index ] ) || empty( $_POST['variable_subscription_period_interval'][ $index ] ) ) {
return;
}
$period = sanitize_text_field( wp_unslash( $_POST['variable_subscription_period'][ $index ] ) );
$interval = absint( wp_unslash( $_POST['variable_subscription_period_interval'][ $index ] ) );
// Prevent WC Subs Core from saving the interval when it's invalid.
if ( ! $this->is_valid_billing_cycle( $period, $interval ) ) {
$new_interval = $this->get_period_interval_limit( $period );
$_POST['variable_subscription_period_interval'][ $index ] = (string) $new_interval;
if ( false === $admin_notice_sent ) {
$admin_notice_sent = true;
/* translators: %1$s Opening strong tag, %2$s Closing strong tag */
wcs_add_admin_notice( sprintf( __( '%1$sThere was an issue saving your variations!%2$s A subscription product\'s billing period cannot be longer than one year. We have updated one or more of this product\'s variations to renew every %3$s.', 'woocommerce-payments' ), '<strong>', '</strong>', wcs_get_subscription_period_strings( $new_interval, $period ) ), 'error' );
}
}
}
/**
* Attaches the callbacks used to update product changes in WC Pay.
*/
private function add_product_update_listeners() {
// This needs to run before WC_Subscriptions_Admin::save_subscription_meta(), which has a priority of 11.
add_action( 'save_post', [ $this, 'limit_subscription_product_intervals' ], 10 );
// This needs to run before WC_Subscriptions_Admin::save_product_variation(), which has a priority of 20.
add_action( 'woocommerce_save_product_variation', [ $this, 'limit_subscription_variation_intervals' ], 19, 2 );
add_action( 'save_post', [ $this, 'maybe_schedule_product_create_or_update' ], 12 );
add_action( 'woocommerce_save_product_variation', [ $this, 'maybe_schedule_product_create_or_update' ], 30 );
}
/**
* Removes the callbacks used to update product changes in WC Pay.
*/
private function remove_product_update_listeners() {
remove_action( 'save_post', [ $this, 'limit_subscription_product_intervals' ], 10 );
remove_action( 'woocommerce_save_product_variation', [ $this, 'limit_subscription_variation_intervals' ], 19 );
remove_action( 'save_post', [ $this, 'maybe_schedule_product_create_or_update' ], 12 );
remove_action( 'woocommerce_save_product_variation', [ $this, 'maybe_schedule_product_create_or_update' ], 30 );
}
/**
* Gets product data relevant to WC Pay from a WC product.
*
* @param WC_Product $product The product to get data from.
* @return array
*/
private function get_product_data( WC_Product $product ) : array {
return [
'description' => $product->get_description() ? $product->get_description() : 'N/A',
'name' => $product->get_name(),
];
}
/**
* Gets the products to update from a given product.
*
* If applicable, returns the product's variations otherwise returns the product by itself.
*
* @param WC_Product|WC_Product_Variable $product The product.
*
* @return array The products to update.
*/
private function get_products_to_update( WC_Product $product ) : array {
return $product->is_type( 'variable-subscription' ) ? $product->get_available_variations( 'object' ) : [ $product ];
}
/**
* Gets a hash of the product's name and description.
* Used to compare WC changes with WC Pay data.
*
* @param WC_Product $product The product to generate the hash for.
* @return string The product's hash.
*/
private function get_product_hash( WC_Product $product ) : string {
return md5( implode( $this->get_product_data( $product ) ) );
}
/**
* Checks if a product needs to be updated in WC Pay.
*
* @param WC_Product $product The product to check updates for.
*
* @return bool Whether the product needs to be update in WC Pay.
*/
private function product_needs_update( WC_Product $product ) : bool {
return $this->get_product_hash( $product ) !== $this->get_wcpay_product_hash( $product );
}
/**
* Sets a WC Pay product hash on a WC product.
*
* @param WC_Product $product The product to set the WC Pay product hash for.
* @param string $value The WC Pay product hash.
*/
private function set_wcpay_product_hash( WC_Product $product, string $value ) {
$product->update_meta_data( self::PRODUCT_HASH_KEY, $value );
$product->save();
}
/**
* Sets a WC Pay product ID on a WC product.
*
* @param WC_Product $product The product to set the WC Pay ID for.
* @param string $value The WC Pay product ID.
*/
private function set_wcpay_product_id( WC_Product $product, string $value ) {
$product->update_meta_data( self::get_wcpay_product_id_option(), $value );
$product->save();
}
/**
* Returns the name of the product id option meta, taking test mode into account.
*
* @param bool|null $test_mode Is WC Pay in test/dev mode.
*
* @return string The WCPay product ID meta key/option name.
*/
public static function get_wcpay_product_id_option( $test_mode = null ) : string {
$test_mode = null === $test_mode ? WC_Payments::get_gateway()->is_in_test_mode() : $test_mode;
return $test_mode ? self::TEST_PRODUCT_ID_KEY : self::LIVE_PRODUCT_ID_KEY;
}
/**
* Returns the name of the price id option meta, taking test mode into account.
*
* @param bool|null $test_mode Is WC Pay in test/dev mode.
*
* @return string The price hash option name.
*/
public static function get_wcpay_price_id_option( $test_mode = null ) : string {
$test_mode = null === $test_mode ? WC_Payments::get_gateway()->is_in_test_mode() : $test_mode;
return $test_mode ? self::TEST_PRICE_ID_KEY : self::LIVE_PRICE_ID_KEY;
}
/**
* Gets all WCPay Product IDs linked to a WC Product (live and testmode products).
*
* @param WC_Product $product The product to fetch WCPay product IDs for.
*
* @return array Live and test WCPay Product IDs if they exist.
*/
private function get_all_wcpay_product_ids( WC_Product $product ) {
$environment_product_ids = [
'live' => self::has_wcpay_product_id( $product, false ) ? $this->get_wcpay_product_id( $product, false ) : null,
'test' => self::has_wcpay_product_id( $product, true ) ? $this->get_wcpay_product_id( $product, true ) : null,
];
return array_filter( $environment_product_ids );
}
/**
* Returns whether the billing cycle is valid, given its period and interval.
*
* @param string $period Cycle period.
* @param int $interval Cycle interval.
* @return boolean
*/
public function is_valid_billing_cycle( $period, $interval ) {
$interval_limit = $this->get_period_interval_limit( $period );
// A cycle is valid when we have a defined limit, and the given interval isn't 0 nor greater than the limit.
return $interval_limit && ! empty( $interval ) && $interval <= $interval_limit;
}
/**
* Returns the interval limit for the given period.
*
* @param string $period The period to get the interval limit for.
* @return int|bool The interval limit for the period, or false if not defined.
*/
private function get_period_interval_limit( $period ) {
$max_intervals = [
'year' => 1,
'month' => 12,
'week' => 52,
'day' => 365,
];
return ! empty( $max_intervals[ $period ] ) ? $max_intervals[ $period ] : false;
}
/**
* Deletes and archives a product WCPay Price IDs.
*
* @param WC_Product $product The WC Product object to delete and archive the a price IDs.
*/
private function delete_all_wcpay_price_ids( $product ) {
// Delete and archive all price IDs for all environments.
foreach ( [ 'test', 'live' ] as $environment ) {
$test_mode = 'test' === $environment;
$price_id_meta_key = self::get_wcpay_price_id_option( $test_mode );
if ( $product->meta_exists( $price_id_meta_key ) ) {
try {
$this->archive_price( $product->get_meta( $price_id_meta_key, true ), $test_mode );
} catch ( API_Exception $e ) {
Logger::log( 'There was a problem archiving the ' . $environment . 'product price ID in WC Pay: ' . $e->getMessage() );
}
// Now that the price has been archived, delete the record of it.
$product->delete_meta_data( $price_id_meta_key );
}
}
$product->delete_meta_data( self::PRICE_HASH_KEY );
$product->save();
}
/**
* Validates that we have the data necessary to create a product in WCPay.
*
* @param array $product_data Data used to create/update the product in WCPay.
* @throws Exception If the product data doesn't contain the 'name' argument as the 'name' property is a required field.
*/
private function validate_product_data( $product_data ) {
if ( empty( $product_data['name'] ) ) {
throw new Exception( 'The product "name" is required.' );
}
}
/**
* Deprecated functions
*/
/**
* Unarchives a WC Pay Price object.
*
* @deprecated 3.3.0
*
* @param string $wcpay_price_id The Price object's ID to unarchive.
* @param bool|null $test_mode Is WC Pay in test/dev mode.
*/
public function unarchive_price( string $wcpay_price_id, $test_mode = null ) {
wc_deprecated_function( __FUNCTION__, '3.3.0' );
$data = [ 'active' => 'true' ];
if ( null !== $test_mode ) {
$data['test_mode'] = $test_mode;
}
$this->payments_api_client->update_price( $wcpay_price_id, $data );
}
/**
* Gets the WC Pay price hash associated with a WC product.
*
* @deprecated 3.3.0
*
* @param WC_Product $product The product to get the hash for.
* @return string The product's price hash or an empty string.
*/
public static function get_wcpay_price_hash( WC_Product $product ) : string {
wc_deprecated_function( __FUNCTION__, '3.3.0' );
return $product->get_meta( self::PRICE_HASH_KEY, true );
}
/**
* Gets the WC Pay price ID associated with a WC product.
*
* @deprecated 3.3.0
*
* @param WC_Product $product The product to get the WC Pay price ID for.
* @param bool|null $test_mode Is WC Pay in test/dev mode.
*
* @return string The product's WC Pay price ID or an empty string.
*/
public function get_wcpay_price_id( WC_Product $product, $test_mode = null ) : string {
wc_deprecated_function( __FUNCTION__, '3.3.0' );
$price_id = $product->get_meta( self::get_wcpay_price_id_option( $test_mode ), true );
// If the subscription product doesn't have a WC Pay price ID, create one now.
if ( empty( $price_id ) && WC_Subscriptions_Product::is_subscription( $product ) ) {
$is_current_environment = null === $test_mode || WC_Payments::get_gateway()->is_in_test_mode() === $test_mode;
// Only create WCPay Price object if we're trying to getch a wcpay price ID in the current environment.
if ( $is_current_environment ) {
WC_Payments_Subscriptions::get_product_service()->create_product( $product );
$price_id = $product->get_meta( self::get_wcpay_price_id_option(), true );
}
}
return $price_id;
}
}