PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.5
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.5
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / stripe / controllers / FrmTransLiteAppController.php

FrmTransLiteAppController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.5, at stripe/controllers/FrmTransLiteAppController.php

159 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @return void
10 */
11 public static function install( $old_db_version = false ) {
12 self::maybe_schedule_cron();
13
14 $db = new FrmTransLiteDb();
15 $db->upgrade( $old_db_version );
16 }
17
18 /**
19 * Schedule the payment cron if it is not already scheduled.
20 *
21 * @return void
22 */
23 public static function maybe_schedule_cron() {
24 if ( ! wp_next_scheduled( 'frm_payment_cron' ) ) {
25 wp_schedule_event( time(), 'daily', 'frm_payment_cron' );
26 }
27 }
28
29 /**
30 * Remove the cron when the plugin is deactivated.
31 *
32 * @return void
33 */
34 public static function remove_cron() {
35 wp_clear_scheduled_hook( 'frm_payment_cron' );
36 }
37
38 /**
39 * Process overdue subscriptions.
40 *
41 * @return void
42 */
43 public static function run_payment_cron() {
44 $frm_sub = new FrmTransLiteSubscription();
45 $frm_payment = new FrmTransLitePayment();
46
47 $overdue_subscriptions = $frm_sub->get_overdue_subscriptions();
48 FrmTransLiteLog::log_message( 'Stripe Cron Message', count( $overdue_subscriptions ) . ' subscriptions found to be processed.' );
49
50 foreach ( $overdue_subscriptions as $sub ) {
51 $last_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' );
52
53 $log_message = 'Subscription #' . $sub->id . ': ';
54 if ( $sub->status === 'future_cancel' ) {
55 FrmTransLiteSubscriptionsController::change_subscription_status(
56 array(
57 'status' => 'canceled',
58 'sub' => $sub,
59 )
60 );
61
62 $status = 'failed';
63 $log_message .= 'Failed triggers run on canceled subscription. ';
64 } else {
65 // Get the most recent payment after the gateway has a chance to create one.
66 $check_payment = $frm_payment->get_one_by( $sub->id, 'sub_id' );
67 $new_payment = $check_payment->id != $last_payment->id;
68 $last_payment = $check_payment;
69 $status = 'no';
70
71 if ( ! $last_payment ) {
72 $log_message .= 'No payments found for subscription #' . $sub->id . '. ';
73 self::add_one_fail( $sub );
74 } elseif ( $new_payment ) {
75 $status = $last_payment->status;
76 self::update_sub_for_new_payment( $sub, $last_payment );
77 } elseif ( $last_payment->expire_date < gmdate( 'Y-m-d' ) ) {
78 // the payment has expired, and no new payment was made
79 $status = 'failed';
80 self::add_one_fail( $sub );
81 } else {
82 // don't run any triggers
83 $last_payment = false;
84 }
85
86 $log_message .= $status . ' triggers run ';
87 if ( $last_payment ) {
88 $log_message .= 'on payment #' . $last_payment->id;
89 }
90 }
91
92 FrmTransLiteLog::log_message( 'Stripe Cron Message', $log_message );
93
94 self::maybe_trigger_changes(
95 array(
96 'status' => $status,
97 'payment' => $last_payment,
98 )
99 );
100
101 unset( $sub );
102 }
103 }
104
105 /**
106 * @param object $sub
107 * @param object $last_payment
108 *
109 * @return void
110 */
111 private static function update_sub_for_new_payment( $sub, $last_payment ) {
112 $frm_sub = new FrmTransLiteSubscription();
113 if ( $last_payment->status === 'complete' ) {
114 $frm_sub->update(
115 $sub->id,
116 array(
117 'fail_count' => 0,
118 'next_bill_date' => $last_payment->expire_date,
119 )
120 );
121 } elseif ( $last_payment->status === 'failed' ) {
122 self::add_one_fail( $sub );
123 }
124 }
125
126 /**
127 * Add to the fail count.
128 * If the subscription has failed > 3 times, set it to canceled.
129 *
130 * @param object $sub
131 * @return void
132 */
133 private static function add_one_fail( $sub ) {
134 $frm_sub = new FrmTransLiteSubscription();
135 $fail_count = $sub->fail_count + 1;
136 $new_values = compact( 'fail_count' );
137 $frm_sub->update( $sub->id, $new_values );
138
139 if ( $fail_count > 3 ) {
140 FrmTransLiteSubscriptionsController::change_subscription_status(
141 array(
142 'status' => 'canceled',
143 'sub' => $sub,
144 )
145 );
146 }
147 }
148
149 /**
150 * @param array $atts
151 * @return void
152 */
153 private static function maybe_trigger_changes( $atts ) {
154 if ( $atts['payment'] ) {
155 FrmTransLiteActionsController::trigger_payment_status_change( $atts );
156 }
157 }
158 }
159