← All changes
|
stripe/controllers/FrmTransLiteSubscriptionsController.php
+61
-25
6.26
→
trunk
View file →
| @@ -92,12 +92,14 @@ | ||
| 92 | 92 | * Handle routing to cancel a subscription. |
| 93 | 93 | * |
| 94 | 94 | * @return void |
| 95 | 95 | */ |
| 96 | - public static function cancel_subscription() { | |
| 96 | + public static function cancel_subscription() { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh | |
| 97 | 97 | check_ajax_referer( 'frm_trans_ajax', 'nonce' ); |
| 98 | + FrmAppHelper::permission_check( 'frm_edit_entries' ); | |
| 98 | 99 | |
| 99 | - $sub_id = FrmAppHelper::get_param( 'sub', '', 'get', 'sanitize_text_field' ); | |
| 100 | + $canceled = false; | |
| 101 | + $sub_id = FrmAppHelper::get_param( 'sub', '', 'get', 'sanitize_text_field' ); | |
| 100 | 102 | |
| 101 | 103 | if ( $sub_id ) { |
| 102 | 104 | $frm_sub = new FrmTransLiteSubscription(); |
| 103 | 105 | $sub = $frm_sub->get_one( $sub_id ); |
| @@ -102,8 +104,11 @@ | ||
| 102 | 104 | $frm_sub = new FrmTransLiteSubscription(); |
| 103 | 105 | $sub = $frm_sub->get_one( $sub_id ); |
| 104 | 106 | |
| 105 | 107 | if ( $sub ) { |
| 108 | + $reason = ''; | |
| 109 | + $debug_id = ''; | |
| 110 | + | |
| 106 | 111 | switch ( $sub->paysys ) { |
| 107 | 112 | case 'stripe': |
| 108 | 113 | $canceled = FrmStrpLiteAppHelper::call_stripe_helper_class( 'cancel_subscription', $sub->sub_id ); |
| 109 | 114 | break; |
| @@ -109,12 +114,38 @@ | ||
| 109 | 114 | break; |
| 110 | 115 | case 'square': |
| 111 | 116 | $canceled = FrmSquareLiteConnectHelper::cancel_subscription( $sub->sub_id ); |
| 112 | 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; | |
| 113 | 144 | default: |
| 114 | 145 | $canceled = false; |
| 115 | 146 | break; |
| 116 | - } | |
| 147 | + }//end switch | |
| 117 | 148 | |
| 118 | 149 | if ( $canceled ) { |
| 119 | 150 | self::change_subscription_status( |
| 120 | 151 | array( |
| @@ -123,10 +154,25 @@ | ||
| 123 | 154 | ) |
| 124 | 155 | ); |
| 125 | 156 | |
| 126 | 157 | $message = __( 'Canceled', 'formidable' ); |
| 158 | + // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found | |
| 127 | 159 | } else { |
| 128 | - $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 ); | |
| 129 | 175 | } |
| 130 | 176 | } else { |
| 131 | 177 | $message = __( 'That subscription was not found', 'formidable' ); |
| 132 | 178 | }//end if |
| @@ -133,10 +179,15 @@ | ||
| 133 | 179 | } else { |
| 134 | 180 | $message = __( 'Oops! No subscription was selected for cancelation.', 'formidable' ); |
| 135 | 181 | }//end if |
| 136 | 182 | |
| 137 | - echo esc_html( $message ); | |
| 138 | - 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 | + ); | |
| 139 | 190 | } |
| 140 | 191 | |
| 141 | 192 | /** |
| 142 | 193 | * @since 6.5, introduced in v1.12 of the Payments submodule. |
| @@ -153,28 +204,13 @@ | ||
| 153 | 204 | FrmTransLiteActionsController::trigger_subscription_status_change( $atts['sub'] ); |
| 154 | 205 | } |
| 155 | 206 | |
| 156 | 207 | /** |
| 208 | + * @deprecated 6.27 | |
| 209 | + * | |
| 157 | 210 | * @return string|null |
| 158 | 211 | */ |
| 159 | 212 | public static function list_subscriptions_shortcode() { |
| 160 | - if ( ! is_user_logged_in() ) { | |
| 161 | - return; | |
| 162 | - } | |
| 163 | - | |
| 164 | - $frm_sub = new FrmTransLiteSubscription(); | |
| 165 | - $subscriptions = $frm_sub->get_all_for_user( get_current_user_id() ); | |
| 166 | - | |
| 167 | - if ( empty( $subscriptions ) ) { | |
| 168 | - return; | |
| 169 | - } | |
| 170 | - | |
| 171 | - FrmTransLiteActionsController::actions_js(); | |
| 172 | - | |
| 173 | - ob_start(); | |
| 174 | - include FrmTransLiteAppHelper::plugin_path() . '/views/subscriptions/list_shortcode.php'; | |
| 175 | - $content = ob_get_contents(); | |
| 176 | - ob_end_clean(); | |
| 177 | - | |
| 178 | - return $content; | |
| 213 | + _deprecated_function( __METHOD__, '6.27' ); | |
| 214 | + return null; | |
| 179 | 215 | } |
| 180 | 216 | } |