| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmTransLiteAppController { |
| 7 |
|
| 8 |
/** |
| 9 |
* Install or upgrade database structures. |
| 10 |
* |
| 11 |
* @param mixed $old_db_version Previous database version, or false on fresh install. |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function install( $old_db_version = false ) { |
| 16 |
self::maybe_schedule_cron(); |
| 17 |
|
| 18 |
$db = new FrmTransLiteDb(); |
| 19 |
$db->upgrade( $old_db_version ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* This is called on the frm_after_install hook that is called when Lite migrations have run. |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public static function on_after_install() { |
| 28 |
if ( ! FrmTransLiteAppHelper::payments_table_exists() ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$db = new FrmTransLiteDb(); |
| 33 |
$db->upgrade(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Schedule the payment cron if it is not already scheduled. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public static function maybe_schedule_cron() { |
| 42 |
if ( ! wp_next_scheduled( 'frm_payment_cron' ) ) { |
| 43 |
wp_schedule_event( time(), 'daily', 'frm_payment_cron' ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Remove the cron when the plugin is deactivated. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public static function remove_cron() { |
| 53 |
wp_clear_scheduled_hook( 'frm_payment_cron' ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Process overdue subscriptions. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public static function run_payment_cron() { |
| 62 |
$frm_sub = new FrmTransLiteSubscription(); |
| 63 |
$frm_payment = new FrmTransLitePayment(); |
| 64 |
|
| 65 |
$overdue_subscriptions = $frm_sub->get_overdue_subscriptions(); |
| 66 |
FrmTransLiteLog::log_message( 'Stripe Cron Message', count( $overdue_subscriptions ) . ' subscriptions found to be processed.', false ); |
| 67 |
|
| 68 |
foreach ( $overdue_subscriptions as $sub ) { |
| 69 |
$last_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' ); |
| 70 |
|
| 71 |
if ( ! $last_payment ) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
$log_message = 'Subscription #' . $sub->id . ': '; |
| 76 |
|
| 77 |
if ( $sub->status === 'future_cancel' ) { |
| 78 |
FrmTransLiteSubscriptionsController::change_subscription_status( |
| 79 |
array( |
| 80 |
'status' => 'canceled', |
| 81 |
'sub' => $sub, |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
$status = 'failed'; |
| 86 |
$log_message .= 'Failed triggers run on canceled subscription. '; |
| 87 |
} else { |
| 88 |
// Get the most recent payment after the gateway has a chance to create one. |
| 89 |
$check_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' ); |
| 90 |
$new_payment = $check_payment->id != $last_payment->id; |
| 91 |
$last_payment = $check_payment; |
| 92 |
$status = 'no'; |
| 93 |
|
| 94 |
if ( ! $last_payment ) { |
| 95 |
$log_message .= 'No payments found for subscription #' . $sub->id . '. '; |
| 96 |
self::add_one_fail( $sub ); |
| 97 |
} elseif ( $new_payment ) { |
| 98 |
$status = $last_payment->status; |
| 99 |
self::update_sub_for_new_payment( $sub, $last_payment ); |
| 100 |
} elseif ( $last_payment->expire_date < gmdate( 'Y-m-d' ) ) { |
| 101 |
// The payment has expired, and no new payment was made. |
| 102 |
$status = 'failed'; |
| 103 |
self::add_one_fail( $sub ); |
| 104 |
} else { |
| 105 |
// Don't run any triggers. |
| 106 |
$last_payment = false; |
| 107 |
} |
| 108 |
|
| 109 |
$log_message .= $status . ' triggers run '; |
| 110 |
|
| 111 |
if ( $last_payment ) { |
| 112 |
$log_message .= 'on payment #' . $last_payment->id; |
| 113 |
} |
| 114 |
}//end if |
| 115 |
|
| 116 |
FrmTransLiteLog::log_message( 'Stripe Cron Message', $log_message ); |
| 117 |
|
| 118 |
self::maybe_trigger_changes( |
| 119 |
array( |
| 120 |
'status' => $status, |
| 121 |
'payment' => $last_payment, |
| 122 |
) |
| 123 |
); |
| 124 |
|
| 125 |
unset( $sub ); |
| 126 |
}//end foreach |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @param object $sub |
| 131 |
* @param object $last_payment |
| 132 |
* |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
private static function update_sub_for_new_payment( $sub, $last_payment ) { |
| 136 |
$frm_sub = new FrmTransLiteSubscription(); |
| 137 |
|
| 138 |
if ( $last_payment->status === 'complete' ) { |
| 139 |
$frm_sub->update( |
| 140 |
$sub->id, |
| 141 |
array( |
| 142 |
'fail_count' => 0, |
| 143 |
'next_bill_date' => $last_payment->expire_date, |
| 144 |
) |
| 145 |
); |
| 146 |
} elseif ( $last_payment->status === 'failed' ) { |
| 147 |
self::add_one_fail( $sub ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Add to the fail count. |
| 153 |
* If the subscription has failed > 3 times, set it to canceled. |
| 154 |
* |
| 155 |
* @param object $sub |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
private static function add_one_fail( $sub ) { |
| 160 |
$frm_sub = new FrmTransLiteSubscription(); |
| 161 |
$fail_count = $sub->fail_count + 1; |
| 162 |
$new_values = compact( 'fail_count' ); |
| 163 |
$frm_sub->update( $sub->id, $new_values ); |
| 164 |
|
| 165 |
if ( $fail_count > 3 ) { |
| 166 |
FrmTransLiteSubscriptionsController::change_subscription_status( |
| 167 |
array( |
| 168 |
'status' => 'canceled', |
| 169 |
'sub' => $sub, |
| 170 |
) |
| 171 |
); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @param array $atts |
| 177 |
* |
| 178 |
* @return void |
| 179 |
*/ |
| 180 |
private static function maybe_trigger_changes( $atts ) { |
| 181 |
if ( $atts['payment'] ) { |
| 182 |
FrmTransLiteActionsController::trigger_payment_status_change( $atts ); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* This is called when the Payments submodule is active. |
| 188 |
* It ensures that the hidden repeater cadence input exists even when another add-on is handling the settings. |
| 189 |
* |
| 190 |
* @since 6.22 |
| 191 |
* |
| 192 |
* @param array $args |
| 193 |
* |
| 194 |
* @return void |
| 195 |
*/ |
| 196 |
public static function add_repeat_cadence_value( $args ) { |
| 197 |
$action = $args['form_action']; |
| 198 |
|
| 199 |
if ( ! empty( $action->post_content['repeat_cadence'] ) ) { |
| 200 |
$params = array( |
| 201 |
'type' => 'hidden', |
| 202 |
'class' => 'frm-repeat-cadence-value', |
| 203 |
'value' => $action->post_content['repeat_cadence'], |
| 204 |
); |
| 205 |
echo '<input '; |
| 206 |
FrmAppHelper::array_to_html_params( $params, true ); |
| 207 |
echo ' />'; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|