3

I am debugging my code. I am no genius at PHP and need some help to decipher what I declare as the parameter if it is not an integer...

Can anyone help?

{

  "error": {

    "code": "parameter_invalid_integer",

    "doc_url": "https://stripe.com/docs/error-codes/parameter-invalid-integer",

    "message": "Invalid integer: 1.13",

    "param": "amount",

    "type": "invalid_request_error"

  }

$app->post('/createCharges', function() use ($app) { $response = array();

    $json = $app->request->getBody();
    $data = array(json_decode($json,true));

    $amount = $data[0]['amount'];
    $source = $data[0]['source'];
    $appointmentid = $data[0]['appointmentid'];


    \Stripe\Stripe::setVerifySslCerts(false);
    \Stripe\Stripe::setApiKey(STRIPE_API_KEY);

    $charge = \Stripe\Charge::create(array(
        "amount" => $amount,
        "currency" => "usd",
        "source" => $source,
        "description" => "test order from ios"
    ));
c andrews
  • 31
  • 1
  • 3

3 Answers3

7

With Stripe, the amount property of a Charge is always an integer in the smallest unit of a currency (cents for USD).

https://stripe.com/docs/api/charges/create#create_charge-amount

So to charge $1.13 you'd need to make sure the amount that you pass in \Stripe\Charge::create is 113 rather than 1.13 --- if your front-end is passing a decimal value a simple *100 should get you the value you need here, e.g. $amount = (int)($data[0]['amount'] * 100);

duck
  • 5,240
  • 1
  • 15
  • 15
1

Though it's an aged post nevertheless I'm willing to leave an answer to help someone else.

Stripe always receive integer as $cent value. So, whenever you tend to create a charge or payout or transfer or any type of transaction through Stripe you are bound to set cent value as an integer. So to charge $5010.458 you must have to convert it to cent as an integer which mustn't contain any fractional/decimal components.

You have several options to do so. For instance:

$amount = (number_format($amount, 2, '.', '') * 100)

If you charged as cent you may use: round(), ceil(), or floor() PHP functions to get nearest integer value. Also, you can use casting:* (int) $amount or intval($amount) as well.

Nazmus Shakib
  • 802
  • 1
  • 9
  • 18
0

(int)($request->amount * 100),

$checkout_session = \Stripe\PaymentIntent::create([
        'amount' => (int)($request->amount * 100),
        'currency' => 'AED',
        'description' => "booking"
    ]);
Aamir Anwar
  • 147
  • 1
  • 6