← All changes
|
stripe/controllers/FrmTransLiteActionsController.php
+86
-44
6.26
→
trunk
View file →
| @@ -105,9 +105,9 @@ | ||
| 105 | 105 | $class_name = 'Frm' . $class_name . 'ActionsController'; |
| 106 | 106 | $response = $class_name::trigger_gateway( $action, $entry, $form ); |
| 107 | 107 | |
| 108 | 108 | if ( ! $response['success'] && $response['show_errors'] ) { |
| 109 | - // the payment failed | |
| 109 | + // The payment failed | |
| 110 | 110 | self::show_failed_message( compact( 'action', 'entry', 'form', 'response' ) ); |
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | |
| @@ -158,17 +158,15 @@ | ||
| 158 | 158 | public static function replace_success_message() { |
| 159 | 159 | global $frm_vars; |
| 160 | 160 | $message = $frm_vars['frm_trans']['error'] ?? ''; |
| 161 | 161 | |
| 162 | - if ( empty( $message ) ) { | |
| 162 | + if ( ! $message ) { | |
| 163 | 163 | $message = __( 'There was an error processing your payment.', 'formidable' ); |
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - $message = '<div class="frm_error_style">' . $message . '</div>'; | |
| 166 | + return '<div class="frm_error_style">' . $message . '</div>'; | |
| 167 | + } | |
| 167 | 168 | |
| 168 | - return $message; | |
| 169 | - } | |
| 170 | - | |
| 171 | 169 | /** |
| 172 | 170 | * @param WP_Post $action |
| 173 | 171 | * @param stdClass $entry |
| 174 | 172 | * @param mixed $form |
| @@ -263,21 +261,16 @@ | ||
| 263 | 261 | // 3D Secure has a delayed payment status, so avoid sending a payment failed email for a pending payment. |
| 264 | 262 | return; |
| 265 | 263 | } |
| 266 | 264 | |
| 267 | - $entry = FrmEntry::getOne( $payment->item_id ); | |
| 268 | - | |
| 269 | - if ( isset( $atts['trigger'] ) ) { | |
| 270 | - $trigger_event = 'payment-' . $atts['trigger']; | |
| 271 | - } else { | |
| 272 | - $trigger_event = 'payment-' . $payment->status; | |
| 273 | - } | |
| 274 | - | |
| 265 | + $entry = FrmEntry::getOne( $payment->item_id ); | |
| 266 | + $trigger_event = isset( $atts['trigger'] ) ? 'payment-' . $atts['trigger'] : 'payment-' . $payment->status; | |
| 275 | 267 | $allowed_triggers = array_keys( self::add_payment_trigger( array() ) ); |
| 276 | 268 | |
| 277 | 269 | if ( ! in_array( $trigger_event, $allowed_triggers, true ) ) { |
| 278 | 270 | $trigger_event = $payment->status === 'complete' ? 'payment-success' : 'payment-failed'; |
| 279 | 271 | } |
| 272 | + | |
| 280 | 273 | FrmFormActionsController::trigger_actions( $trigger_event, $entry->form_id, $entry->id ); |
| 281 | 274 | } |
| 282 | 275 | |
| 283 | 276 | /** |
| @@ -290,13 +283,15 @@ | ||
| 290 | 283 | */ |
| 291 | 284 | public static function prepare_description( &$action, $atts ) { |
| 292 | 285 | $description = $action->post_content['description']; |
| 293 | 286 | |
| 294 | - if ( ! empty( $description ) ) { | |
| 295 | - $atts['value'] = $description; | |
| 296 | - $description = FrmTransLiteAppHelper::process_shortcodes( $atts ); | |
| 297 | - $action->post_content['description'] = $description; | |
| 287 | + if ( ! $description ) { | |
| 288 | + return; | |
| 298 | 289 | } |
| 290 | + | |
| 291 | + $atts['value'] = $description; | |
| 292 | + $description = FrmTransLiteAppHelper::process_shortcodes( $atts ); | |
| 293 | + $action->post_content['description'] = $description; | |
| 299 | 294 | } |
| 300 | 295 | |
| 301 | 296 | /** |
| 302 | 297 | * Convert the amount into 10.00. |
| @@ -311,17 +306,16 @@ | ||
| 311 | 306 | $atts['value'] = $amount; |
| 312 | 307 | $amount = FrmTransLiteAppHelper::process_shortcodes( $atts ); |
| 313 | 308 | } |
| 314 | 309 | |
| 315 | - if ( is_string( $amount ) && strlen( $amount ) >= 2 && $amount[0] === '[' && substr( $amount, -1 ) === ']' ) { | |
| 310 | + if ( is_string( $amount ) && strlen( $amount ) >= 2 && $amount[0] === '[' && str_ends_with( $amount, ']' ) ) { | |
| 316 | 311 | // Make sure we don't use a field id as the amount. |
| 317 | 312 | $amount = 0; |
| 318 | 313 | } |
| 319 | 314 | |
| 320 | 315 | $currency = self::get_currency_for_action( $atts ); |
| 316 | + $total = 0; | |
| 321 | 317 | |
| 322 | - $total = 0; | |
| 323 | - | |
| 324 | 318 | foreach ( (array) $amount as $a ) { |
| 325 | 319 | $this_amount = self::get_amount_from_string( $a ); |
| 326 | 320 | self::maybe_use_decimal( $this_amount, $currency ); |
| 327 | 321 | self::normalize_number( $this_amount, $currency ); |
| @@ -360,10 +354,9 @@ | ||
| 360 | 354 | private static function get_amount_from_string( $amount ) { |
| 361 | 355 | $amount = html_entity_decode( $amount ); |
| 362 | 356 | $amount = trim( $amount ); |
| 363 | 357 | preg_match_all( '/[0-9,.]*\.?\,?[0-9]+/', $amount, $matches ); |
| 364 | - $amount = $matches ? end( $matches[0] ) : 0; | |
| 365 | - return $amount; | |
| 358 | + return $matches ? end( $matches[0] ) : 0; | |
| 366 | 359 | } |
| 367 | 360 | |
| 368 | 361 | /** |
| 369 | 362 | * @param string $amount |
| @@ -417,8 +410,9 @@ | ||
| 417 | 410 | $settings_for_action = array( |
| 418 | 411 | 'id' => $payment_action->ID, |
| 419 | 412 | 'first_name' => $payment_action->post_content['billing_first_name'], |
| 420 | 413 | 'last_name' => $payment_action->post_content['billing_last_name'], |
| 414 | + 'address' => $payment_action->post_content['billing_address'] ?? '', | |
| 421 | 415 | 'gateways' => $payment_action->post_content['gateway'], |
| 422 | 416 | 'fields' => self::get_fields_for_price( $payment_action ), |
| 423 | 417 | 'one' => $payment_action->post_content['type'], |
| 424 | 418 | 'email' => $payment_action->post_content['email'], |
| @@ -463,12 +457,9 @@ | ||
| 463 | 457 | 'post_status' => 'publish', |
| 464 | 458 | ); |
| 465 | 459 | $payment_actions = FrmFormAction::get_action_for_form( $form_id, 'payment', $action_status ); |
| 466 | 460 | |
| 467 | - if ( ! $payment_actions ) { | |
| 468 | - $payment_actions = array(); | |
| 469 | - } | |
| 470 | - return $payment_actions; | |
| 461 | + return $payment_actions ? $payment_actions : array(); | |
| 471 | 462 | } |
| 472 | 463 | |
| 473 | 464 | /** |
| 474 | 465 | * Make sure a gateway field is hidden on the front end. |
| @@ -482,8 +473,14 @@ | ||
| 482 | 473 | if ( $field->type !== 'gateway' ) { |
| 483 | 474 | return $values; |
| 484 | 475 | } |
| 485 | 476 | |
| 477 | + if ( FrmAppHelper::is_form_builder_page() ) { | |
| 478 | + // The hooks this uses can get called in the form builder and settings pages. | |
| 479 | + // But we do not need the script in this case. | |
| 480 | + return $values; | |
| 481 | + } | |
| 482 | + | |
| 486 | 483 | // This is also called from the frm_enqueue_form_scripts hook. |
| 487 | 484 | // With this here, the value of frm_stripe_vars.settings[0].fields is -1 |
| 488 | 485 | // This is because the amount value is processed and a shortcode is not found in '000'. |
| 489 | 486 | FrmStrpLiteActionsController::load_scripts( (int) $field->form_id ); |
| @@ -506,9 +503,10 @@ | ||
| 506 | 503 | public static function fill_entry_from_previous( $values, $field ) { |
| 507 | 504 | global $frm_vars; |
| 508 | 505 | $previous_entry = $frm_vars['frm_trans']['pay_entry'] ?? false; |
| 509 | 506 | |
| 510 | - if ( empty( $previous_entry ) || $previous_entry->form_id != $field->form_id ) { | |
| 507 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 508 | + if ( ! $previous_entry || $previous_entry->form_id != $field->form_id ) { | |
| 511 | 509 | return $values; |
| 512 | 510 | } |
| 513 | 511 | |
| 514 | 512 | if ( is_array( $previous_entry->metas ) && isset( $previous_entry->metas[ $field->id ] ) ) { |
| @@ -515,10 +513,9 @@ | ||
| 515 | 513 | $values['value'] = $previous_entry->metas[ $field->id ]; |
| 516 | 514 | } |
| 517 | 515 | |
| 518 | 516 | $frm_vars['trans_filled'] = true; |
| 519 | - | |
| 520 | - $previous_entry_id = $previous_entry->id; | |
| 517 | + $previous_entry_id = $previous_entry->id; | |
| 521 | 518 | self::destroy_entry_later( $previous_entry_id ); |
| 522 | 519 | |
| 523 | 520 | return $values; |
| 524 | 521 | } |
| @@ -569,18 +566,13 @@ | ||
| 569 | 566 | * |
| 570 | 567 | * @return array |
| 571 | 568 | */ |
| 572 | 569 | public static function before_save_settings( $settings, $action ) { |
| 573 | - $settings['gateway'] = ! empty( $settings['gateway'] ) ? (array) $settings['gateway'] : array( 'stripe' ); | |
| 570 | + $settings['gateway'] = ! empty( $settings['gateway'] ) ? (array) $settings['gateway'] : array( 'stripe' ); | |
| 574 | 571 | |
| 575 | - if ( in_array( 'square', $settings['gateway'] ) ) { | |
| 576 | - $currency = FrmSquareLiteConnectHelper::get_merchant_currency(); | |
| 577 | - | |
| 578 | - if ( false !== $currency ) { | |
| 579 | - $settings['currency'] = strtolower( $currency ); | |
| 580 | - } else { | |
| 581 | - $settings['currency'] = 'usd'; | |
| 582 | - } | |
| 572 | + if ( in_array( 'square', $settings['gateway'], true ) ) { | |
| 573 | + $currency = FrmSquareLiteConnectHelper::get_merchant_currency(); | |
| 574 | + $settings['currency'] = false !== $currency ? strtolower( $currency ) : 'usd'; | |
| 583 | 575 | } else { |
| 584 | 576 | $settings['currency'] = strtolower( $settings['currency'] ); |
| 585 | 577 | } |
| 586 | 578 | |
| @@ -603,8 +595,14 @@ | ||
| 603 | 595 | $settings['credit_card'] = $credit_card_field_id; |
| 604 | 596 | } |
| 605 | 597 | } |
| 606 | 598 | |
| 599 | + if ( ! in_array( 'stripe', $settings['gateway'], true ) ) { | |
| 600 | + // We only need a gateway field for Stripe add-on compatibility, | |
| 601 | + // so unless Stripe is selected, we can return early. | |
| 602 | + return $settings; | |
| 603 | + } | |
| 604 | + | |
| 607 | 605 | $gateway_field_id = FrmDb::get_var( |
| 608 | 606 | 'frm_fields', |
| 609 | 607 | array( |
| 610 | 608 | 'type' => 'gateway', |
| @@ -614,9 +612,9 @@ | ||
| 614 | 612 | |
| 615 | 613 | if ( ! $gateway_field_id ) { |
| 616 | 614 | self::add_a_gateway_field( $form_id ); |
| 617 | 615 | } |
| 618 | - | |
| 616 | + | |
| 619 | 617 | return $settings; |
| 620 | 618 | } |
| 621 | 619 | |
| 622 | 620 | /** |
| @@ -649,10 +647,54 @@ | ||
| 649 | 647 | * |
| 650 | 648 | * @return false|int |
| 651 | 649 | */ |
| 652 | 650 | protected static function add_a_field( $form_id, $field_type, $field_name ) { |
| 653 | - $new_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id ); | |
| 654 | - $new_values['name'] = $field_name; | |
| 655 | - $field_id = FrmField::create( $new_values ); | |
| 656 | - return $field_id; | |
| 651 | + $new_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id ); | |
| 652 | + $new_values['name'] = $field_name; | |
| 653 | + $new_values['field_order'] = self::get_field_order_before_submit( $form_id, $new_values['field_order'] ); | |
| 654 | + return FrmField::create( $new_values ); | |
| 655 | + } | |
| 656 | + | |
| 657 | + /** | |
| 658 | + * When auto-injecting a field, ensure it is placed before the submit button. | |
| 659 | + * | |
| 660 | + * @since 6.29 | |
| 661 | + * | |
| 662 | + * @param int $form_id | |
| 663 | + * @param int $field_order | |
| 664 | + * | |
| 665 | + * @return int | |
| 666 | + */ | |
| 667 | + private static function get_field_order_before_submit( $form_id, $field_order ) { | |
| 668 | + $submit_field = FrmSubmitHelper::get_submit_field( $form_id ); | |
| 669 | + | |
| 670 | + if ( ! $submit_field || $field_order < (int) $submit_field->field_order ) { | |
| 671 | + return $field_order; | |
| 672 | + } | |
| 673 | + | |
| 674 | + $submit_order = (int) $submit_field->field_order; | |
| 675 | + FrmField::update( $submit_field->id, array( 'field_order' => $submit_order + 1 ) ); | |
| 676 | + return $submit_order; | |
| 677 | + } | |
| 678 | + | |
| 679 | + /** | |
| 680 | + * Remove credit card validation errors. | |
| 681 | + * | |
| 682 | + * @param array $errors | |
| 683 | + * @param stdClass $field | |
| 684 | + * | |
| 685 | + * @return array | |
| 686 | + */ | |
| 687 | + public static function remove_cc_errors( $errors, $field ) { | |
| 688 | + $field_id = $field->temp_id ?? $field->id; | |
| 689 | + | |
| 690 | + if ( isset( $errors[ 'field' . $field_id . '-cc' ] ) ) { | |
| 691 | + unset( $errors[ 'field' . $field_id . '-cc' ] ); | |
| 692 | + } | |
| 693 | + | |
| 694 | + if ( isset( $errors[ 'field' . $field_id ] ) ) { | |
| 695 | + unset( $errors[ 'field' . $field_id ] ); | |
| 696 | + } | |
| 697 | + | |
| 698 | + return $errors; | |
| 657 | 699 | } |
| 658 | 700 | } |