← All changes
|
stripe/controllers/FrmTransLiteSubscriptionsController.php
+84
-28
6.5.2
→
trunk
View file →
| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | class FrmTransLiteSubscriptionsController extends FrmTransLiteCRUDController { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * @param object $subscription |
| 10 | + * | |
| 10 | 11 | * @return void |
| 11 | 12 | */ |
| 12 | 13 | public static function load_sidebar_actions( $subscription ) { |
| 13 | 14 | $date_format = __( 'M j, Y @ G:i', 'formidable' ); |
| @@ -23,12 +24,14 @@ | ||
| 23 | 24 | /** |
| 24 | 25 | * @since 6.5 |
| 25 | 26 | * |
| 26 | 27 | * @param object $subscription |
| 28 | + * | |
| 27 | 29 | * @return void |
| 28 | 30 | */ |
| 29 | 31 | public static function show_receipt_link( $subscription ) { |
| 30 | 32 | $link = esc_html( $subscription->sub_id ); |
| 33 | + | |
| 31 | 34 | if ( $subscription->sub_id !== 'None' ) { |
| 32 | 35 | /** |
| 33 | 36 | * Filter a receipt link for a specific gateway. |
| 34 | 37 | * For example, Stripe uses frm_sub_stripe_receipt. |
| @@ -39,14 +42,15 @@ | ||
| 39 | 42 | */ |
| 40 | 43 | $link = apply_filters( 'frm_sub_' . $subscription->paysys . '_receipt', $link ); |
| 41 | 44 | } |
| 42 | 45 | |
| 43 | - echo FrmAppHelper::kses( $link, array( 'a' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 46 | + FrmAppHelper::kses_echo( $link, array( 'a' ) ); | |
| 44 | 47 | } |
| 45 | 48 | |
| 46 | 49 | /** |
| 47 | 50 | * @param object $sub |
| 48 | 51 | * @param array $atts |
| 52 | + * | |
| 49 | 53 | * @return void |
| 50 | 54 | */ |
| 51 | 55 | public static function show_cancel_link( $sub, $atts = array() ) { |
| 52 | 56 | if ( ! isset( $sub->user_id ) ) { |
| @@ -60,8 +64,9 @@ | ||
| 60 | 64 | |
| 61 | 65 | /** |
| 62 | 66 | * @param object $sub |
| 63 | 67 | * @param array $atts |
| 68 | + * | |
| 64 | 69 | * @return string |
| 65 | 70 | */ |
| 66 | 71 | public static function cancel_link( $sub, $atts = array() ) { |
| 67 | 72 | $defaults = array( |
| @@ -87,17 +92,61 @@ | ||
| 87 | 92 | * Handle routing to cancel a subscription. |
| 88 | 93 | * |
| 89 | 94 | * @return void |
| 90 | 95 | */ |
| 91 | - public static function cancel_subscription() { | |
| 96 | + public static function cancel_subscription() { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh | |
| 92 | 97 | check_ajax_referer( 'frm_trans_ajax', 'nonce' ); |
| 98 | + FrmAppHelper::permission_check( 'frm_edit_entries' ); | |
| 93 | 99 | |
| 94 | - $sub_id = FrmAppHelper::get_param( 'sub', '', 'get', 'sanitize_text_field' ); | |
| 100 | + $canceled = false; | |
| 101 | + $sub_id = FrmAppHelper::get_param( 'sub', '', 'get', 'sanitize_text_field' ); | |
| 102 | + | |
| 95 | 103 | if ( $sub_id ) { |
| 96 | 104 | $frm_sub = new FrmTransLiteSubscription(); |
| 97 | 105 | $sub = $frm_sub->get_one( $sub_id ); |
| 106 | + | |
| 98 | 107 | if ( $sub ) { |
| 99 | - $canceled = FrmStrpLiteAppHelper::call_stripe_helper_class( 'cancel_subscription', $sub->sub_id ); | |
| 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 | + | |
| 100 | 149 | if ( $canceled ) { |
| 101 | 150 | self::change_subscription_status( |
| 102 | 151 | array( |
| 103 | 152 | 'status' => 'future_cancel', |
| @@ -105,20 +154,40 @@ | ||
| 105 | 154 | ) |
| 106 | 155 | ); |
| 107 | 156 | |
| 108 | 157 | $message = __( 'Canceled', 'formidable' ); |
| 158 | + // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found | |
| 109 | 159 | } else { |
| 110 | - $message = __( 'Failed', 'formidable' ); | |
| 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 ); | |
| 111 | 175 | } |
| 112 | 176 | } else { |
| 113 | 177 | $message = __( 'That subscription was not found', 'formidable' ); |
| 114 | - } | |
| 178 | + }//end if | |
| 115 | 179 | } else { |
| 116 | 180 | $message = __( 'Oops! No subscription was selected for cancelation.', 'formidable' ); |
| 117 | - } | |
| 181 | + }//end if | |
| 118 | 182 | |
| 119 | - echo esc_html( $message ); | |
| 120 | - wp_die(); | |
| 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 | + ); | |
| 121 | 190 | } |
| 122 | 191 | |
| 123 | 192 | /** |
| 124 | 193 | * @since 6.5, introduced in v1.12 of the Payments submodule. |
| @@ -123,8 +192,9 @@ | ||
| 123 | 192 | /** |
| 124 | 193 | * @since 6.5, introduced in v1.12 of the Payments submodule. |
| 125 | 194 | * |
| 126 | 195 | * @param array $atts |
| 196 | + * | |
| 127 | 197 | * @return void |
| 128 | 198 | */ |
| 129 | 199 | public static function change_subscription_status( $atts ) { |
| 130 | 200 | $frm_sub = new FrmTransLiteSubscription(); |
| @@ -134,27 +204,13 @@ | ||
| 134 | 204 | FrmTransLiteActionsController::trigger_subscription_status_change( $atts['sub'] ); |
| 135 | 205 | } |
| 136 | 206 | |
| 137 | 207 | /** |
| 138 | - * @return null|string | |
| 208 | + * @deprecated 6.27 | |
| 209 | + * | |
| 210 | + * @return string|null | |
| 139 | 211 | */ |
| 140 | 212 | public static function list_subscriptions_shortcode() { |
| 141 | - if ( ! is_user_logged_in() ) { | |
| 142 | - return; | |
| 143 | - } | |
| 144 | - | |
| 145 | - $frm_sub = new FrmTransLiteSubscription(); | |
| 146 | - $subscriptions = $frm_sub->get_all_for_user( get_current_user_id() ); | |
| 147 | - if ( empty( $subscriptions ) ) { | |
| 148 | - return; | |
| 149 | - } | |
| 150 | - | |
| 151 | - FrmTransLiteActionsController::actions_js(); | |
| 152 | - | |
| 153 | - ob_start(); | |
| 154 | - include FrmTransLiteAppHelper::plugin_path() . '/views/subscriptions/list_shortcode.php'; | |
| 155 | - $content = ob_get_contents(); | |
| 156 | - ob_end_clean(); | |
| 157 | - | |
| 158 | - return $content; | |
| 213 | + _deprecated_function( __METHOD__, '6.27' ); | |
| 214 | + return null; | |
| 159 | 215 | } |
| 160 | 216 | } |