PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | stripe/controllers/FrmTransLitePaymentsController.php +134 -21 6.25 → 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,13 +51,13 @@
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' );
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';
62 59
63 - $class_name = $type === 'subscriptions' ? 'FrmTransLiteSubscriptionsController' : 'FrmTransLitePaymentsController';
64 60 if ( method_exists( $class_name, $action ) ) {
65 61 $class_name::$action();
66 62 return;
67 63 }
@@ -70,8 +66,9 @@
70 66 }
71 67
72 68 /**
73 69 * @param object $payment
70 + *
74 71 * @return void
75 72 */
76 73 public static function load_sidebar_actions( $payment ) {
77 74 FrmTransLiteActionsController::actions_js();
@@ -84,8 +81,9 @@
84 81 /**
85 82 * Echo a receipt link.
86 83 *
87 84 * @param object $payment
85 + *
88 86 * @return void
89 87 */
90 88 public static function show_receipt_link( $payment ) {
91 89 $link = esc_html( $payment->receipt_id );
@@ -117,8 +115,9 @@
117 115 /**
118 116 * Echo a refund link.
119 117 *
120 118 * @param object $payment
119 + *
121 120 * @return void
122 121 */
123 122 public static function show_refund_link( $payment ) {
124 123 $link = self::refund_link( $payment );
@@ -128,8 +127,9 @@
128 127 /**
129 128 * Show a link to a payment entry (unless it is deleted).
130 129 *
131 130 * @param object $payment
131 + *
132 132 * @return void
133 133 */
134 134 public static function show_entry_link( $payment ) {
135 135 $entry = FrmDb::get_col( 'frm_items', array( 'id' => $payment->item_id ) );
@@ -139,13 +139,15 @@
139 139 echo esc_html( sprintf( __( '%d (Deleted)', 'formidable' ), $payment->item_id ) );
140 140 return;
141 141 }
142 142
143 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
143 144 ?>
144 145 <a href="?page=formidable-entries&amp;action=show&amp;frm_action=show&amp;id=<?php echo absint( $payment->item_id ); ?>">
145 146 <?php echo absint( $payment->item_id ); ?>
146 147 </a>
147 148 <?php
149 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
148 150 }
149 151
150 152 /**
151 153 * Get a refund link.
@@ -150,8 +152,9 @@
150 152 /**
151 153 * Get a refund link.
152 154 *
153 155 * @param object $payment
156 + *
154 157 * @return string
155 158 */
156 159 public static function refund_link( $payment ) {
157 160 if ( $payment->status === 'refunded' ) {
@@ -157,16 +160,16 @@
157 160 if ( $payment->status === 'refunded' ) {
158 161 $link = esc_html__( 'Refunded', 'formidable' );
159 162 } else {
160 163 $confirm = __( 'Are you sure you want to refund that payment?', 'formidable' );
161 -
162 - $link = admin_url( 'admin-ajax.php?action=frm_trans_refund&payment_id=' . $payment->id . '&nonce=' . wp_create_nonce( 'frm_trans_ajax' ) );
163 - $link = '<a href="' . esc_url( $link ) . '" class="frm_trans_ajax_link" data-frmverify="' . esc_attr( $confirm ) . '">';
164 - $link .= esc_html__( 'Refund', 'formidable' );
165 - $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>';
166 168 }
167 169
168 170 $paysys = $payment->paysys;
171 +
169 172 if ( self::should_filter_refund_link( $paysys ) ) {
170 173 /**
171 174 * Filter the refund link for a specific gateway.
172 175 * For example, Stripe uses frm_pay_stripe_refund_link.
@@ -173,9 +176,9 @@
173 176 *
174 177 * @param string $link
175 178 * @param object $payment
176 179 */
177 - $link = apply_filters( 'frm_pay_' . $paysys . '_refund_link', $link, $payment );
180 + return apply_filters( 'frm_pay_' . $paysys . '_refund_link', $link, $payment );
178 181 }
179 182
180 183 return $link;
181 184 }
@@ -199,16 +202,25 @@
199 202 FrmAppHelper::permission_check( 'frm_edit_entries' );
200 203 check_ajax_referer( 'frm_trans_ajax', 'nonce' );
201 204
202 205 $payment_id = FrmAppHelper::get_param( 'payment_id', '', 'get', 'absint' );
206 +
203 207 if ( ! $payment_id ) {
204 208 wp_die( esc_html__( 'Oops! No payment was selected for refund.', 'formidable' ) );
205 209 }
206 210
207 - $frm_payment = new FrmTransLitePayment();
208 - $payment = $frm_payment->get_one( $payment_id );
211 + $payment = ( new FrmTransLitePayment() )->get_one( $payment_id );
209 212
210 - 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 ) {
211 223 case 'stripe':
212 224 $refunded = FrmStrpLiteAppHelper::call_stripe_helper_class( 'refund_payment', $payment->receipt_id );
213 225 break;
214 226 case 'square':
@@ -213,28 +225,115 @@
213 225 break;
214 226 case 'square':
215 227 $refunded = FrmSquareLiteConnectHelper::refund_payment( $payment->receipt_id );
216 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;
217 251 default:
218 252 $refunded = false;
219 253 break;
220 - }
254 + }//end switch
221 255
222 256 if ( $refunded ) {
223 257 self::change_payment_status( $payment, 'refunded' );
224 258 $message = __( 'Refunded', 'formidable' );
259 + // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found
225 260 } else {
226 - $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 + }
227 272 }
228 273
229 - 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 + );
230 285 }
231 286
232 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 + /**
233 331 * Update the status of a payment.
234 332 *
235 333 * @param object $payment
236 334 * @param string $status
335 + *
237 336 * @return void
238 337 */
239 338 public static function change_payment_status( $payment, $status ) {
240 339 if ( $status === $payment->status ) {
@@ -244,6 +343,20 @@
244 343
245 344 $frm_payment = new FrmTransLitePayment();
246 345 $frm_payment->update( $payment->id, array( 'status' => $status ) );
247 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 + }
248 361 }
249 362 }