Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 6 additions & 102 deletions stripe/controllers/FrmTransLiteAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,121 +81,25 @@ public static function run_payment_cron() {
FrmTransLiteLog::log_message( 'Overdue Subscription Cron Message', count( $overdue_subscriptions ) . ' subscriptions found to be processed.', false );

foreach ( $overdue_subscriptions as $sub ) {
if ( $sub->status !== 'future_cancel' ) {
continue;
}

$last_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' );

if ( ! $last_payment ) {
continue;
}

$log_message = 'Subscription #' . $sub->id . ': ';

if ( $sub->status === 'future_cancel' ) {
FrmTransLiteSubscriptionsController::change_subscription_status(
array(
'status' => 'canceled',
'sub' => $sub,
)
);

$status = 'failed';
$log_message .= 'Failed triggers run on canceled subscription. ';
} else {
// Get the most recent payment after the gateway has a chance to create one.
$check_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' );
$new_payment = (int) $check_payment->id !== (int) $last_payment->id;
$last_payment = $check_payment;
$status = 'no';

if ( ! $last_payment ) {
$log_message .= 'No payments found for subscription #' . $sub->id . '. ';
self::add_one_fail( $sub );
} elseif ( $new_payment ) {
$status = $last_payment->status;
self::update_sub_for_new_payment( $sub, $last_payment );
} elseif ( $last_payment->expire_date < gmdate( 'Y-m-d' ) ) {
// The payment has expired, and no new payment was made.
$status = 'failed';
self::add_one_fail( $sub );
} else {
// Don't run any triggers.
$last_payment = false;
}

$log_message .= $status . ' triggers run ';

if ( $last_payment ) {
$log_message .= 'on payment #' . $last_payment->id;
}
}//end if

FrmTransLiteLog::log_message( 'Overdue Subscription Cron Message', $log_message );

self::maybe_trigger_changes(
array(
'status' => $status,
'payment' => $last_payment,
)
);

unset( $sub );
}//end foreach
}

/**
* @param object $sub
* @param object $last_payment
*
* @return void
*/
private static function update_sub_for_new_payment( $sub, $last_payment ) {
$frm_sub = new FrmTransLiteSubscription();

if ( $last_payment->status === 'complete' ) {
$frm_sub->update(
$sub->id,
array(
'fail_count' => 0,
'next_bill_date' => $last_payment->expire_date,
)
);
} elseif ( $last_payment->status === 'failed' ) {
self::add_one_fail( $sub );
}
}

/**
* Add to the fail count.
* If the subscription has failed > 3 times, set it to canceled.
*
* @param object $sub
*
* @return void
*/
private static function add_one_fail( $sub ) {
$frm_sub = new FrmTransLiteSubscription();
$fail_count = $sub->fail_count + 1;
$new_values = compact( 'fail_count' );
$frm_sub->update( $sub->id, $new_values );

if ( $fail_count > 3 ) {
FrmTransLiteSubscriptionsController::change_subscription_status(
array(
'status' => 'canceled',
'sub' => $sub,
)
);
}
}

/**
* @param array $atts
*
* @return void
*/
private static function maybe_trigger_changes( $atts ) {
if ( $atts['payment'] ) {
FrmTransLiteActionsController::trigger_payment_status_change( $atts );
}
unset( $sub );
}//end foreach
}

/**
Expand Down
Loading