| @@ -18,14 +18,12 @@ | ||
| 18 | 18 | * |
| 19 | 19 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 20 | 20 | * |
| 21 | 21 | * @param string $html Form HTML that gets filtered through frm_filter_final_form. |
| 22 | - * | |
| 23 | 22 | * @return string |
| 24 | 23 | */ |
| 25 | 24 | public static function maybe_show_message( $html ) { |
| 26 | 25 | $link_error = FrmAppHelper::simple_get( 'frm_link_error' ); |
| 27 | - | |
| 28 | 26 | if ( $link_error ) { |
| 29 | 27 | $message = '<div class="frm_error_style">' . self::get_message_for_stripe_link_code( $link_error ) . '</div>'; |
| 30 | 28 | self::insert_error_message( $message, $html ); |
| 31 | 29 | return $html; |
| @@ -31,15 +29,13 @@ | ||
| 31 | 29 | return $html; |
| 32 | 30 | } |
| 33 | 31 | |
| 34 | 32 | $form_id = self::check_html_for_form_id_match( $html ); |
| 35 | - | |
| 36 | 33 | if ( false === $form_id ) { |
| 37 | 34 | return $html; |
| 38 | 35 | } |
| 39 | 36 | |
| 40 | 37 | $details = FrmStrpLiteUrlParamHelper::get_details_for_form( $form_id ); |
| 41 | - | |
| 42 | 38 | if ( ! is_array( $details ) ) { |
| 43 | 39 | return $html; |
| 44 | 40 | } |
| 45 | 41 | |
| @@ -48,9 +44,10 @@ | ||
| 48 | 44 | 'entry' => $details['entry'], |
| 49 | 45 | ); |
| 50 | 46 | self::prepare_success_atts( $atts ); |
| 51 | 47 | |
| 52 | - $intent = $details['intent']; | |
| 48 | + $intent = $details['intent']; | |
| 49 | + $payment = $details['payment']; | |
| 53 | 50 | |
| 54 | 51 | if ( self::intent_has_failed_status( $intent ) ) { |
| 55 | 52 | $message = '<div class="frm_error_style">' . $intent->last_payment_error->message . '</div>'; |
| 56 | 53 | self::insert_error_message( $message, $html ); |
| @@ -57,14 +54,14 @@ | ||
| 57 | 54 | return $html; |
| 58 | 55 | } |
| 59 | 56 | |
| 60 | 57 | $intent_is_processing = 'processing' === $intent->status; |
| 61 | - | |
| 62 | 58 | if ( $intent_is_processing ) { |
| 63 | 59 | // Append an additional processing message to the end of the success message. |
| 64 | 60 | $filter = function ( $message ) { |
| 65 | 61 | $stripe_settings = FrmStrpLiteAppHelper::get_settings(); |
| 66 | - return $message . ( '<p>' . esc_html( $stripe_settings->settings->processing_message ) . '</p>' ); | |
| 62 | + $message .= '<p>' . esc_html( $stripe_settings->settings->processing_message ) . '</p>'; | |
| 63 | + return $message; | |
| 67 | 64 | }; |
| 68 | 65 | add_filter( 'frm_content', $filter ); |
| 69 | 66 | } |
| 70 | 67 | |
| @@ -69,9 +66,10 @@ | ||
| 69 | 66 | } |
| 70 | 67 | |
| 71 | 68 | ob_start(); |
| 72 | 69 | FrmFormsController::run_on_submit_actions( $atts ); |
| 73 | - $message = ob_get_clean(); | |
| 70 | + $message = ob_get_contents(); | |
| 71 | + ob_end_clean(); | |
| 74 | 72 | |
| 75 | 73 | // Clean up the filter we added above so no other success messages get altered if there are multiple forms. |
| 76 | 74 | if ( $intent_is_processing ) { |
| 77 | 75 | remove_filter( 'frm_content', $filter ); |
| @@ -81,9 +79,8 @@ | ||
| 81 | 79 | } |
| 82 | 80 | |
| 83 | 81 | /** |
| 84 | 82 | * @param int|string $form_id |
| 85 | - * | |
| 86 | 83 | * @return array|false |
| 87 | 84 | */ |
| 88 | 85 | private static function check_request_params( $form_id ) { |
| 89 | 86 | if ( ! FrmStrpLiteAppHelper::stripe_is_configured() ) { |
| @@ -90,9 +87,8 @@ | ||
| 90 | 87 | return false; |
| 91 | 88 | } |
| 92 | 89 | |
| 93 | 90 | $details = FrmStrpLiteUrlParamHelper::get_details_for_form( $form_id ); |
| 94 | - | |
| 95 | 91 | if ( ! is_array( $details ) ) { |
| 96 | 92 | return false; |
| 97 | 93 | } |
| 98 | 94 | |
| @@ -108,16 +104,14 @@ | ||
| 108 | 104 | * |
| 109 | 105 | * @since 6.5 |
| 110 | 106 | * |
| 111 | 107 | * @param string $html |
| 112 | - * | |
| 113 | 108 | * @return false|int Matching form id or false if there is no match. |
| 114 | 109 | */ |
| 115 | 110 | private static function check_html_for_form_id_match( $html ) { |
| 116 | 111 | foreach ( self::$form_ids as $form_id ) { |
| 117 | 112 | $substring = '<input type="hidden" name="form_id" value="' . $form_id . '"'; |
| 118 | - | |
| 119 | - if ( str_contains( $html, $substring ) ) { | |
| 113 | + if ( strpos( $html, $substring ) ) { | |
| 120 | 114 | return $form_id; |
| 121 | 115 | } |
| 122 | 116 | } |
| 123 | 117 | |
| @@ -130,9 +124,8 @@ | ||
| 130 | 124 | * |
| 131 | 125 | * @since 6.5, introduced in v3.0 of the Stripe add on. |
| 132 | 126 | * |
| 133 | 127 | * @param string $code |
| 134 | - * | |
| 135 | 128 | * @return string |
| 136 | 129 | */ |
| 137 | 130 | private static function get_message_for_stripe_link_code( $code ) { |
| 138 | 131 | switch ( $code ) { |
| @@ -161,9 +154,8 @@ | ||
| 161 | 154 | * |
| 162 | 155 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 163 | 156 | * |
| 164 | 157 | * @param array $atts |
| 165 | - * | |
| 166 | 158 | * @return void |
| 167 | 159 | */ |
| 168 | 160 | private static function prepare_success_atts( &$atts ) { |
| 169 | 161 | $atts['form'] = FrmForm::getOne( $atts['entry']->form_id ); |
| @@ -171,18 +163,14 @@ | ||
| 171 | 163 | $opt = 'success_action'; |
| 172 | 164 | $atts['conf_method'] = ! empty( $atts['form']->options[ $opt ] ) ? $atts['form']->options[ $opt ] : 'message'; |
| 173 | 165 | |
| 174 | 166 | $actions = FrmFormsController::get_met_on_submit_actions( $atts, 'create' ); |
| 175 | - | |
| 176 | - if ( ! $actions ) { | |
| 177 | - return; | |
| 167 | + if ( $actions ) { | |
| 168 | + $action = reset( $actions ); | |
| 169 | + if ( ! empty( $action->post_content['success_action'] ) && 'message' === $action->post_content['success_action'] ) { | |
| 170 | + $atts['conf_method'] = $action->post_content['success_action']; | |
| 171 | + } | |
| 178 | 172 | } |
| 179 | - | |
| 180 | - $action = reset( $actions ); | |
| 181 | - | |
| 182 | - if ( ! empty( $action->post_content['success_action'] ) && 'message' === $action->post_content['success_action'] ) { | |
| 183 | - $atts['conf_method'] = $action->post_content['success_action']; | |
| 184 | - } | |
| 185 | 173 | } |
| 186 | 174 | |
| 187 | 175 | /** |
| 188 | 176 | * Insert a message/error where the form styling will be applied. |
| @@ -187,18 +175,12 @@ | ||
| 187 | 175 | /** |
| 188 | 176 | * Insert a message/error where the form styling will be applied. |
| 189 | 177 | * |
| 190 | 178 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 191 | - * | |
| 192 | - * @param string $message Message. | |
| 193 | - * @param string $form Form. | |
| 194 | - * | |
| 195 | - * @return void | |
| 196 | 179 | */ |
| 197 | 180 | private static function insert_error_message( $message, &$form ) { |
| 198 | 181 | $add_after = '<fieldset>'; |
| 199 | 182 | $pos = strpos( $form, $add_after ); |
| 200 | - | |
| 201 | 183 | if ( $pos !== false ) { |
| 202 | 184 | $form = substr_replace( $form, $add_after . $message, $pos, strlen( $add_after ) ); |
| 203 | 185 | } |
| 204 | 186 | } |
| @@ -206,15 +188,13 @@ | ||
| 206 | 188 | /** |
| 207 | 189 | * Include the token if going between pages. |
| 208 | 190 | * |
| 209 | 191 | * @param object $form The form being submitted. |
| 210 | - * | |
| 211 | 192 | * @return void |
| 212 | 193 | */ |
| 213 | 194 | public static function add_hidden_token_field( $form ) { |
| 214 | 195 | $posted_form = FrmAppHelper::get_param( 'form_id', 0, 'post', 'absint' ); |
| 215 | - | |
| 216 | - if ( $posted_form !== (int) $form->id || FrmFormsController::just_created_entry( $form->id ) ) { | |
| 196 | + if ( $posted_form != $form->id || FrmFormsController::just_created_entry( $form->id ) ) { | |
| 217 | 197 | // Check to make sure the correct form was submitted. |
| 218 | 198 | // Was an entry already created and the form should be loaded fresh? |
| 219 | 199 | |
| 220 | 200 | $intents = self::maybe_create_intents( $form->id ); |
| @@ -223,10 +203,9 @@ | ||
| 223 | 203 | return; |
| 224 | 204 | } |
| 225 | 205 | |
| 226 | 206 | $intents = self::get_payment_intents( 'frmintent' . $form->id ); |
| 227 | - | |
| 228 | - if ( $intents ) { | |
| 207 | + if ( ! empty( $intents ) ) { | |
| 229 | 208 | self::update_intent_pricing( $form->id, $intents ); |
| 230 | 209 | } else { |
| 231 | 210 | $intents = self::maybe_create_intents( $form->id ); |
| 232 | 211 | } |
| @@ -240,9 +219,8 @@ | ||
| 240 | 219 | * @since 6.5, introduced in v2.02 of the Stripe add on. |
| 241 | 220 | * |
| 242 | 221 | * @param array $intents |
| 243 | 222 | * @param stdClass $form |
| 244 | - * | |
| 245 | 223 | * @return void |
| 246 | 224 | */ |
| 247 | 225 | private static function include_intents_in_form( $intents, $form ) { |
| 248 | 226 | foreach ( $intents as $intent ) { |
| @@ -263,9 +241,8 @@ | ||
| 263 | 241 | * |
| 264 | 242 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 265 | 243 | * |
| 266 | 244 | * @param string $name |
| 267 | - * | |
| 268 | 245 | * @return mixed |
| 269 | 246 | */ |
| 270 | 247 | public static function get_payment_intents( $name ) { |
| 271 | 248 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| @@ -271,11 +248,9 @@ | ||
| 271 | 248 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 272 | 249 | if ( ! isset( $_POST[ $name ] ) ) { |
| 273 | 250 | return array(); |
| 274 | 251 | } |
| 275 | - | |
| 276 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing | |
| 277 | - $intents = $_POST[ $name ]; | |
| 252 | + $intents = $_POST[ $name ]; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing | |
| 278 | 253 | FrmAppHelper::sanitize_value( 'sanitize_text_field', $intents ); |
| 279 | 254 | return $intents; |
| 280 | 255 | } |
| 281 | 256 | |
| @@ -292,11 +267,9 @@ | ||
| 292 | 267 | if ( empty( $_POST['form'] ) ) { |
| 293 | 268 | wp_die(); |
| 294 | 269 | } |
| 295 | 270 | |
| 296 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 297 | - $form = json_decode( stripslashes( $_POST['form'] ), true ); | |
| 298 | - | |
| 271 | + $form = json_decode( stripslashes( $_POST['form'] ), true ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 299 | 272 | if ( ! is_array( $form ) ) { |
| 300 | 273 | wp_die(); |
| 301 | 274 | } |
| 302 | 275 | |
| @@ -302,11 +275,11 @@ | ||
| 302 | 275 | |
| 303 | 276 | self::format_form_data( $form ); |
| 304 | 277 | |
| 305 | 278 | $form_id = absint( $form['form_id'] ); |
| 306 | - $intents = $form[ 'frmintent' . $form_id ] ?? array(); | |
| 279 | + $intents = isset( $form[ 'frmintent' . $form_id ] ) ? $form[ 'frmintent' . $form_id ] : array(); | |
| 307 | 280 | |
| 308 | - if ( ! $intents ) { | |
| 281 | + if ( empty( $intents ) ) { | |
| 309 | 282 | wp_die(); |
| 310 | 283 | } |
| 311 | 284 | |
| 312 | 285 | if ( ! is_array( $intents ) ) { |
| @@ -328,23 +301,20 @@ | ||
| 328 | 301 | /** |
| 329 | 302 | * Update pricing on page turn and non-ajax validation. |
| 330 | 303 | * |
| 331 | 304 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 332 | - * | |
| 333 | 305 | * @param int $form_id |
| 334 | 306 | * @param array $intents |
| 335 | - * | |
| 336 | 307 | * @return void |
| 337 | 308 | */ |
| 338 | 309 | private static function update_intent_pricing( $form_id, &$intents ) { |
| 339 | - // phpcs:ignore WordPress.Security.NonceVerification.Missing, Universal.Operators.StrictComparisons | |
| 310 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 340 | 311 | if ( ! isset( $_POST['form_id'] ) || absint( $_POST['form_id'] ) != $form_id ) { |
| 341 | 312 | return; |
| 342 | 313 | } |
| 343 | 314 | |
| 344 | 315 | $actions = FrmStrpLiteActionsController::get_actions_before_submit( $form_id ); |
| 345 | - | |
| 346 | - if ( ! $actions || ! $intents ) { | |
| 316 | + if ( empty( $actions ) || empty( $intents ) ) { | |
| 347 | 317 | return; |
| 348 | 318 | } |
| 349 | 319 | |
| 350 | 320 | $form = FrmForm::getOne( $form_id ); |
| @@ -359,26 +329,22 @@ | ||
| 359 | 329 | } |
| 360 | 330 | |
| 361 | 331 | foreach ( $intents as $k => $intent ) { |
| 362 | 332 | $intent_id = explode( '_secret_', $intent )[0]; |
| 363 | - $is_setup_intent = str_starts_with( $intent_id, 'seti_' ); | |
| 364 | - | |
| 333 | + $is_setup_intent = 0 === strpos( $intent_id, 'seti_' ); | |
| 365 | 334 | if ( $is_setup_intent ) { |
| 366 | 335 | continue; |
| 367 | 336 | } |
| 368 | 337 | |
| 369 | 338 | $saved = FrmStrpLiteAppHelper::call_stripe_helper_class( 'get_intent', $intent_id ); |
| 370 | - | |
| 371 | 339 | if ( empty( $saved->metadata->action ) ) { |
| 372 | 340 | continue; |
| 373 | 341 | } |
| 374 | 342 | |
| 375 | 343 | foreach ( $actions as $action ) { |
| 376 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 377 | 344 | if ( $saved->metadata->action != $action->ID ) { |
| 378 | 345 | continue; |
| 379 | 346 | } |
| 380 | - | |
| 381 | 347 | $intents[ $k ] = array( |
| 382 | 348 | 'id' => $intent, |
| 383 | 349 | 'action' => $action->ID, |
| 384 | 350 | ); |
| @@ -383,10 +349,9 @@ | ||
| 383 | 349 | 'action' => $action->ID, |
| 384 | 350 | ); |
| 385 | 351 | |
| 386 | 352 | $amount = $action->post_content['amount']; |
| 387 | - | |
| 388 | - if ( ! str_contains( $amount, '[' ) ) { | |
| 353 | + if ( strpos( $amount, '[' ) === false ) { | |
| 389 | 354 | // The amount is static, so it doesn't need an update. |
| 390 | 355 | continue; |
| 391 | 356 | } |
| 392 | 357 | |
| @@ -392,10 +357,8 @@ | ||
| 392 | 357 | |
| 393 | 358 | // Update amount based on field shortcodes. |
| 394 | 359 | $entry = self::generate_false_entry(); |
| 395 | 360 | $amount = FrmStrpLiteActionsController::prepare_amount( $amount, compact( 'form', 'entry', 'action' ) ); |
| 396 | - | |
| 397 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 398 | 361 | if ( $saved->amount == $amount || $amount == '000' ) { |
| 399 | 362 | continue; |
| 400 | 363 | } |
| 401 | 364 | |
| @@ -407,17 +370,15 @@ | ||
| 407 | 370 | /** |
| 408 | 371 | * Create an entry object with posted values. |
| 409 | 372 | * |
| 410 | 373 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 411 | - * | |
| 412 | 374 | * @return stdClass |
| 413 | 375 | */ |
| 414 | 376 | private static function generate_false_entry() { |
| 415 | - $entry = new stdClass(); | |
| 416 | - $entry->post_id = 0; | |
| 417 | - $entry->id = 0; | |
| 418 | - $entry->item_key = ''; | |
| 419 | - $entry->metas = array(); | |
| 377 | + $entry = new stdClass(); | |
| 378 | + $entry->post_id = 0; | |
| 379 | + $entry->id = 0; | |
| 380 | + $entry->metas = array(); | |
| 420 | 381 | |
| 421 | 382 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 422 | 383 | foreach ( $_POST as $k => $v ) { |
| 423 | 384 | $k = sanitize_text_field( stripslashes( $k ) ); |
| @@ -422,18 +383,17 @@ | ||
| 422 | 383 | foreach ( $_POST as $k => $v ) { |
| 423 | 384 | $k = sanitize_text_field( stripslashes( $k ) ); |
| 424 | 385 | $v = wp_unslash( $v ); |
| 425 | 386 | |
| 426 | - if ( $k !== 'item_meta' ) { | |
| 387 | + if ( $k === 'item_meta' ) { | |
| 388 | + foreach ( $v as $f => $value ) { | |
| 389 | + FrmAppHelper::sanitize_value( 'wp_kses_post', $value ); | |
| 390 | + $entry->metas[ absint( $f ) ] = $value; | |
| 391 | + } | |
| 392 | + } else { | |
| 427 | 393 | FrmAppHelper::sanitize_value( 'wp_kses_post', $v ); |
| 428 | 394 | $entry->{$k} = $v; |
| 429 | - continue; | |
| 430 | 395 | } |
| 431 | - | |
| 432 | - foreach ( $v as $f => $value ) { | |
| 433 | - FrmAppHelper::sanitize_value( 'wp_kses_post', $value ); | |
| 434 | - $entry->metas[ absint( $f ) ] = $value; | |
| 435 | - } | |
| 436 | 396 | } |
| 437 | 397 | |
| 438 | 398 | return $entry; |
| 439 | 399 | } |
| @@ -443,9 +403,8 @@ | ||
| 443 | 403 | * |
| 444 | 404 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 445 | 405 | * |
| 446 | 406 | * @param array $form |
| 447 | - * | |
| 448 | 407 | * @return void |
| 449 | 408 | */ |
| 450 | 409 | private static function format_form_data( &$form ) { |
| 451 | 410 | $formatted = array(); |
| @@ -451,19 +410,17 @@ | ||
| 451 | 410 | $formatted = array(); |
| 452 | 411 | |
| 453 | 412 | foreach ( $form as $input ) { |
| 454 | 413 | $key = $input['name']; |
| 455 | - | |
| 456 | - if ( ! isset( $formatted[ $key ] ) ) { | |
| 414 | + if ( isset( $formatted[ $key ] ) ) { | |
| 415 | + if ( is_array( $formatted[ $key ] ) ) { | |
| 416 | + $formatted[ $key ][] = $input['value']; | |
| 417 | + } else { | |
| 418 | + $formatted[ $key ] = array( $formatted[ $key ], $input['value'] ); | |
| 419 | + } | |
| 420 | + } else { | |
| 457 | 421 | $formatted[ $key ] = $input['value']; |
| 458 | - continue; | |
| 459 | 422 | } |
| 460 | - | |
| 461 | - if ( is_array( $formatted[ $key ] ) ) { | |
| 462 | - $formatted[ $key ][] = $input['value']; | |
| 463 | - } else { | |
| 464 | - $formatted[ $key ] = array( $formatted[ $key ], $input['value'] ); | |
| 465 | - } | |
| 466 | 423 | } |
| 467 | 424 | |
| 468 | 425 | parse_str( http_build_query( $formatted ), $form ); |
| 469 | 426 | } |
| @@ -474,15 +431,14 @@ | ||
| 474 | 431 | * |
| 475 | 432 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 476 | 433 | * |
| 477 | 434 | * @param int|string $form_id |
| 478 | - * | |
| 479 | 435 | * @return array |
| 480 | 436 | */ |
| 481 | 437 | private static function maybe_create_intents( $form_id ) { |
| 482 | 438 | $intents = array(); |
| 439 | + | |
| 483 | 440 | $details = self::check_request_params( $form_id ); |
| 484 | - | |
| 485 | 441 | if ( is_array( $details ) ) { |
| 486 | 442 | $payment = $details['payment']; |
| 487 | 443 | $intent = $details['intent']; |
| 488 | 444 | $payment_failed = self::payment_failed( $payment, $intent ); |
| @@ -511,9 +467,8 @@ | ||
| 511 | 467 | continue; |
| 512 | 468 | } |
| 513 | 469 | |
| 514 | 470 | $intent = self::create_intent( $action ); |
| 515 | - | |
| 516 | 471 | if ( ! is_object( $intent ) ) { |
| 517 | 472 | // A non-object is a string error message. |
| 518 | 473 | // The error gets logged to results.log so we can just skip it. |
| 519 | 474 | // Reasons it could fail is because a payment method type was specified that will not work. |
| @@ -538,9 +493,8 @@ | ||
| 538 | 493 | * |
| 539 | 494 | * @since 3.0 This code was moved out of self::maybe_create_intents into a new function. |
| 540 | 495 | * |
| 541 | 496 | * @param WP_Post $action |
| 542 | - * | |
| 543 | 497 | * @return mixed |
| 544 | 498 | */ |
| 545 | 499 | private static function create_intent( $action ) { |
| 546 | 500 | $amount = $action->post_content['amount']; |
| @@ -545,9 +499,8 @@ | ||
| 545 | 499 | private static function create_intent( $action ) { |
| 546 | 500 | $amount = $action->post_content['amount']; |
| 547 | 501 | $currency = $action->post_content['currency']; |
| 548 | 502 | |
| 549 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 550 | 503 | if ( $amount == '000' ) { |
| 551 | 504 | // Create the intent when the form loads. |
| 552 | 505 | $amount = in_array( strtolower( $currency ), array( 'aud', 'cad', 'eur', 'gbp', 'usd' ), true ) ? 100 : 1000; |
| 553 | 506 | } |
| @@ -578,14 +531,12 @@ | ||
| 578 | 531 | /** |
| 579 | 532 | * Add the statement descriptor to the intent data, if it is valid. |
| 580 | 533 | * |
| 581 | 534 | * @param array $intent_data |
| 582 | - * | |
| 583 | 535 | * @return array |
| 584 | 536 | */ |
| 585 | 537 | private static function maybe_add_statement_descriptor( $intent_data ) { |
| 586 | 538 | $statement_descriptor = self::get_statement_descriptor(); |
| 587 | - | |
| 588 | 539 | if ( false !== $statement_descriptor ) { |
| 589 | 540 | $intent_data['statement_descriptor'] = $statement_descriptor; |
| 590 | 541 | } |
| 591 | 542 | |
| @@ -617,9 +568,13 @@ | ||
| 617 | 568 | } |
| 618 | 569 | |
| 619 | 570 | $name = self::strip_special_characters_from_statement_descriptor( $name ); |
| 620 | 571 | |
| 621 | - return self::statement_descriptor_is_valid( $name ) ? $name : false; | |
| 572 | + if ( ! self::statement_descriptor_is_valid( $name ) ) { | |
| 573 | + return false; | |
| 574 | + } | |
| 575 | + | |
| 576 | + return $name; | |
| 622 | 577 | } |
| 623 | 578 | |
| 624 | 579 | /** |
| 625 | 580 | * Remove the special characters that Stripe doesn't allow in statement descriptors, in case any exist. |
| @@ -626,9 +581,8 @@ | ||
| 626 | 581 | * |
| 627 | 582 | * @since 6.23 |
| 628 | 583 | * |
| 629 | 584 | * @param string $name The name of the site. |
| 630 | - * | |
| 631 | 585 | * @return string The name with special characters removed. |
| 632 | 586 | */ |
| 633 | 587 | private static function strip_special_characters_from_statement_descriptor( $name ) { |
| 634 | 588 | $special_characters = array( |
| @@ -648,9 +602,8 @@ | ||
| 648 | 602 | * |
| 649 | 603 | * @since 6.23 |
| 650 | 604 | * |
| 651 | 605 | * @param string $name Passed by reference, as this is updated if it is too long. |
| 652 | - * | |
| 653 | 606 | * @return bool |
| 654 | 607 | */ |
| 655 | 608 | private static function statement_descriptor_is_valid( &$name ) { |
| 656 | 609 | if ( strlen( $name ) < 5 ) { |
| @@ -660,9 +613,13 @@ | ||
| 660 | 613 | if ( strlen( $name ) > 22 ) { |
| 661 | 614 | $name = substr( $name, 0, 22 ); |
| 662 | 615 | } |
| 663 | 616 | |
| 664 | - return (bool) preg_match( '/^[a-zA-Z0-9\s\p{P}]+$/', $name ); | |
| 617 | + if ( ! preg_match( '/^[a-zA-Z0-9\s\p{P}]+$/', $name ) ) { | |
| 618 | + return false; | |
| 619 | + } | |
| 620 | + | |
| 621 | + return true; | |
| 665 | 622 | } |
| 666 | 623 | |
| 667 | 624 | /** |
| 668 | 625 | * Create a customer and an associated setup intent for a recurring Stripe link payment. |
| @@ -669,9 +626,8 @@ | ||
| 669 | 626 | * |
| 670 | 627 | * @since 6.5, introduced in v3.0 of the Stripe add on. |
| 671 | 628 | * |
| 672 | 629 | * @param array $payment_method_types |
| 673 | - * | |
| 674 | 630 | * @return false|object |
| 675 | 631 | */ |
| 676 | 632 | private static function create_setup_intent( $payment_method_types ) { |
| 677 | 633 | $payment_info = array( |
| @@ -679,9 +635,8 @@ | ||
| 679 | 635 | ); |
| 680 | 636 | |
| 681 | 637 | // We need to add a customer to support subscriptions with link. |
| 682 | 638 | $customer = FrmStrpLiteAppHelper::call_stripe_helper_class( 'get_customer', $payment_info ); |
| 683 | - | |
| 684 | 639 | if ( ! is_object( $customer ) ) { |
| 685 | 640 | return false; |
| 686 | 641 | } |
| 687 | 642 | |
| @@ -692,16 +647,14 @@ | ||
| 692 | 647 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 693 | 648 | * |
| 694 | 649 | * @param int|string $form_id |
| 695 | 650 | * @param array $actions |
| 696 | - * | |
| 697 | 651 | * @return void |
| 698 | 652 | */ |
| 699 | 653 | private static function add_amount_to_actions( $form_id, &$actions ) { |
| 700 | - if ( ! $actions ) { | |
| 654 | + if ( empty( $actions ) ) { | |
| 701 | 655 | return; |
| 702 | 656 | } |
| 703 | - | |
| 704 | 657 | $form = FrmForm::getOne( $form_id ); |
| 705 | 658 | |
| 706 | 659 | foreach ( $actions as $k => $action ) { |
| 707 | 660 | $amount = self::get_amount_before_submit( compact( 'action', 'form' ) ); |
| @@ -712,12 +665,12 @@ | ||
| 712 | 665 | /** |
| 713 | 666 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 714 | 667 | * |
| 715 | 668 | * @param array $atts |
| 716 | - * | |
| 717 | 669 | * @return string |
| 718 | 670 | */ |
| 719 | 671 | private static function get_amount_before_submit( $atts ) { |
| 672 | + $amount = $atts['action']->post_content['amount']; | |
| 720 | 673 | return FrmStrpLiteActionsController::prepare_amount( $atts['action']->post_content['amount'], $atts ); |
| 721 | 674 | } |
| 722 | 675 | |
| 723 | 676 | /** |
| @@ -727,9 +680,8 @@ | ||
| 727 | 680 | * |
| 728 | 681 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 729 | 682 | * |
| 730 | 683 | * @param array $atts |
| 731 | - * | |
| 732 | 684 | * @return string |
| 733 | 685 | */ |
| 734 | 686 | public static function return_url( $atts ) { |
| 735 | 687 | $atts = array( |
| @@ -736,9 +688,15 @@ | ||
| 736 | 688 | 'entry' => $atts['entry'], |
| 737 | 689 | ); |
| 738 | 690 | self::prepare_success_atts( $atts ); |
| 739 | 691 | |
| 740 | - return $atts['conf_method'] === 'redirect' ? self::get_redirect_url( $atts ) : self::get_message_url( $atts ); | |
| 692 | + if ( $atts['conf_method'] === 'redirect' ) { | |
| 693 | + $redirect = self::get_redirect_url( $atts ); | |
| 694 | + } else { | |
| 695 | + $redirect = self::get_message_url( $atts ); | |
| 696 | + } | |
| 697 | + | |
| 698 | + return $redirect; | |
| 741 | 699 | } |
| 742 | 700 | |
| 743 | 701 | /** |
| 744 | 702 | * If the form should redirect, get the url to redirect to. |
| @@ -750,14 +708,12 @@ | ||
| 750 | 708 | * |
| 751 | 709 | * @type stdClass $form |
| 752 | 710 | * @type stdClass $entry |
| 753 | 711 | * } |
| 754 | - * | |
| 755 | 712 | * @return string |
| 756 | 713 | */ |
| 757 | 714 | private static function get_redirect_url( $atts ) { |
| 758 | 715 | $actions = FrmFormsController::get_met_on_submit_actions( $atts ); |
| 759 | - | |
| 760 | 716 | if ( $actions ) { |
| 761 | 717 | $success_url = reset( $actions )->post_content['success_url']; |
| 762 | 718 | } |
| 763 | 719 | |
| @@ -779,18 +735,14 @@ | ||
| 779 | 735 | * |
| 780 | 736 | * @since 6.5, introduced in v2.0 of the Stripe add on. |
| 781 | 737 | * |
| 782 | 738 | * @param array $atts |
| 783 | - * | |
| 784 | - * @return string | |
| 785 | 739 | */ |
| 786 | 740 | private static function get_message_url( $atts ) { |
| 787 | 741 | $url = self::get_referer_url( $atts['entry_id'], false ); |
| 788 | - | |
| 789 | 742 | if ( false === $url ) { |
| 790 | 743 | $url = FrmAppHelper::get_server_value( 'HTTP_REFERER' ); |
| 791 | 744 | } |
| 792 | - | |
| 793 | 745 | return add_query_arg( array( 'frmstrp' => $atts['entry_id'] ), $url ); |
| 794 | 746 | } |
| 795 | 747 | |
| 796 | 748 | /** |
| @@ -797,9 +749,8 @@ | ||
| 797 | 749 | * @since 6.5 |
| 798 | 750 | * |
| 799 | 751 | * @param int|string $entry_id |
| 800 | 752 | * @param bool $delete_meta |
| 801 | - * | |
| 802 | 753 | * @return false|string |
| 803 | 754 | */ |
| 804 | 755 | public static function get_referer_url( $entry_id, $delete_meta = true ) { |
| 805 | 756 | $row = FrmDb::get_row( |
| @@ -810,9 +761,8 @@ | ||
| 810 | 761 | 'meta_value LIKE' => '{"referer":', |
| 811 | 762 | ), |
| 812 | 763 | 'id, meta_value' |
| 813 | 764 | ); |
| 814 | - | |
| 815 | 765 | if ( ! $row ) { |
| 816 | 766 | return false; |
| 817 | 767 | } |
| 818 | 768 | |
| @@ -822,12 +772,9 @@ | ||
| 822 | 772 | if ( ! is_array( $meta ) || empty( $meta['referer'] ) ) { |
| 823 | 773 | return false; |
| 824 | 774 | } |
| 825 | 775 | |
| 826 | - if ( $delete_meta ) { | |
| 827 | - self::delete_temporary_referer_meta( (int) $row->id ); | |
| 828 | - } | |
| 829 | - | |
| 776 | + self::delete_temporary_referer_meta( (int) $row->id ); | |
| 830 | 777 | return $meta['referer']; |
| 831 | 778 | } |
| 832 | 779 | |
| 833 | 780 | /** |
| @@ -833,9 +780,8 @@ | ||
| 833 | 780 | /** |
| 834 | 781 | * Delete the referer meta as we'll no longer need it. |
| 835 | 782 | * |
| 836 | 783 | * @param int $row_id |
| 837 | - * | |
| 838 | 784 | * @return void |
| 839 | 785 | */ |
| 840 | 786 | private static function delete_temporary_referer_meta( $row_id ) { |
| 841 | 787 | global $wpdb; |
| @@ -847,9 +793,8 @@ | ||
| 847 | 793 | * |
| 848 | 794 | * @since 6.5.1 |
| 849 | 795 | * |
| 850 | 796 | * @param object $intent |
| 851 | - * | |
| 852 | 797 | * @return bool |
| 853 | 798 | */ |
| 854 | 799 | private static function intent_has_failed_status( $intent ) { |
| 855 | 800 | return in_array( $intent->status, array( 'requires_source', 'requires_payment_method', 'canceled' ), true ); |
| @@ -861,9 +806,8 @@ | ||
| 861 | 806 | * @since 6.8 |
| 862 | 807 | * |
| 863 | 808 | * @param object $payment |
| 864 | 809 | * @param object $intent |
| 865 | - * | |
| 866 | 810 | * @return bool |
| 867 | 811 | */ |
| 868 | 812 | public static function payment_failed( $payment, $intent ) { |
| 869 | 813 | if ( self::intent_has_failed_status( $intent ) ) { |
| @@ -868,8 +812,13 @@ | ||
| 868 | 812 | public static function payment_failed( $payment, $intent ) { |
| 869 | 813 | if ( self::intent_has_failed_status( $intent ) ) { |
| 870 | 814 | return true; |
| 871 | 815 | } |
| 816 | + | |
| 872 | 817 | // The $intent will be "succeeded" with a failed payment when testing with the 4000000000000341 credit card. |
| 873 | - return 'payment_failed' === FrmAppHelper::simple_get( 'frm_link_error' ) && 'failed' === $payment->status; | |
| 818 | + if ( 'payment_failed' === FrmAppHelper::simple_get( 'frm_link_error' ) && 'failed' === $payment->status ) { | |
| 819 | + return true; | |
| 820 | + } | |
| 821 | + | |
| 822 | + return false; | |
| 874 | 823 | } |
| 875 | 824 | } |