← All changes
|
stripe/controllers/FrmTransLitePaymentsController.php
+176
-17
6.5.2
→
trunk
View file →
| @@ -13,20 +13,51 @@ | ||
| 13 | 13 | |
| 14 | 14 | // Remove the PayPal submenu (PayPal payments will just appear in the regular Payments page). |
| 15 | 15 | remove_action( 'admin_menu', 'FrmPaymentsController::menu', 26 ); |
| 16 | 16 | |
| 17 | - add_submenu_page( 'formidable', $frm_settings->menu . ' | Payments', 'Payments', 'frm_view_entries', 'formidable-payments', 'FrmTransLitePaymentsController::route' ); | |
| 17 | + if ( in_array( FrmAppHelper::simple_get( 'action' ), array( 'edit', 'new', 'bulk_delete' ), true ) && is_callable( 'FrmPaymentsController::route' ) ) { | |
| 18 | + // Use the PayPal addon for add new and edit routing if it is active. | |
| 19 | + // This is required to support the "edit" link when using the Stripe Lite table view. | |
| 20 | + // It is also required for the "Add New" button to work on the payments table page. | |
| 21 | + $menu_route = 'FrmPaymentsController::route'; | |
| 22 | + } else { | |
| 23 | + $menu_route = 'FrmTransLitePaymentsController::route'; | |
| 24 | + } | |
| 25 | + | |
| 26 | + $payments_string = __( 'Payments', 'formidable' ); | |
| 27 | + add_submenu_page( | |
| 28 | + 'formidable', | |
| 29 | + $frm_settings->menu . ' | ' . $payments_string, | |
| 30 | + self::payments_menu_title( $payments_string ), | |
| 31 | + 'frm_view_entries', | |
| 32 | + 'formidable-payments', | |
| 33 | + $menu_route | |
| 34 | + ); | |
| 18 | 35 | } |
| 19 | 36 | |
| 20 | 37 | /** |
| 38 | + * @since 6.11.1 | |
| 39 | + * | |
| 40 | + * @param string $payments_string | |
| 41 | + * | |
| 42 | + * @return string | |
| 43 | + */ | |
| 44 | + private static function payments_menu_title( $payments_string ) { | |
| 45 | + ob_start(); | |
| 46 | + echo esc_html( $payments_string ); | |
| 47 | + FrmAppHelper::show_pill_text(); | |
| 48 | + return ob_get_clean(); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 21 | 52 | * @return void |
| 22 | 53 | */ |
| 23 | 54 | public static function route() { |
| 24 | - $action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; | |
| 25 | - $action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); | |
| 26 | - $type = FrmAppHelper::get_param( 'type', '', 'get', 'sanitize_title' ); | |
| 55 | + $action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; | |
| 56 | + $action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); | |
| 57 | + $type = FrmAppHelper::get_param( 'type', '', 'get', 'sanitize_title' ); | |
| 58 | + $class_name = $type === 'subscriptions' ? 'FrmTransLiteSubscriptionsController' : 'FrmTransLitePaymentsController'; | |
| 27 | 59 | |
| 28 | - $class_name = $type === 'subscriptions' ? 'FrmTransLiteSubscriptionsController' : 'FrmTransLitePaymentsController'; | |
| 29 | 60 | if ( method_exists( $class_name, $action ) ) { |
| 30 | 61 | $class_name::$action(); |
| 31 | 62 | return; |
| 32 | 63 | } |
| @@ -35,8 +66,9 @@ | ||
| 35 | 66 | } |
| 36 | 67 | |
| 37 | 68 | /** |
| 38 | 69 | * @param object $payment |
| 70 | + * | |
| 39 | 71 | * @return void |
| 40 | 72 | */ |
| 41 | 73 | public static function load_sidebar_actions( $payment ) { |
| 42 | 74 | FrmTransLiteActionsController::actions_js(); |
| @@ -49,8 +81,9 @@ | ||
| 49 | 81 | /** |
| 50 | 82 | * Echo a receipt link. |
| 51 | 83 | * |
| 52 | 84 | * @param object $payment |
| 85 | + * | |
| 53 | 86 | * @return void |
| 54 | 87 | */ |
| 55 | 88 | public static function show_receipt_link( $payment ) { |
| 56 | 89 | $link = esc_html( $payment->receipt_id ); |
| @@ -65,9 +98,9 @@ | ||
| 65 | 98 | */ |
| 66 | 99 | $link = apply_filters( 'frm_pay_' . $paysys . '_receipt', $link ); |
| 67 | 100 | } |
| 68 | 101 | |
| 69 | - echo FrmAppHelper::kses( $link, array( 'a' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 102 | + FrmAppHelper::kses_echo( $link, array( 'a' ) ); | |
| 70 | 103 | } |
| 71 | 104 | |
| 72 | 105 | /** |
| 73 | 106 | * @param string $paysys |
| @@ -82,8 +115,9 @@ | ||
| 82 | 115 | /** |
| 83 | 116 | * Echo a refund link. |
| 84 | 117 | * |
| 85 | 118 | * @param object $payment |
| 119 | + * | |
| 86 | 120 | * @return void |
| 87 | 121 | */ |
| 88 | 122 | public static function show_refund_link( $payment ) { |
| 89 | 123 | $link = self::refund_link( $payment ); |
| @@ -93,8 +127,9 @@ | ||
| 93 | 127 | /** |
| 94 | 128 | * Show a link to a payment entry (unless it is deleted). |
| 95 | 129 | * |
| 96 | 130 | * @param object $payment |
| 131 | + * | |
| 97 | 132 | * @return void |
| 98 | 133 | */ |
| 99 | 134 | public static function show_entry_link( $payment ) { |
| 100 | 135 | $entry = FrmDb::get_col( 'frm_items', array( 'id' => $payment->item_id ) ); |
| @@ -104,13 +139,15 @@ | ||
| 104 | 139 | echo esc_html( sprintf( __( '%d (Deleted)', 'formidable' ), $payment->item_id ) ); |
| 105 | 140 | return; |
| 106 | 141 | } |
| 107 | 142 | |
| 143 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 108 | 144 | ?> |
| 109 | 145 | <a href="?page=formidable-entries&action=show&frm_action=show&id=<?php echo absint( $payment->item_id ); ?>"> |
| 110 | 146 | <?php echo absint( $payment->item_id ); ?> |
| 111 | 147 | </a> |
| 112 | 148 | <?php |
| 149 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 113 | 150 | } |
| 114 | 151 | |
| 115 | 152 | /** |
| 116 | 153 | * Get a refund link. |
| @@ -115,8 +152,9 @@ | ||
| 115 | 152 | /** |
| 116 | 153 | * Get a refund link. |
| 117 | 154 | * |
| 118 | 155 | * @param object $payment |
| 156 | + * | |
| 119 | 157 | * @return string |
| 120 | 158 | */ |
| 121 | 159 | public static function refund_link( $payment ) { |
| 122 | 160 | if ( $payment->status === 'refunded' ) { |
| @@ -122,16 +160,16 @@ | ||
| 122 | 160 | if ( $payment->status === 'refunded' ) { |
| 123 | 161 | $link = esc_html__( 'Refunded', 'formidable' ); |
| 124 | 162 | } else { |
| 125 | 163 | $confirm = __( 'Are you sure you want to refund that payment?', 'formidable' ); |
| 126 | - | |
| 127 | - $link = admin_url( 'admin-ajax.php?action=frm_trans_refund&payment_id=' . $payment->id . '&nonce=' . wp_create_nonce( 'frm_trans_ajax' ) ); | |
| 128 | - $link = '<a href="' . esc_url( $link ) . '" class="frm_trans_ajax_link" data-frmverify="' . esc_attr( $confirm ) . '">'; | |
| 129 | - $link .= esc_html__( 'Refund', 'formidable' ); | |
| 130 | - $link .= '</a>'; | |
| 164 | + $link = admin_url( 'admin-ajax.php?action=frm_trans_refund&payment_id=' . $payment->id . '&nonce=' . wp_create_nonce( 'frm_trans_ajax' ) ); | |
| 165 | + $link = '<a href="' . esc_url( $link ) . '" class="frm_trans_ajax_link" data-frmverify="' . esc_attr( $confirm ) . '">'; | |
| 166 | + $link .= esc_html__( 'Refund', 'formidable' ); | |
| 167 | + $link .= '</a>'; | |
| 131 | 168 | } |
| 132 | 169 | |
| 133 | 170 | $paysys = $payment->paysys; |
| 171 | + | |
| 134 | 172 | if ( self::should_filter_refund_link( $paysys ) ) { |
| 135 | 173 | /** |
| 136 | 174 | * Filter the refund link for a specific gateway. |
| 137 | 175 | * For example, Stripe uses frm_pay_stripe_refund_link. |
| @@ -138,9 +176,9 @@ | ||
| 138 | 176 | * |
| 139 | 177 | * @param string $link |
| 140 | 178 | * @param object $payment |
| 141 | 179 | */ |
| 142 | - $link = apply_filters( 'frm_pay_' . $paysys . '_refund_link', $link, $payment ); | |
| 180 | + return apply_filters( 'frm_pay_' . $paysys . '_refund_link', $link, $payment ); | |
| 143 | 181 | } |
| 144 | 182 | |
| 145 | 183 | return $link; |
| 146 | 184 | } |
| @@ -164,31 +202,138 @@ | ||
| 164 | 202 | FrmAppHelper::permission_check( 'frm_edit_entries' ); |
| 165 | 203 | check_ajax_referer( 'frm_trans_ajax', 'nonce' ); |
| 166 | 204 | |
| 167 | 205 | $payment_id = FrmAppHelper::get_param( 'payment_id', '', 'get', 'absint' ); |
| 206 | + | |
| 168 | 207 | if ( ! $payment_id ) { |
| 169 | 208 | wp_die( esc_html__( 'Oops! No payment was selected for refund.', 'formidable' ) ); |
| 170 | 209 | } |
| 171 | 210 | |
| 172 | - $frm_payment = new FrmTransLitePayment(); | |
| 173 | - $payment = $frm_payment->get_one( $payment_id ); | |
| 174 | - $refunded = FrmStrpLiteAppHelper::call_stripe_helper_class( 'refund_payment', $payment->receipt_id ); | |
| 211 | + $payment = ( new FrmTransLitePayment() )->get_one( $payment_id ); | |
| 175 | 212 | |
| 213 | + if ( ! $payment ) { | |
| 214 | + wp_die( esc_html__( 'Oops! That payment does not exist.', 'formidable' ) ); | |
| 215 | + } | |
| 216 | + | |
| 217 | + $refunded = false; | |
| 218 | + $reason = ''; | |
| 219 | + $debug_id = ''; | |
| 220 | + $paysys = $payment->paysys; | |
| 221 | + | |
| 222 | + switch ( $paysys ) { | |
| 223 | + case 'stripe': | |
| 224 | + $refunded = FrmStrpLiteAppHelper::call_stripe_helper_class( 'refund_payment', $payment->receipt_id ); | |
| 225 | + break; | |
| 226 | + case 'square': | |
| 227 | + $refunded = FrmSquareLiteConnectHelper::refund_payment( $payment->receipt_id ); | |
| 228 | + break; | |
| 229 | + case 'paypal': | |
| 230 | + $response = FrmPayPalLiteConnectHelper::refund_payment( $payment->receipt_id ); | |
| 231 | + | |
| 232 | + // Check for structured error response with message and debug_id | |
| 233 | + if ( is_object( $response ) && isset( $response->message ) && isset( $response->debug_id ) ) { | |
| 234 | + $refunded = false; | |
| 235 | + $reason = $response->message; | |
| 236 | + $debug_id = $response->debug_id; | |
| 237 | + } elseif ( false === $response ) { | |
| 238 | + $refunded = false; | |
| 239 | + $reason = self::get_paypal_refund_reason(); | |
| 240 | + $debug_id = FrmPayPalLiteConnectHelper::get_latest_debug_id_from_paypal_api(); | |
| 241 | + } elseif ( is_object( $response ) && isset( $response->refund_error ) ) { | |
| 242 | + // Handle mock error responses from PayPal API | |
| 243 | + $refunded = false; | |
| 244 | + $reason = $response->message ?? ''; | |
| 245 | + $debug_id = $response->debug_id ?? ''; | |
| 246 | + } else { | |
| 247 | + $refunded = true; | |
| 248 | + } | |
| 249 | + | |
| 250 | + break; | |
| 251 | + default: | |
| 252 | + $refunded = false; | |
| 253 | + break; | |
| 254 | + }//end switch | |
| 255 | + | |
| 176 | 256 | if ( $refunded ) { |
| 177 | 257 | self::change_payment_status( $payment, 'refunded' ); |
| 178 | 258 | $message = __( 'Refunded', 'formidable' ); |
| 259 | + // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found | |
| 179 | 260 | } else { |
| 180 | - $message = __( 'Failed', 'formidable' ); | |
| 261 | + // If the reason is already a complete error message, use it directly | |
| 262 | + // instead of wrapping it redundantly in "Refund Failed (...)" | |
| 263 | + if ( $reason && ! preg_match( '/^[A-Z_]+$/', $reason ) ) { | |
| 264 | + $message = $reason; | |
| 265 | + } else { | |
| 266 | + $message = __( 'Refund Failed', 'formidable' ); | |
| 267 | + | |
| 268 | + if ( $reason ) { | |
| 269 | + $message .= ' (' . $reason . ')'; | |
| 270 | + } | |
| 271 | + } | |
| 181 | 272 | } |
| 182 | 273 | |
| 183 | - wp_die( esc_html( $message ) ); | |
| 274 | + if ( $debug_id ) { | |
| 275 | + $message .= '<br><br>Debug ID: ' . esc_html( $debug_id ); | |
| 276 | + } | |
| 277 | + | |
| 278 | + wp_die( | |
| 279 | + sprintf( | |
| 280 | + '<div class="%1$s">%2$s</div>', | |
| 281 | + $refunded ? 'frm_updated_message' : 'frm_error_style', | |
| 282 | + wp_kses_post( $message ) | |
| 283 | + ) | |
| 284 | + ); | |
| 184 | 285 | } |
| 185 | 286 | |
| 186 | 287 | /** |
| 288 | + * Get a human-friendly reason from the latest PayPal refund error. | |
| 289 | + * | |
| 290 | + * Handles both uppercase issue codes (e.g. REFUND_FAILED_INSUFFICIENT_FUNDS) | |
| 291 | + * and human-friendly description strings from the PayPal API. | |
| 292 | + * Strips the {{debug_id:...}} token if present. | |
| 293 | + * | |
| 294 | + * @since 6.31 | |
| 295 | + * | |
| 296 | + * @return string | |
| 297 | + */ | |
| 298 | + private static function get_paypal_refund_reason() { | |
| 299 | + $error = FrmPayPalLiteConnectHelper::get_latest_error_from_paypal_api(); | |
| 300 | + | |
| 301 | + if ( ! $error ) { | |
| 302 | + return ''; | |
| 303 | + } | |
| 304 | + | |
| 305 | + $error = preg_replace( '/\{\{debug_id:[^}]+\}\}/', '', $error ); | |
| 306 | + $error = trim( $error ); | |
| 307 | + | |
| 308 | + if ( preg_match( '/^[A-Z_]+$/', $error ) ) { | |
| 309 | + return self::convert_uppercase_underscores_to_ucwords( $error, array( 'REFUND_FAILED_', 'REFUND_' ) ); | |
| 310 | + } | |
| 311 | + | |
| 312 | + return $error; | |
| 313 | + } | |
| 314 | + | |
| 315 | + /** | |
| 316 | + * @param string $error The uppercase underscored string to convert. | |
| 317 | + * @param array $prefixes_to_strip Prefixes to remove before converting. | |
| 318 | + * | |
| 319 | + * @return string | |
| 320 | + */ | |
| 321 | + private static function convert_uppercase_underscores_to_ucwords( $error, $prefixes_to_strip = array() ) { | |
| 322 | + if ( ! preg_match( '/^[A-Z_]+$/', $error ) ) { | |
| 323 | + return ''; | |
| 324 | + } | |
| 325 | + | |
| 326 | + $reason = str_replace( $prefixes_to_strip, '', $error ); | |
| 327 | + return ucwords( strtolower( str_replace( '_', ' ', $reason ) ) ); | |
| 328 | + } | |
| 329 | + | |
| 330 | + /** | |
| 187 | 331 | * Update the status of a payment. |
| 188 | 332 | * |
| 189 | 333 | * @param object $payment |
| 190 | 334 | * @param string $status |
| 335 | + * | |
| 191 | 336 | * @return void |
| 192 | 337 | */ |
| 193 | 338 | public static function change_payment_status( $payment, $status ) { |
| 194 | 339 | if ( $status === $payment->status ) { |
| @@ -198,6 +343,20 @@ | ||
| 198 | 343 | |
| 199 | 344 | $frm_payment = new FrmTransLitePayment(); |
| 200 | 345 | $frm_payment->update( $payment->id, array( 'status' => $status ) ); |
| 201 | 346 | FrmTransLiteActionsController::trigger_payment_status_change( compact( 'status', 'payment' ) ); |
| 347 | + } | |
| 348 | + | |
| 349 | + /** | |
| 350 | + * @since 6.31 | |
| 351 | + * | |
| 352 | + * @param array|string $expected_gateways | |
| 353 | + * @param array|string $selected_gateways | |
| 354 | + * | |
| 355 | + * @return void | |
| 356 | + */ | |
| 357 | + public static function maybe_hide_payment_setting( $expected_gateways, $selected_gateways ) { | |
| 358 | + if ( ! array_intersect( (array) $expected_gateways, (array) $selected_gateways ) ) { | |
| 359 | + echo ' frm_hidden'; | |
| 360 | + } | |
| 202 | 361 | } |
| 203 | 362 | } |