| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmTransLiteSubscriptionsController extends FrmTransLiteCRUDController { |
| 7 |
|
| 8 |
/** |
| 9 |
* @param object $subscription |
| 10 |
* |
| 11 |
* @return void |
| 12 |
*/ |
| 13 |
public static function load_sidebar_actions( $subscription ) { |
| 14 |
$date_format = __( 'M j, Y @ G:i', 'formidable' ); |
| 15 |
|
| 16 |
FrmTransLiteActionsController::actions_js(); |
| 17 |
|
| 18 |
$frm_payment = new FrmTransLitePayment(); |
| 19 |
$payments = $frm_payment->get_all_by( $subscription->id, 'sub_id' ); |
| 20 |
|
| 21 |
include FrmTransLiteAppHelper::plugin_path() . '/views/subscriptions/sidebar_actions.php'; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* @since 6.5 |
| 26 |
* |
| 27 |
* @param object $subscription |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public static function show_receipt_link( $subscription ) { |
| 32 |
$link = esc_html( $subscription->sub_id ); |
| 33 |
|
| 34 |
if ( $subscription->sub_id !== 'None' ) { |
| 35 |
/** |
| 36 |
* Filter a receipt link for a specific gateway. |
| 37 |
* For example, Stripe uses frm_sub_stripe_receipt. |
| 38 |
* |
| 39 |
* @since 6.5 |
| 40 |
* |
| 41 |
* @param string $link |
| 42 |
*/ |
| 43 |
$link = apply_filters( 'frm_sub_' . $subscription->paysys . '_receipt', $link ); |
| 44 |
} |
| 45 |
|
| 46 |
FrmAppHelper::kses_echo( $link, array( 'a' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @param object $sub |
| 51 |
* @param array $atts |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public static function show_cancel_link( $sub, $atts = array() ) { |
| 56 |
if ( ! isset( $sub->user_id ) ) { |
| 57 |
global $wpdb; |
| 58 |
$sub->user_id = $wpdb->get_var( $wpdb->prepare( 'SELECT user_id FROM ' . $wpdb->prefix . 'frm_items WHERE id=%d', $sub->item_id ) ); |
| 59 |
} |
| 60 |
|
| 61 |
$link = self::cancel_link( $sub, $atts ); |
| 62 |
FrmTransLiteAppHelper::echo_confirmation_link( $link ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @param object $sub |
| 67 |
* @param array $atts |
| 68 |
* |
| 69 |
* @return string |
| 70 |
*/ |
| 71 |
public static function cancel_link( $sub, $atts = array() ) { |
| 72 |
$defaults = array( |
| 73 |
'cancel' => __( 'Cancel', 'formidable' ), |
| 74 |
'canceled' => __( 'Canceled', 'formidable' ), |
| 75 |
'confirm' => __( 'Are you sure you want to cancel that subscription?', 'formidable' ), |
| 76 |
); |
| 77 |
$atts = array_merge( $defaults, $atts ); |
| 78 |
|
| 79 |
if ( $sub->status === 'active' ) { |
| 80 |
$link = admin_url( 'admin-ajax.php?action=frm_trans_cancel&sub=' . $sub->id . '&nonce=' . wp_create_nonce( 'frm_trans_ajax' ) ); |
| 81 |
$link = '<a href="' . esc_url( $link ) . '" class="frm_trans_ajax_link" data-frmverify="' . esc_attr( $atts['confirm'] ) . '" data-frmverify-btn="frm-button-red">'; |
| 82 |
$link .= esc_html( $atts['cancel'] ); |
| 83 |
$link .= '</a>'; |
| 84 |
} else { |
| 85 |
$link = esc_html( $atts['canceled'] ); |
| 86 |
} |
| 87 |
|
| 88 |
return $link; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Handle routing to cancel a subscription. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public static function cancel_subscription() { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh |
| 97 |
check_ajax_referer( 'frm_trans_ajax', 'nonce' ); |
| 98 |
FrmAppHelper::permission_check( 'frm_edit_entries' ); |
| 99 |
|
| 100 |
$canceled = false; |
| 101 |
$sub_id = FrmAppHelper::get_param( 'sub', '', 'get', 'sanitize_text_field' ); |
| 102 |
|
| 103 |
if ( $sub_id ) { |
| 104 |
$frm_sub = new FrmTransLiteSubscription(); |
| 105 |
$sub = $frm_sub->get_one( $sub_id ); |
| 106 |
|
| 107 |
if ( $sub ) { |
| 108 |
$reason = ''; |
| 109 |
$debug_id = ''; |
| 110 |
|
| 111 |
switch ( $sub->paysys ) { |
| 112 |
case 'stripe': |
| 113 |
$canceled = FrmStrpLiteAppHelper::call_stripe_helper_class( 'cancel_subscription', $sub->sub_id ); |
| 114 |
break; |
| 115 |
case 'square': |
| 116 |
$canceled = FrmSquareLiteConnectHelper::cancel_subscription( $sub->sub_id ); |
| 117 |
break; |
| 118 |
case 'paypal': |
| 119 |
$response = FrmPayPalLiteConnectHelper::cancel_subscription( $sub->sub_id ); |
| 120 |
|
| 121 |
// Check for structured error response with message and debug_id (array or object) |
| 122 |
$reason = ''; |
| 123 |
$debug_id = ''; |
| 124 |
$canceled = false !== $response; |
| 125 |
|
| 126 |
if ( is_array( $response ) || is_object( $response ) ) { |
| 127 |
// Extract error details without type checks to avoid Mago type narrowing |
| 128 |
$response_array = is_array( $response ) ? $response : (array) $response; |
| 129 |
$reason = $response_array['message'] ?? ''; |
| 130 |
// PayPal API returns debug_id (snake_case) in some cases and debugId (camelCase) in others |
| 131 |
$debug_id = $response_array['debug_id'] ?? $response_array['debugId'] ?? ''; |
| 132 |
|
| 133 |
// If there's an error message or debug_id, the cancellation failed |
| 134 |
if ( $reason || $debug_id ) { |
| 135 |
$canceled = false; |
| 136 |
} |
| 137 |
} elseif ( false === $response ) { |
| 138 |
// Response is false, get error from static properties |
| 139 |
$reason = FrmPayPalLiteConnectHelper::get_latest_error_from_paypal_api(); |
| 140 |
$debug_id = FrmPayPalLiteConnectHelper::get_latest_debug_id_from_paypal_api(); |
| 141 |
$canceled = false; |
| 142 |
} |
| 143 |
break; |
| 144 |
default: |
| 145 |
$canceled = false; |
| 146 |
break; |
| 147 |
}//end switch |
| 148 |
|
| 149 |
if ( $canceled ) { |
| 150 |
self::change_subscription_status( |
| 151 |
array( |
| 152 |
'status' => 'future_cancel', |
| 153 |
'sub' => $sub, |
| 154 |
) |
| 155 |
); |
| 156 |
|
| 157 |
$message = __( 'Canceled', 'formidable' ); |
| 158 |
// phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found |
| 159 |
} else { |
| 160 |
// If the reason is already a complete error message, use it directly |
| 161 |
// instead of wrapping it redundantly in "Failed (...)" |
| 162 |
if ( $reason && ! preg_match( '/^[A-Z_]+$/', $reason ) ) { |
| 163 |
$message = $reason; |
| 164 |
} else { |
| 165 |
$message = __( 'Failed', 'formidable' ); |
| 166 |
|
| 167 |
if ( ! empty( $reason ) ) { |
| 168 |
$message .= ' (' . $reason . ')'; |
| 169 |
} |
| 170 |
} |
| 171 |
}//end if |
| 172 |
|
| 173 |
if ( $debug_id ) { |
| 174 |
$message .= '<br><br>Debug ID: ' . esc_html( $debug_id ); |
| 175 |
} |
| 176 |
} else { |
| 177 |
$message = __( 'That subscription was not found', 'formidable' ); |
| 178 |
}//end if |
| 179 |
} else { |
| 180 |
$message = __( 'Oops! No subscription was selected for cancelation.', 'formidable' ); |
| 181 |
}//end if |
| 182 |
|
| 183 |
wp_die( |
| 184 |
sprintf( |
| 185 |
'<div class="%1$s">%2$s</div>', |
| 186 |
$canceled ? 'frm_updated_message' : 'frm_error_style', |
| 187 |
wp_kses_post( $message ) |
| 188 |
) |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @since 6.5, introduced in v1.12 of the Payments submodule. |
| 194 |
* |
| 195 |
* @param array $atts |
| 196 |
* |
| 197 |
* @return void |
| 198 |
*/ |
| 199 |
public static function change_subscription_status( $atts ) { |
| 200 |
$frm_sub = new FrmTransLiteSubscription(); |
| 201 |
$frm_sub->update( $atts['sub']->id, array( 'status' => $atts['status'] ) ); |
| 202 |
$atts['sub']->status = $atts['status']; |
| 203 |
|
| 204 |
FrmTransLiteActionsController::trigger_subscription_status_change( $atts['sub'] ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* @deprecated 6.27 |
| 209 |
* |
| 210 |
* @return string|null |
| 211 |
*/ |
| 212 |
public static function list_subscriptions_shortcode() { |
| 213 |
_deprecated_function( __METHOD__, '6.27' ); |
| 214 |
return null; |
| 215 |
} |
| 216 |
} |
| 217 |
|