{"id":16400670,"url":"https://github.com/coopflow/fastify-stripe","last_synced_at":"2025-03-21T02:33:00.597Z","repository":{"id":34247089,"uuid":"173485756","full_name":"coopflow/fastify-stripe","owner":"coopflow","description":"Stripe Node.js Library instance initialization and encapsulation in fastify framework.","archived":false,"fork":false,"pushed_at":"2023-03-06T10:57:35.000Z","size":136,"stargazers_count":22,"open_issues_count":5,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-12T05:28:21.283Z","etag":null,"topics":["credit-card","fastify","fastify-plugin","payment-processing","stripe"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coopflow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-02T18:44:57.000Z","updated_at":"2024-08-21T06:41:40.000Z","dependencies_parsed_at":"2023-02-18T05:45:45.690Z","dependency_job_id":null,"html_url":"https://github.com/coopflow/fastify-stripe","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coopflow%2Ffastify-stripe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coopflow%2Ffastify-stripe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coopflow%2Ffastify-stripe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coopflow%2Ffastify-stripe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coopflow","download_url":"https://codeload.github.com/coopflow/fastify-stripe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221811380,"owners_count":16884305,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["credit-card","fastify","fastify-plugin","payment-processing","stripe"],"created_at":"2024-10-11T05:28:21.906Z","updated_at":"2024-10-28T09:12:09.176Z","avatar_url":"https://github.com/coopflow.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# fastify-stripe\r\n\r\n[![NPM version](https://img.shields.io/npm/v/fastify-stripe.svg?style=flat)](https://www.npmjs.com/package/fastify-stripe)\r\n[![GitHub CI](https://github.com/coopflow/fastify-stripe/workflows/GitHub%20CI/badge.svg)](https://github.com/coopflow/fastify-stripe/actions?workflow=GitHub+CI)\r\n[![Coverage Status](https://coveralls.io/repos/github/coopflow/fastify-stripe/badge.svg?branch=master)](https://coveralls.io/github/coopflow/fastify-stripe?branch=master)\r\n[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)\r\n\r\n[Stripe Node.js Library](https://github.com/stripe/stripe-node) instance initialization and encapsulation in [fastify](https://github.com/fastify/fastify) framework.\r\n\r\n## Install\r\n\r\nInstall the package with:\r\n```sh\r\nnpm i stripe fastify-stripe --save\r\n```\r\n\r\n\r\n## Usage\r\n\r\nThe package needs to be added to your project with `register` and you must at least configure your account's secret key wich is available in your [Stripe Dashboard](https://dashboard.stripe.com/account/apikeys) then call the [Stripe](https://github.com/stripe/stripe-node) API and you are done.\r\n```js\r\nconst fastify = require('fastify')({ logger: true })\r\n\r\nfastify.register(require('fastify-stripe'), {\r\n  apiKey: 'sk_test_...'\r\n})\r\n\r\nfastify.get('/customers/add', async (request, reply) =\u003e {\r\n  const { stripe } = fastify\r\n  const email = 'customer@exemple.com'\r\n\r\n  try {\r\n    // We create a new customer using Stripe API\r\n    const customers = await stripe.customers.create({ email })\r\n\r\n    reply.code(201)\r\n    return {\r\n      status: 'ok',\r\n      message: `customer ${email} succesfully added`,\r\n      customers\r\n    }\r\n  } catch (err) {\r\n    reply.code(500)\r\n    return err\r\n  }\r\n})\r\n\r\nfastify.listen(3000, (err) =\u003e {\r\n  if (err) {\r\n    fastify.log.error(err)\r\n    process.exit(1)\r\n  }\r\n})\r\n```\r\n\r\nand it works using `Promises` too:\r\n```js\r\nconst fastify = require('fastify')({ logger: true })\r\n\r\nfastify.register(require('fastify-stripe'), {\r\n  apiKey: 'sk_test_...'\r\n})\r\n\r\nfastify.get('/customers/add', function (request, reply) {\r\n  const { stripe } = fastify\r\n  const email = 'customer@exemple.com'\r\n\r\n  // We create a new customer using Stripe API\r\n  stripe.customers.create({ email })\r\n    .then((customers) =\u003e {\r\n      reply\r\n        .code(201)\r\n        .send({\r\n          status: 'ok',\r\n          message: `customer ${email} succesfully added`,\r\n          customers\r\n        })\r\n    })\r\n    .catch((err) =\u003e {\r\n      reply\r\n        .code(500)\r\n        .send(err)\r\n    })\r\n})\r\n\r\nfastify.listen(3000, function (err) {\r\n  if (err) {\r\n    fastify.log.error(err)\r\n    process.exit(1)\r\n  }\r\n})\r\n```\r\n\r\n### Options\r\n\r\n* `apiKey` **[ required ]**: Your account's secret key wich is available in your [Stripe Dashboard](https://dashboard.stripe.com/account/apikeys)\r\n\r\n\r\n* `name` **[ optional ]**: Through this option `fastify-stripe` lets you define multiple Stripe singular instances (with different options parameters if you wish) that you can later use in your application.\r\n```js\r\nconst fastify = require('fastify')({ logger: true })\r\n\r\nfastify\r\n  .register(require('fastify-stripe'), {\r\n    apiKey: 'sk_test_...',\r\n    name: 'test',\r\n    timeout: 24000 // in ms (this is 24 seconds)\r\n  })\r\n  .register(require('fastify-stripe'), {\r\n    apiKey: 'sk_prod_...',\r\n    name: 'prod'\r\n  })\r\n\r\nfastify.get('/customers/test/add', async (request, reply) =\u003e {\r\n  const { stripe } = fastify\r\n  const email = 'customer@exemple.com'\r\n\r\n  try {\r\n    // We create a new customer using the singular Stripe test instance\r\n    // This instance has a request timeout of 4 minutes\r\n    // and uses a Stripe test API key\r\n    const customers = await stripe['test'].customers.create({ email })\r\n\r\n    reply.code(201)\r\n    return {\r\n      status: 'ok',\r\n      message: `customer ${email} succesfully added`,\r\n      customers\r\n    }\r\n  } catch (err) {\r\n    reply.code(500)\r\n    return err\r\n  }\r\n\r\nfastify.get('/customers/prod/add', async (request, reply) =\u003e {\r\n  const { stripe } = fastify\r\n  const email = 'customer@exemple.com'\r\n\r\n  try {\r\n    // We create a new customer using the singular Stripe prod instance\r\n    // This instance has the default request timeout of 2 minutes\r\n    // and uses a Stripe production API key\r\n    const customers = await stripe.prod.customers.create({ email })\r\n\r\n    reply.code(201)\r\n    return {\r\n      status: 'ok',\r\n      message: `customer ${email} succesfully added`,\r\n      customers\r\n    }\r\n  } catch (err) {\r\n    reply.code(500)\r\n    return err\r\n  }\r\n})\r\n\r\nfastify.listen(3000, (err) =\u003e {\r\n  if (err) {\r\n    fastify.log.error(err)\r\n    process.exit(1)\r\n  }\r\n})\r\n```\r\n\r\n* `maxNetworkRetries` **[ optional ]**: Automatic network retries can be enabled with setMaxNetworkRetries. This will retry requests `n` times with exponential backoff if they fail due to an intermittent network problem. Idempotency keys are added where appropriate to prevent duplication. You can later change this on a per-request basis with the new Stripe API configuration object property [`maxNetworkRetries`](https://github.com/stripe/stripe-node#network-retries).\r\n```js\r\n// Retry this request once before giving up\r\nfastify.stripe.customers.create(\r\n  {\r\n    email: 'customer@example.com'\r\n  },\r\n  {\r\n    maxNetworkRetries: 1\r\n  }\r\n)\r\n```\r\n\r\n* `timeout` **[ optional ]**: The request timeout is configurable and must be expressed in `ms`. By default [Stripe Node.js Library](https://github.com/stripe/stripe-node) uses Node's default of 120 seconds (`{ timeout: 120000 }`). You can later change this on a per-request basis with the new Stripe API configuration object property [`timeout`](https://github.com/stripe/stripe-node#configuring-timeout).\r\n```js\r\nfastify.stripe.customers.create(\r\n  {\r\n    email: 'customer@example.com'\r\n  },\r\n  {\r\n    timeout: 20000 // in ms (this is 20 seconds)\r\n  }\r\n)\r\n```\r\n\r\n* `apiVersion` **[ optional ]**: It is important for you to check what version of the Stripe REST API you're currently consuming ([via the dashboard](https://manage.stripe.com/account/apikeys)). You can explicitly set the version you wish to use like so: `{ apiVersion: '2020-08-27' }`. You can later change this on a per-request basis with the new Stripe API configuration object property [`apiVersion`](https://github.com/stripe/stripe-node/wiki/Migration-guide-for-v8#mark-all-setter-methods-as-deprecated-emit-warnings).\r\n```js\r\nfastify.stripe.customers.list({ apiVersion: '2020-08-27' })\r\n```\r\n*__Note__: You don't need to set a version explicitly. If you don't set it the Stripe REST API will use your account's default, which you can view under your dashboard (Account Settings » API Keys). Setting a version explicitly does not affect your account's default version. It'll only affect the API you consume through that singular stripe instance.*\r\n\r\n*__Note for TypeScript users__: If you are a TypeScript user, follow [Stripe Node.js Library](https://github.com/stripe/stripe-node) maintainers recommendations: \"we recommend upgrading your account's API Version to the latest version.\r\nIf you wish to remain on your account's default API version, you may pass `null` or another version instead of the latest version, and add a `@ts-ignore` comment here and anywhere the types differ between API versions.\"*\r\n\r\n*You can see the other options in [Node Stripe config object initialization documentation](https://github.com/stripe/stripe-node#initialize-with-config-object).*\r\n\r\n## Documentation\r\n\r\nSee the [Node Stripe API docs](https://stripe.com/docs/api/node#intro).\r\n\r\n## Acknowledgements\r\n\r\nThis project is kindly sponsored by [coopflow](https://www.coopflow.com).\r\n\r\n\r\n## License\r\n\r\nLicensed under [MIT](https://github.com/coopflow/fastify-stripe/blob/master/LICENSE)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoopflow%2Ffastify-stripe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoopflow%2Ffastify-stripe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoopflow%2Ffastify-stripe/lists"}