← All changes
|
stripe/controllers/FrmTransLitePaymentsController.php
+125
-21
6.26
→
trunk
View file →
| @@ -8,18 +8,14 @@ | ||
| 8 | 8 | /** |
| 9 | 9 | * @return void |
| 10 | 10 | */ |
| 11 | 11 | public static function menu() { |
| 12 | - if ( FrmTransLiteAppHelper::should_fallback_to_paypal() ) { | |
| 13 | - return; | |
| 14 | - } | |
| 15 | - | |
| 16 | 12 | $frm_settings = FrmAppHelper::get_settings(); |
| 17 | 13 | |
| 18 | 14 | // Remove the PayPal submenu (PayPal payments will just appear in the regular Payments page). |
| 19 | 15 | remove_action( 'admin_menu', 'FrmPaymentsController::menu', 26 ); |
| 20 | 16 | |
| 21 | - if ( in_array( FrmAppHelper::simple_get( 'action' ), array( 'edit', 'new' ), true ) && is_callable( 'FrmPaymentsController::route' ) ) { | |
| 17 | + if ( in_array( FrmAppHelper::simple_get( 'action' ), array( 'edit', 'new', 'bulk_delete' ), true ) && is_callable( 'FrmPaymentsController::route' ) ) { | |
| 22 | 18 | // Use the PayPal addon for add new and edit routing if it is active. |
| 23 | 19 | // This is required to support the "edit" link when using the Stripe Lite table view. |
| 24 | 20 | // It is also required for the "Add New" button to work on the payments table page. |
| 25 | 21 | $menu_route = 'FrmPaymentsController::route'; |
| @@ -55,12 +51,11 @@ | ||
| 55 | 51 | /** |
| 56 | 52 | * @return void |
| 57 | 53 | */ |
| 58 | 54 | public static function route() { |
| 59 | - $action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; | |
| 60 | - $action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); | |
| 61 | - $type = FrmAppHelper::get_param( 'type', '', 'get', 'sanitize_title' ); | |
| 62 | - | |
| 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' ); | |
| 63 | 58 | $class_name = $type === 'subscriptions' ? 'FrmTransLiteSubscriptionsController' : 'FrmTransLitePaymentsController'; |
| 64 | 59 | |
| 65 | 60 | if ( method_exists( $class_name, $action ) ) { |
| 66 | 61 | $class_name::$action(); |
| @@ -144,13 +139,15 @@ | ||
| 144 | 139 | echo esc_html( sprintf( __( '%d (Deleted)', 'formidable' ), $payment->item_id ) ); |
| 145 | 140 | return; |
| 146 | 141 | } |
| 147 | 142 | |
| 143 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 148 | 144 | ?> |
| 149 | 145 | <a href="?page=formidable-entries&action=show&frm_action=show&id=<?php echo absint( $payment->item_id ); ?>"> |
| 150 | 146 | <?php echo absint( $payment->item_id ); ?> |
| 151 | 147 | </a> |
| 152 | 148 | <?php |
| 149 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 153 | 150 | } |
| 154 | 151 | |
| 155 | 152 | /** |
| 156 | 153 | * Get a refund link. |
| @@ -163,13 +160,12 @@ | ||
| 163 | 160 | if ( $payment->status === 'refunded' ) { |
| 164 | 161 | $link = esc_html__( 'Refunded', 'formidable' ); |
| 165 | 162 | } else { |
| 166 | 163 | $confirm = __( 'Are you sure you want to refund that payment?', 'formidable' ); |
| 167 | - | |
| 168 | - $link = admin_url( 'admin-ajax.php?action=frm_trans_refund&payment_id=' . $payment->id . '&nonce=' . wp_create_nonce( 'frm_trans_ajax' ) ); | |
| 169 | - $link = '<a href="' . esc_url( $link ) . '" class="frm_trans_ajax_link" data-frmverify="' . esc_attr( $confirm ) . '">'; | |
| 170 | - $link .= esc_html__( 'Refund', 'formidable' ); | |
| 171 | - $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>'; | |
| 172 | 168 | } |
| 173 | 169 | |
| 174 | 170 | $paysys = $payment->paysys; |
| 175 | 171 | |
| @@ -180,9 +176,9 @@ | ||
| 180 | 176 | * |
| 181 | 177 | * @param string $link |
| 182 | 178 | * @param object $payment |
| 183 | 179 | */ |
| 184 | - $link = apply_filters( 'frm_pay_' . $paysys . '_refund_link', $link, $payment ); | |
| 180 | + return apply_filters( 'frm_pay_' . $paysys . '_refund_link', $link, $payment ); | |
| 185 | 181 | } |
| 186 | 182 | |
| 187 | 183 | return $link; |
| 188 | 184 | } |
| @@ -211,12 +207,20 @@ | ||
| 211 | 207 | if ( ! $payment_id ) { |
| 212 | 208 | wp_die( esc_html__( 'Oops! No payment was selected for refund.', 'formidable' ) ); |
| 213 | 209 | } |
| 214 | 210 | |
| 215 | - $frm_payment = new FrmTransLitePayment(); | |
| 216 | - $payment = $frm_payment->get_one( $payment_id ); | |
| 211 | + $payment = ( new FrmTransLitePayment() )->get_one( $payment_id ); | |
| 217 | 212 | |
| 218 | - switch ( $payment->paysys ) { | |
| 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 ) { | |
| 219 | 223 | case 'stripe': |
| 220 | 224 | $refunded = FrmStrpLiteAppHelper::call_stripe_helper_class( 'refund_payment', $payment->receipt_id ); |
| 221 | 225 | break; |
| 222 | 226 | case 'square': |
| @@ -221,24 +225,110 @@ | ||
| 221 | 225 | break; |
| 222 | 226 | case 'square': |
| 223 | 227 | $refunded = FrmSquareLiteConnectHelper::refund_payment( $payment->receipt_id ); |
| 224 | 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; | |
| 225 | 251 | default: |
| 226 | 252 | $refunded = false; |
| 227 | 253 | break; |
| 228 | - } | |
| 254 | + }//end switch | |
| 229 | 255 | |
| 230 | 256 | if ( $refunded ) { |
| 231 | 257 | self::change_payment_status( $payment, 'refunded' ); |
| 232 | 258 | $message = __( 'Refunded', 'formidable' ); |
| 259 | + // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found | |
| 233 | 260 | } else { |
| 234 | - $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 | + } | |
| 235 | 272 | } |
| 236 | 273 | |
| 237 | - 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 | + ); | |
| 238 | 285 | } |
| 239 | 286 | |
| 240 | 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 | + /** | |
| 241 | 331 | * Update the status of a payment. |
| 242 | 332 | * |
| 243 | 333 | * @param object $payment |
| 244 | 334 | * @param string $status |
| @@ -253,6 +343,20 @@ | ||
| 253 | 343 | |
| 254 | 344 | $frm_payment = new FrmTransLitePayment(); |
| 255 | 345 | $frm_payment->update( $payment->id, array( 'status' => $status ) ); |
| 256 | 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 | + } | |
| 257 | 361 | } |
| 258 | 362 | } |