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