Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error in give_maybe_sanitize_amount() with certain currencies #7568

Open
joshAppDev opened this issue Oct 12, 2024 · 1 comment
Open
Labels
type: bug Existing functionality is broken

Comments

@joshAppDev
Copy link

Details

In includes/formatting.php the give_maybe_sanitize_amount function can sometimes crash when calling number_format(). When this happened to me I was using the IDR currency, which uses the symbol Rp. The symbol is supposed to be removed by this line of code:

    // Remove currency symbols from number if any.
    $number = trim(str_replace(give_currency_symbols(true), '', $number));

But in my case the removal was incomplete. $number was originally "Rp10000", and became "p10000" after the removal. This was because there is another currency, ZAR, that has the symbol R. So depending on the array order from give_currency_sybmols(), the "R" can be removed first before IDR currency is proc'd.

This results in my $number == 'p10000'. And the letter 'p' crashes number_format().

Fatal error: Uncaught Error: number_format(): Argument #1 ($num) must be of type float, string given
in /wp/wp-content/plugins/give/includes/formatting.php on line 269

Call stack:

number_format()
wp-content/plugins/give/includes/formatting.php:269
give_maybe_sanitize_amount()
wp-content/plugins/give/includes/forms/template.php:1935
give_checkout_final_total()
wp-includes/class-wp-hook.php:326
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:348
WP_Hook::do_action()
wp-includes/plugin.php:517
do_action()
wp-content/plugins/give/includes/forms/template.php:2002
give_checkout_submit()
wp-includes/class-wp-hook.php:324
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:348
WP_Hook::do_action()
wp-includes/plugin.php:517
do_action()
wp-content/plugins/give/includes/forms/template.php:314
give_show_purchase_form()
wp-includes/class-wp-hook.php:324
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:348
WP_Hook::do_action()
wp-includes/plugin.php:517
do_action()
wp-content/plugins/give/includes/gateways/actions.php:64
give_load_ajax_gateway()
wp-includes/class-wp-hook.php:324
WP_Hook::apply_filters()
wp-includes/class-wp-hook.php:348
WP_Hook::do_action()
wp-includes/plugin.php:517
do_action()
wp-admin/admin-ajax.php:192

Steps to Reproduce

This was how I discovered the issue. Your own mileage may vary.

  1. Set IDR as your currency.
  2. Have multiple payment gateways available.
  3. Enter a donation but don't finalize it yet.
  4. Switch to a different gateway than the default.
  5. Browser will make an ajax call that fails due to 500 error.
  6. Inspect the network tab to see the error in ajax response.
@joshAppDev joshAppDev added the type: bug Existing functionality is broken label Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Existing functionality is broken
Projects
None yet
Development

No branches or pull requests

2 participants
@joshAppDev and others