Search code examples
phplaravelstripe-paymentsstripe-connect

Format price with commission giving error on Stripe using Laravel


I'm trying to charge customers and then split the payment between myself and the owner who owns the product. The problem Im running into is if the owner of the product, (in this case some file uploads) inserts a value like 99.99 for their price, then someone tries to go checkout, once they enter their Stripe credit card information, they get this error:

enter image description here

In that case, the price of the product was $99.99.

Here is where I make the charge:

 try {
     $charge = Charge::create([
    'amount' => $file->price * 100,
    'currency' => 'usd',
    'source' => $request->stripeToken,
    'application_fee' => $file->calculateCommission() * 100
 ], [
    'stripe_account' => $file->user->stripe_id
 ]);

And here is the calculateCommission method on the File Model:

public function calculateCommission() {
    return (config('marketplace.sales.commission') / 100) * $this->price;
}

config('marketplace.sales.commission') in this case is just 20 (20%)

If a user enters a price of lets say 100 ($100), then that charge succeeds

How do I process this commission fee with decimal formats? Or what am I doing wrong?


Solution

  • As commented by @spencdev, it seems like floor does the trick. I have tested it with:

    99.99, .87, 1219.77, 3.00, 10.01, 100.00
    

    All work and go through to Stripe

    floor($file->calculateCommission() * 100)