← All changes
|
stripe/controllers/FrmTransLitePaymentsController.php
+114
-6
6.28
→
trunk
View file →
| @@ -207,12 +207,20 @@ | ||
| 207 | 207 | if ( ! $payment_id ) { |
| 208 | 208 | wp_die( esc_html__( 'Oops! No payment was selected for refund.', 'formidable' ) ); |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | - $frm_payment = new FrmTransLitePayment(); | |
| 212 | - $payment = $frm_payment->get_one( $payment_id ); | |
| 211 | + $payment = ( new FrmTransLitePayment() )->get_one( $payment_id ); | |
| 213 | 212 | |
| 214 | - 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 ) { | |
| 215 | 223 | case 'stripe': |
| 216 | 224 | $refunded = FrmStrpLiteAppHelper::call_stripe_helper_class( 'refund_payment', $payment->receipt_id ); |
| 217 | 225 | break; |
| 218 | 226 | case 'square': |
| @@ -217,24 +225,110 @@ | ||
| 217 | 225 | break; |
| 218 | 226 | case 'square': |
| 219 | 227 | $refunded = FrmSquareLiteConnectHelper::refund_payment( $payment->receipt_id ); |
| 220 | 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; | |
| 221 | 251 | default: |
| 222 | 252 | $refunded = false; |
| 223 | 253 | break; |
| 224 | - } | |
| 254 | + }//end switch | |
| 225 | 255 | |
| 226 | 256 | if ( $refunded ) { |
| 227 | 257 | self::change_payment_status( $payment, 'refunded' ); |
| 228 | 258 | $message = __( 'Refunded', 'formidable' ); |
| 259 | + // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found | |
| 229 | 260 | } else { |
| 230 | - $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 | + } | |
| 231 | 272 | } |
| 232 | 273 | |
| 233 | - 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 | + ); | |
| 234 | 285 | } |
| 235 | 286 | |
| 236 | 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 | + /** | |
| 237 | 331 | * Update the status of a payment. |
| 238 | 332 | * |
| 239 | 333 | * @param object $payment |
| 240 | 334 | * @param string $status |
| @@ -249,6 +343,20 @@ | ||
| 249 | 343 | |
| 250 | 344 | $frm_payment = new FrmTransLitePayment(); |
| 251 | 345 | $frm_payment->update( $payment->id, array( 'status' => $status ) ); |
| 252 | 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 | + } | |
| 253 | 361 | } |
| 254 | 362 | } |