← All changes
|
square/controllers/FrmSquareLiteActionsController.php
+67
-42
6.25.1
→
trunk
View file →
| @@ -9,8 +9,9 @@ | ||
| 9 | 9 | * @since 6.22 |
| 10 | 10 | * |
| 11 | 11 | * @param string $callback |
| 12 | 12 | * @param array|false|object $field |
| 13 | + * | |
| 13 | 14 | * @return string |
| 14 | 15 | */ |
| 15 | 16 | public static function maybe_show_card( $callback, $field = false ) { |
| 16 | 17 | if ( false === $field ) { |
| @@ -19,13 +20,15 @@ | ||
| 19 | 20 | } |
| 20 | 21 | |
| 21 | 22 | $form_id = is_object( $field ) ? $field->form_id : $field['form_id']; |
| 22 | 23 | $actions = self::get_actions_before_submit( $form_id ); |
| 23 | - if ( empty( $actions ) ) { | |
| 24 | + | |
| 25 | + if ( ! $actions ) { | |
| 24 | 26 | return $callback; |
| 25 | 27 | } |
| 26 | 28 | |
| 27 | 29 | $field_id = is_object( $field ) ? $field->id : $field['id']; |
| 30 | + | |
| 28 | 31 | foreach ( $actions as $action ) { |
| 29 | 32 | if ( (int) $action->post_content['credit_card'] === (int) $field_id ) { |
| 30 | 33 | return self::class . '::show_card'; |
| 31 | 34 | } |
| @@ -41,8 +44,9 @@ | ||
| 41 | 44 | * |
| 42 | 45 | * @param array $field |
| 43 | 46 | * @param string $field_name |
| 44 | 47 | * @param array $atts |
| 48 | + * | |
| 45 | 49 | * @return void |
| 46 | 50 | */ |
| 47 | 51 | public static function show_card( $field, $field_name, $atts ) { |
| 48 | 52 | $actions = self::get_actions_before_submit( $field['form_id'] ); |
| @@ -67,19 +71,23 @@ | ||
| 67 | 71 | * |
| 68 | 72 | * @since 6.22 |
| 69 | 73 | * |
| 70 | 74 | * @param int|string $form_id |
| 75 | + * | |
| 71 | 76 | * @return array |
| 72 | 77 | */ |
| 73 | 78 | public static function get_actions_before_submit( $form_id ) { |
| 74 | 79 | $payment_actions = self::get_actions_for_form( $form_id ); |
| 80 | + | |
| 75 | 81 | foreach ( $payment_actions as $k => $payment_action ) { |
| 76 | 82 | $gateway = $payment_action->post_content['gateway']; |
| 77 | 83 | $is_square = $gateway === 'square' || ( is_array( $gateway ) && in_array( 'square', $gateway, true ) ); |
| 84 | + | |
| 78 | 85 | if ( ! $is_square || empty( $payment_action->post_content['amount'] ) ) { |
| 79 | 86 | unset( $payment_actions[ $k ] ); |
| 80 | 87 | } |
| 81 | 88 | } |
| 89 | + | |
| 82 | 90 | return $payment_actions; |
| 83 | 91 | } |
| 84 | 92 | |
| 85 | 93 | /** |
| @@ -88,8 +96,9 @@ | ||
| 88 | 96 | * |
| 89 | 97 | * @param WP_Post $action |
| 90 | 98 | * @param stdClass $entry |
| 91 | 99 | * @param mixed $form |
| 100 | + * | |
| 92 | 101 | * @return array |
| 93 | 102 | */ |
| 94 | 103 | public static function trigger_gateway( $action, $entry, $form ) { |
| 95 | 104 | $response = array( |
| @@ -97,17 +106,18 @@ | ||
| 97 | 106 | 'run_triggers' => false, |
| 98 | 107 | 'show_errors' => true, |
| 99 | 108 | ); |
| 100 | 109 | $atts = compact( 'action', 'entry', 'form' ); |
| 110 | + $amount = self::prepare_amount( $action->post_content['amount'], $atts ); | |
| 101 | 111 | |
| 102 | - $amount = self::prepare_amount( $action->post_content['amount'], $atts ); | |
| 103 | - if ( empty( $amount ) || $amount == 000 ) { | |
| 112 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 113 | + if ( ! $amount || $amount == 000 ) { | |
| 104 | 114 | $response['error'] = __( 'Please specify an amount for the payment', 'formidable' ); |
| 105 | 115 | return $response; |
| 106 | 116 | } |
| 107 | 117 | |
| 108 | 118 | if ( ! self::square_is_configured() ) { |
| 109 | - $response['error'] = __( 'There was a problem communicating with Square. Please try again.', 'formidable' ); | |
| 119 | + $response['error'] = __( 'Square still needs to be configured.', 'formidable' ); | |
| 110 | 120 | return $response; |
| 111 | 121 | } |
| 112 | 122 | |
| 113 | 123 | $payment_args = compact( 'form', 'entry', 'action', 'amount' ); |
| @@ -131,8 +141,9 @@ | ||
| 131 | 141 | /** |
| 132 | 142 | * Trigger a one time payment. |
| 133 | 143 | * |
| 134 | 144 | * @param array $atts The arguments for the payment. |
| 145 | + * | |
| 135 | 146 | * @return string|true string on error, true on success. |
| 136 | 147 | */ |
| 137 | 148 | private static function trigger_one_time_payment( $atts ) { |
| 138 | 149 | if ( empty( $_POST['square-token'] ) || empty( $_POST['square-verification-token'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| @@ -173,8 +184,9 @@ | ||
| 173 | 184 | /** |
| 174 | 185 | * Add a payment row for the payments table. |
| 175 | 186 | * |
| 176 | 187 | * @param array $atts The arguments for the payment. |
| 188 | + * | |
| 177 | 189 | * @return int |
| 178 | 190 | */ |
| 179 | 191 | private static function create_new_payment( $atts ) { |
| 180 | 192 | $atts['charge'] = (object) $atts['charge']; |
| @@ -185,15 +197,14 @@ | ||
| 185 | 197 | 'paysys' => 'square', |
| 186 | 198 | 'item_id' => $atts['entry']->id, |
| 187 | 199 | 'action_id' => $atts['action']->ID, |
| 188 | 200 | 'receipt_id' => $atts['charge']->id, |
| 189 | - 'sub_id' => isset( $atts['charge']->sub_id ) ? $atts['charge']->sub_id : '', | |
| 201 | + 'sub_id' => $atts['charge']->sub_id ?? '', | |
| 190 | 202 | 'test' => 'test' === FrmSquareLiteAppHelper::active_mode() ? 1 : 0, |
| 191 | 203 | ); |
| 192 | 204 | |
| 193 | 205 | $frm_payment = new FrmTransLitePayment(); |
| 194 | - $payment_id = $frm_payment->create( $new_values ); | |
| 195 | - return $payment_id; | |
| 206 | + return $frm_payment->create( $new_values ); | |
| 196 | 207 | } |
| 197 | 208 | |
| 198 | 209 | /** |
| 199 | 210 | * Create a new Square subscription and a subscription and payment for the payments tables. |
| @@ -198,8 +209,9 @@ | ||
| 198 | 209 | /** |
| 199 | 210 | * Create a new Square subscription and a subscription and payment for the payments tables. |
| 200 | 211 | * |
| 201 | 212 | * @param array $atts Includes 'customer', 'entry', 'action', 'amount'. |
| 213 | + * | |
| 202 | 214 | * @return bool|string True on success, error message on failure |
| 203 | 215 | */ |
| 204 | 216 | private static function trigger_recurring_payment( $atts ) { |
| 205 | 217 | if ( empty( $_POST['square-token'] ) || empty( $_POST['square-verification-token'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| @@ -249,8 +261,9 @@ | ||
| 249 | 261 | ); |
| 250 | 262 | } |
| 251 | 263 | |
| 252 | 264 | $response = FrmSquareLiteConnectHelper::create_subscription( $info ); |
| 265 | + | |
| 253 | 266 | if ( false === $response ) { |
| 254 | 267 | return FrmSquareLiteConnectHelper::get_latest_error_from_square_api(); |
| 255 | 268 | } |
| 256 | 269 | |
| @@ -263,9 +276,9 @@ | ||
| 263 | 276 | } |
| 264 | 277 | |
| 265 | 278 | // Add subscription database row. |
| 266 | 279 | // We do not add a payment row at this time. This is handled with our webhook handling. |
| 267 | - $subscription_id = self::create_new_subscription( $response->id, $atts ); | |
| 280 | + self::create_new_subscription( $response->id, $atts ); | |
| 268 | 281 | |
| 269 | 282 | return true; |
| 270 | 283 | } |
| 271 | 284 | |
| @@ -273,8 +286,9 @@ | ||
| 273 | 286 | * Prepare the description for a subscription. |
| 274 | 287 | * |
| 275 | 288 | * @param string $description |
| 276 | 289 | * @param array $atts |
| 290 | + * | |
| 277 | 291 | * @return string |
| 278 | 292 | */ |
| 279 | 293 | private static function prepare_subscription_description( $description, $atts ) { |
| 280 | 294 | $shortcode_atts = array( |
| @@ -289,8 +303,9 @@ | ||
| 289 | 303 | * Create a new subscription and payment for the payments tables. |
| 290 | 304 | * |
| 291 | 305 | * @param string $subscription_id |
| 292 | 306 | * @param array $atts |
| 307 | + * | |
| 293 | 308 | * @return int |
| 294 | 309 | */ |
| 295 | 310 | private static function create_new_subscription( $subscription_id, $atts ) { |
| 296 | 311 | $repeat_cadence = $atts['action']->post_content['repeat_cadence'] ?? 'DAILY'; |
| @@ -310,15 +325,15 @@ | ||
| 310 | 325 | 'test' => 'test' === FrmSquareLiteAppHelper::active_mode() ? 1 : 0, |
| 311 | 326 | ); |
| 312 | 327 | |
| 313 | 328 | $frm_payment = new FrmTransLiteSubscription(); |
| 314 | - $payment_id = $frm_payment->create( $new_values ); | |
| 315 | 329 | |
| 316 | - return $payment_id; | |
| 330 | + return $frm_payment->create( $new_values ); | |
| 317 | 331 | } |
| 318 | 332 | |
| 319 | 333 | /** |
| 320 | 334 | * @param string $repeat_cadence |
| 335 | + * | |
| 321 | 336 | * @return int |
| 322 | 337 | */ |
| 323 | 338 | private static function get_interval_count_from_repeat_cadence( $repeat_cadence ) { |
| 324 | 339 | switch ( $repeat_cadence ) { |
| @@ -348,8 +363,9 @@ | ||
| 348 | 363 | } |
| 349 | 364 | |
| 350 | 365 | /** |
| 351 | 366 | * @param string $repeat_cadence |
| 367 | + * | |
| 352 | 368 | * @return string |
| 353 | 369 | */ |
| 354 | 370 | private static function get_interval_from_repeat_cadence( $repeat_cadence ) { |
| 355 | 371 | switch ( $repeat_cadence ) { |
| @@ -386,31 +402,13 @@ | ||
| 386 | 402 | return (bool) FrmSquareLiteConnectHelper::get_merchant_id(); |
| 387 | 403 | } |
| 388 | 404 | |
| 389 | 405 | /** |
| 390 | - * Replace an [email] shortcode with the current user email. | |
| 391 | - * | |
| 392 | - * @param string $email | |
| 393 | - * @return string | |
| 394 | - */ | |
| 395 | - private static function replace_email_shortcode( $email ) { | |
| 396 | - if ( false === strpos( $email, '[email]' ) ) { | |
| 397 | - return $email; | |
| 398 | - } | |
| 399 | - | |
| 400 | - global $current_user; | |
| 401 | - return str_replace( | |
| 402 | - '[email]', | |
| 403 | - ! empty( $current_user->user_email ) ? $current_user->user_email : '', | |
| 404 | ||
| 405 | - ); | |
| 406 | - } | |
| 407 | - | |
| 408 | - /** | |
| 409 | 406 | * Convert the amount from 10.00 to 1000. |
| 410 | 407 | * |
| 411 | 408 | * @param mixed $amount |
| 412 | 409 | * @param array $atts |
| 410 | + * | |
| 413 | 411 | * @return string |
| 414 | 412 | */ |
| 415 | 413 | public static function prepare_amount( $amount, $atts = array() ) { |
| 416 | 414 | $amount = parent::prepare_amount( $amount, $atts ); |
| @@ -421,11 +419,13 @@ | ||
| 421 | 419 | /** |
| 422 | 420 | * If this form submits with ajax, load the scripts on the first page. |
| 423 | 421 | * |
| 424 | 422 | * @param array $params |
| 423 | + * | |
| 425 | 424 | * @return void |
| 426 | 425 | */ |
| 427 | 426 | public static function maybe_load_scripts( $params ) { |
| 427 | + // phpcs:ignore Universal.Operators.StrictComparisons | |
| 428 | 428 | if ( $params['form_id'] == $params['posted_form_id'] ) { |
| 429 | 429 | // This form has already been posted, so we aren't on the first page. |
| 430 | 430 | return; |
| 431 | 431 | } |
| @@ -430,8 +430,9 @@ | ||
| 430 | 430 | return; |
| 431 | 431 | } |
| 432 | 432 | |
| 433 | 433 | $form = FrmForm::getOne( $params['form_id'] ); |
| 434 | + | |
| 434 | 435 | if ( ! $form ) { |
| 435 | 436 | return; |
| 436 | 437 | } |
| 437 | 438 | |
| @@ -440,20 +441,24 @@ | ||
| 440 | 441 | 'fi.form_id' => $form->id, |
| 441 | 442 | 'type' => 'credit_card', |
| 442 | 443 | ) |
| 443 | 444 | ); |
| 445 | + | |
| 444 | 446 | if ( ! $credit_card_field ) { |
| 445 | 447 | return; |
| 446 | 448 | } |
| 447 | 449 | |
| 448 | 450 | $payment_actions = self::get_actions_before_submit( $form->id ); |
| 451 | + | |
| 449 | 452 | if ( ! $payment_actions ) { |
| 450 | 453 | return; |
| 451 | 454 | } |
| 452 | 455 | |
| 453 | 456 | $found_gateway = false; |
| 457 | + | |
| 454 | 458 | foreach ( $payment_actions as $action ) { |
| 455 | 459 | $gateways = $action->post_content['gateway']; |
| 460 | + | |
| 456 | 461 | if ( in_array( 'square', (array) $gateways, true ) ) { |
| 457 | 462 | $found_gateway = true; |
| 458 | 463 | break; |
| 459 | 464 | } |
| @@ -469,8 +474,9 @@ | ||
| 469 | 474 | /** |
| 470 | 475 | * Load front end JavaScript for a Stripe form. |
| 471 | 476 | * |
| 472 | 477 | * @param int $form_id |
| 478 | + * | |
| 473 | 479 | * @return void |
| 474 | 480 | */ |
| 475 | 481 | public static function load_scripts( $form_id ) { |
| 476 | 482 | if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) ) { |
| @@ -487,10 +493,12 @@ | ||
| 487 | 493 | } |
| 488 | 494 | |
| 489 | 495 | $action_settings = self::prepare_settings_for_js( $form_id ); |
| 490 | 496 | $found_gateway = false; |
| 497 | + | |
| 491 | 498 | foreach ( $action_settings as $action ) { |
| 492 | 499 | $gateways = $action['gateways']; |
| 500 | + | |
| 493 | 501 | if ( ! $gateways || in_array( 'square', (array) $gateways, true ) ) { |
| 494 | 502 | $found_gateway = true; |
| 495 | 503 | break; |
| 496 | 504 | } |
| @@ -518,9 +526,9 @@ | ||
| 518 | 526 | FrmAppHelper::plugin_version(), |
| 519 | 527 | false |
| 520 | 528 | ); |
| 521 | 529 | |
| 522 | - $square_vars = array( | |
| 530 | + $square_vars = array( | |
| 523 | 531 | 'formId' => $form_id, |
| 524 | 532 | 'nonce' => wp_create_nonce( 'frm_square_ajax' ), |
| 525 | 533 | 'ajax' => esc_url_raw( FrmAppHelper::get_ajax_url() ), |
| 526 | 534 | 'settings' => $action_settings, |
| @@ -538,11 +546,13 @@ | ||
| 538 | 546 | * @return string |
| 539 | 547 | */ |
| 540 | 548 | private static function get_app_id() { |
| 541 | 549 | $mode = FrmSquareLiteAppHelper::active_mode(); |
| 550 | + | |
| 542 | 551 | if ( 'live' === $mode ) { |
| 543 | 552 | return 'sq0idp-eR4XI1xgNduJAXcBvjemTg'; |
| 544 | 553 | } |
| 554 | + | |
| 545 | 555 | return 'sandbox-sq0idb-MXl8ilzmhAgsHWKV9c6ycQ'; |
| 546 | 556 | } |
| 547 | 557 | |
| 548 | 558 | /** |
| @@ -555,8 +565,9 @@ | ||
| 555 | 565 | } |
| 556 | 566 | |
| 557 | 567 | /** |
| 558 | 568 | * @param int $form_id |
| 569 | + * | |
| 559 | 570 | * @return array |
| 560 | 571 | */ |
| 561 | 572 | private static function get_style( $form_id ) { |
| 562 | 573 | $settings = self::get_style_settings_for_form( $form_id ); |
| @@ -566,9 +577,8 @@ | ||
| 566 | 577 | 'color' => $settings['text_color'], |
| 567 | 578 | 'backgroundColor' => $settings['bg_color'], |
| 568 | 579 | 'fontWeight' => $settings['field_weight'], |
| 569 | 580 | ), |
| 570 | - // How does input placeholder work?? | |
| 571 | 581 | 'input::placeholder' => array( |
| 572 | 582 | 'color' => $settings['text_color_disabled'], |
| 573 | 583 | ), |
| 574 | 584 | '.input-container' => array( |
| @@ -579,9 +589,9 @@ | ||
| 579 | 589 | ), |
| 580 | 590 | ); |
| 581 | 591 | |
| 582 | 592 | if ( ! empty( $settings['font'] ) ) { |
| 583 | - $style['input']['fontFamily'] = $settings['font']; | |
| 593 | + $style['input']['fontFamily'] = self::prepare_font_family_setting( $settings['font'] ); | |
| 584 | 594 | } |
| 585 | 595 | |
| 586 | 596 | /** |
| 587 | 597 | * @since 6.22 |
| @@ -593,13 +603,32 @@ | ||
| 593 | 603 | return apply_filters( 'frm_square_style', $style, $settings, $form_id ); |
| 594 | 604 | } |
| 595 | 605 | |
| 596 | 606 | /** |
| 607 | + * Prepare the font family setting for the Stripe element. | |
| 608 | + * | |
| 609 | + * @since 6.26 | |
| 610 | + * | |
| 611 | + * @param string $font | |
| 612 | + * | |
| 613 | + * @return string | |
| 614 | + */ | |
| 615 | + private static function prepare_font_family_setting( $font ) { | |
| 616 | + if ( ! str_contains( $font, ',' ) ) { | |
| 617 | + return $font; | |
| 618 | + } | |
| 619 | + | |
| 620 | + $fonts = explode( ',', $font ); | |
| 621 | + return trim( reset( $fonts ) ); | |
| 622 | + } | |
| 623 | + | |
| 624 | + /** | |
| 597 | 625 | * Get the border radius for Stripe elements. |
| 598 | 626 | * |
| 599 | 627 | * @since 6.22 |
| 600 | 628 | * |
| 601 | 629 | * @param array $settings |
| 630 | + * | |
| 602 | 631 | * @return string |
| 603 | 632 | */ |
| 604 | 633 | private static function get_border_radius( $settings ) { |
| 605 | 634 | if ( ! empty( $settings['field_shape_type'] ) ) { |
| @@ -619,8 +648,9 @@ | ||
| 619 | 648 | * |
| 620 | 649 | * @since 6.22 |
| 621 | 650 | * |
| 622 | 651 | * @param int $form_id |
| 652 | + * | |
| 623 | 653 | * @return array |
| 624 | 654 | */ |
| 625 | 655 | private static function get_style_settings_for_form( $form_id ) { |
| 626 | 656 | if ( ! $form_id ) { |
| @@ -627,8 +657,9 @@ | ||
| 627 | 657 | return array(); |
| 628 | 658 | } |
| 629 | 659 | |
| 630 | 660 | $style = FrmStylesController::get_form_style( $form_id ); |
| 661 | + | |
| 631 | 662 | if ( ! $style ) { |
| 632 | 663 | return array(); |
| 633 | 664 | } |
| 634 | 665 | |
| @@ -633,8 +664,9 @@ | ||
| 633 | 664 | } |
| 634 | 665 | |
| 635 | 666 | $settings = FrmStylesHelper::get_settings_for_output( $style ); |
| 636 | 667 | $disallowed = array( ';', ':', '!important' ); |
| 668 | + | |
| 637 | 669 | foreach ( $settings as $k => $s ) { |
| 638 | 670 | if ( is_string( $s ) ) { |
| 639 | 671 | $settings[ $k ] = str_replace( $disallowed, '', $s ); |
| 640 | 672 | } |
| @@ -651,26 +683,19 @@ | ||
| 651 | 683 | * |
| 652 | 684 | * @param array $errors |
| 653 | 685 | * @param stdClass $field |
| 654 | 686 | * @param array $values |
| 687 | + * | |
| 655 | 688 | * @return array |
| 656 | 689 | */ |
| 657 | 690 | public static function remove_cc_validation( $errors, $field, $values ) { |
| 658 | 691 | $has_processed = ! empty( $_POST['square-token'] ) && ! empty( $_POST['square-verification-token'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 692 | + | |
| 659 | 693 | if ( ! $has_processed ) { |
| 660 | 694 | return $errors; |
| 661 | 695 | } |
| 662 | 696 | |
| 663 | - $field_id = isset( $field->temp_id ) ? $field->temp_id : $field->id; | |
| 664 | - | |
| 665 | - if ( isset( $errors[ 'field' . $field_id . '-cc' ] ) ) { | |
| 666 | - unset( $errors[ 'field' . $field_id . '-cc' ] ); | |
| 667 | - } | |
| 668 | - if ( isset( $errors[ 'field' . $field_id ] ) ) { | |
| 669 | - unset( $errors[ 'field' . $field_id ] ); | |
| 670 | - } | |
| 671 | - | |
| 672 | - return $errors; | |
| 697 | + return FrmTransLiteActionsController::remove_cc_errors( $errors, $field ); | |
| 673 | 698 | } |
| 674 | 699 | |
| 675 | 700 | /** |
| 676 | 701 | * @return void |