stripe / react-stripe-js

React components for Stripe.js and Stripe Elements
https://stripe.com/docs/stripe-js/react
MIT License
1.76k stars 268 forks source link

[BUG]: Price Creation / Integer Problem #408

Closed ydax closed 1 year ago

ydax commented 1 year ago

What happened?

I'm using this library in combination with Firebase Cloud Functions. I'm passing the following integer to the stripe.prices.create API: 7840

This integer is tabulated by multiplying the amount, 78.4 times 100 in my cloud function.

When I try to create this price, the API call errors out and I get the following error message:

{
  "textPayload": "Unhandled error StripeInvalidRequestError: Invalid integer: 7840.000000000001\n    at Function.generate (/layers/google.nodejs.yarn/yarn_modules/node_modules/stripe/lib/Error.js:40:16)\n    at res.toJSON.then.StripeAPIError.message (/layers/google.nodejs.yarn/yarn_modules/node_modules/stripe/lib/StripeResource.js:238:35)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5) {\n  type: 'StripeInvalidRequestError',\n  raw: {\n    code: 'parameter_invalid_integer',\n    doc_url: 'https://stripe.com/docs/error-codes/parameter-invalid-integer',\n    message: 'Invalid integer: 7840.000000000001',\n    param: 'unit_amount',\n    request_log_url: 'https://dashboard.stripe.com/test/logs/req_H7H7da2LpL53WR?t=1686937796',\n    type: 'invalid_request_error',\n    headers: {\n      server: 'nginx',\n      date: 'Fri, 16 Jun 2023 17:49:56 GMT',\n      'content-type': 'application/json',\n      'content-length': '356',\n      connection: 'keep-alive',\n      'access-control-allow-credentials': 'true',\n      'access-control-allow-methods': 'GET, POST, HEAD, OPTIONS, DELETE',\n      'access-control-allow-origin': '*',\n      'access-control-expose-headers': 'Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required',\n      'access-control-max-age': '300',\n      'cache-control': 'no-cache, no-store',\n      'idempotency-key': 'd9a4f726-bc67-44d4-8d48-938f01ceb622',\n      'original-request': 'req_H7H7da2LpL53WR',\n      'request-id': 'req_H7H7da2LpL53WR',\n      'stripe-version': '2022-08-01',\n      'x-stripe-routing-context-priority-tier': 'api-testmode',\n      'strict-transport-security': 'max-age=63072000; includeSubDomains; preload'\n    },\n    statusCode: 400,\n    requestId: 'req_H7H7da2LpL53WR'\n  },\n  rawType: 'invalid_request_error',\n  code: 'parameter_invalid_integer',\n  doc_url: 'https://stripe.com/docs/error-codes/parameter-invalid-integer',\n  param: 'unit_amount',\n  detail: undefined,\n  headers: {\n    server: 'nginx',\n    date: 'Fri, 16 Jun 2023 17:49:56 GMT',\n    'content-type': 'application/json',\n    'content-length': '356',\n    connection: 'keep-alive',\n    'access-control-allow-credentials': 'true',\n    'access-control-allow-methods': 'GET, POST, HEAD, OPTIONS, DELETE',\n    'access-control-allow-origin': '*',\n    'access-control-expose-headers': 'Request-Id, Stripe-Manage-Version, X-Stripe-External-Auth-Required, X-Stripe-Privileged-Session-Required',\n    'access-control-max-age': '300',\n    'cache-control': 'no-cache, no-store',\n    'idempotency-key': 'd9a4f726-bc67-44d4-8d48-938f01ceb622',\n    'original-request': 'req_H7H7da2LpL53WR',\n    'request-id': 'req_H7H7da2LpL53WR',\n    'stripe-version': '2022-08-01',\n    'x-stripe-routing-context-priority-tier': 'api-testmode',\n    'strict-transport-security': 'max-age=63072000; includeSubDomains; preload'\n  },\n  requestId: 'req_H7H7da2LpL53WR',\n  statusCode: 400,\n  charge: undefined,\n  decline_code: undefined,\n  payment_intent: undefined,\n  payment_method: undefined,\n  payment_method_type: undefined,\n  setup_intent: undefined,\n  source: undefined\n}",
  "insertId": "648ca0c4000869cd4504ccdb",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "function_name": "modifyStripeProduct",
      "project_id": "staging-ccd7e",
      "region": "us-central1"
    }
  },
  "timestamp": "2023-06-16T17:49:56.551373Z",
  "severity": "ERROR",
  "labels": {
    "execution_id": "z58jpqq4vhbc",
    "instance_id": "00c61b117cf96b729b3a77fa3cf53d7473b82487f02ac4c2e22dde4e0bb3c4d116ea84297b2fcad88bee35a202651831bc560cb063fd167933"
  },
  "logName": "projects/staging-ccd7e/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
  "trace": "projects/staging-ccd7e/traces/e5a87ca4a732a2c72a65a75386a28284",
  "receiveTimestamp": "2023-06-16T17:49:56.591663053Z"
}

You'll note this in the error: Invalid integer: 7840.000000000001

For some reason, on the Stripe side of this API call, the integer of 7840 is being converted to this strange, long integer an causing the API to error out.

Environment

No response

Reproduction

No response

brendanm-stripe commented 1 year ago

Hey @ydax based on the API/endpoint and the timestamp in your error I was able to find your errored request, which you can view in your developer dashboard here: https://dashboard.stripe.com/test/logs/req_H7H7da2LpL53WR

As you can see, you're sending the invalid parameter value unit_amount: "7840.000000000001",

This kind of floating point precision calculation error is a known quirk of javascript and you'll need to ensure you round decimal calculation results in your code, eg using Math.round(78.4*100) or similar.

ydax commented 1 year ago

@brendanm-stripe Wow that's far out. Thanks for the heads up with the weird Javascript problem.