| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Freemius |
| 4 |
* @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 |
* @since 1.0.9 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
class FS_Subscription extends FS_Entity { |
| 14 |
|
| 15 |
#region Properties |
| 16 |
|
| 17 |
/** |
| 18 |
* @var number |
| 19 |
*/ |
| 20 |
public $user_id; |
| 21 |
/** |
| 22 |
* @var number |
| 23 |
*/ |
| 24 |
public $install_id; |
| 25 |
/** |
| 26 |
* @var number |
| 27 |
*/ |
| 28 |
public $plan_id; |
| 29 |
/** |
| 30 |
* @var number |
| 31 |
*/ |
| 32 |
public $license_id; |
| 33 |
/** |
| 34 |
* @var float |
| 35 |
*/ |
| 36 |
public $total_gross; |
| 37 |
/** |
| 38 |
* @var float |
| 39 |
*/ |
| 40 |
public $amount_per_cycle; |
| 41 |
/** |
| 42 |
* @var int # of months |
| 43 |
*/ |
| 44 |
public $billing_cycle; |
| 45 |
/** |
| 46 |
* @var float |
| 47 |
*/ |
| 48 |
public $outstanding_balance; |
| 49 |
/** |
| 50 |
* @var int |
| 51 |
*/ |
| 52 |
public $failed_payments; |
| 53 |
/** |
| 54 |
* @var string |
| 55 |
*/ |
| 56 |
public $gateway; |
| 57 |
/** |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
public $external_id; |
| 61 |
/** |
| 62 |
* @var string|null |
| 63 |
*/ |
| 64 |
public $trial_ends; |
| 65 |
/** |
| 66 |
* @var string|null Datetime of the next payment, or null if cancelled. |
| 67 |
*/ |
| 68 |
public $next_payment; |
| 69 |
/** |
| 70 |
* @since 2.3.1 |
| 71 |
* |
| 72 |
* @var string|null Datetime of the cancellation. |
| 73 |
*/ |
| 74 |
public $canceled_at; |
| 75 |
/** |
| 76 |
* @var string|null |
| 77 |
*/ |
| 78 |
public $vat_id; |
| 79 |
/** |
| 80 |
* @var string Two characters country code |
| 81 |
*/ |
| 82 |
public $country_code; |
| 83 |
|
| 84 |
#endregion Properties |
| 85 |
|
| 86 |
/** |
| 87 |
* @param object|bool $subscription |
| 88 |
*/ |
| 89 |
function __construct( $subscription = false ) { |
| 90 |
parent::__construct( $subscription ); |
| 91 |
} |
| 92 |
|
| 93 |
static function get_type() { |
| 94 |
return 'subscription'; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Check if subscription is active. |
| 99 |
* |
| 100 |
* @author Vova Feldman (@svovaf) |
| 101 |
* @since 1.0.9 |
| 102 |
* |
| 103 |
* @return bool |
| 104 |
*/ |
| 105 |
function is_active() { |
| 106 |
if ( $this->is_canceled() ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
return ( |
| 111 |
! empty( $this->next_payment ) && |
| 112 |
strtotime( $this->next_payment ) > WP_FS__SCRIPT_START_TIME |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @author Vova Feldman (@svovaf) |
| 118 |
* @since 2.3.1 |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
*/ |
| 122 |
function is_canceled() { |
| 123 |
return ! is_null( $this->canceled_at ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Subscription considered to be new without any payments |
| 128 |
* if the next payment should be made within less than 24 hours |
| 129 |
* from the subscription creation. |
| 130 |
* |
| 131 |
* @author Vova Feldman (@svovaf) |
| 132 |
* @since 1.0.9 |
| 133 |
* |
| 134 |
* @return bool |
| 135 |
*/ |
| 136 |
function is_first_payment_pending() { |
| 137 |
return ( WP_FS__TIME_24_HOURS_IN_SEC >= strtotime( $this->next_payment ) - strtotime( $this->created ) ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @author Vova Feldman (@svovaf) |
| 142 |
* @since 1.1.7 |
| 143 |
*/ |
| 144 |
function has_trial() { |
| 145 |
return ! is_null( $this->trial_ends ); |
| 146 |
} |
| 147 |
} |