concretecms-community-store / community_store_stripe

Stripe payment add-on for Community Store for concrete5
MIT License
7 stars 2 forks source link

Invalid Integer showing in logs, payments will not process #5

Open tooqinc opened 7 years ago

tooqinc commented 7 years ago

I have Stripe running on two sites (one .com, one .ca) so they can run payments in both currencies. The sites are completely separate entities, however, the .ca was cloned from the .com (full backup made and restored to a new account, separate accounts in WHM).

The .com is running fine and processing payments, but the .ca is providing errors when attempting payments, other than the $0.50 test item transactions.

Status is:400 Type is:invalid_request_error Code is: Param is:amount Message is:Invalid integer: 5018.75

Fairly obvious that the price is not dropping the numbers after the decimal place in the request to process, but I'm not quite sure where the code that sets the price lives and all other trouble shooting has failed me (turning off tax, swapping between form and button, uninstalling and reinstalling the Stripe add-on, etc).

Happy to answer any further questions that can help resolve this.

DevScarabyte commented 7 years ago

$response = \Stripe\Charge::create(array("amount" => number_format(StoreCalculator::getGrandTotal(),2)*100, "currency" => $currency, "source" => $token));

^ this will fix this by rounding floats with 3 or more decimals to 2 and then multiply it by 100 to get the desired outcome.

Mesuva commented 7 years ago

Some code like this should be put into the Stripe add-on to guarantee the number is the correct format, but what is important is working out why the Stripe payment method is being passed a value with more then two decimal places in the first place.

Can you post what values you were using for the product(s), tax rates and anything else that might be useful to recreate the calculation?

With the addition of number_format, I think this should be replaced by by round instead:

$response = \Stripe\Charge::create(array("amount" => round(StoreCalculator::getGrandTotal(),2)*100, "currency" => $currency, "source" => $token));

If number_format is used, don't forget to specify the extra parameters needed so you don't end up with commas in the value. It should at least be:

$response = \Stripe\Charge::create(array("amount" => number_format(StoreCalculator::getGrandTotal(),2, '.', '')*100, "currency" => $currency, "source" => $token));
DevScarabyte commented 7 years ago

Rounding would be better I totally forgot that you can have it round to decimals but I knew number_format could.

Mesuva commented 7 years ago

I've added in for this Stripe package the extra rounding, but in community store itself I've added in some rounding where I think it's appropriate.

I've done this as a pull request, I'd love someone else to cast their eyes over this to check that I haven't done something like prematurely round.

https://github.com/concrete5-community-store/community_store/pull/215

DevScarabyte commented 7 years ago

The issue was getGrandTotal was returning 50.1875 so rounding any totals to 2 decimals anywhere would solve all issues with this.

I would say anytime a total is wanted it should be rounded.