coopflow / fastify-stripe

Stripe Node.js Library instance initialization and encapsulation in fastify framework.
MIT License
22 stars 4 forks source link
credit-card fastify fastify-plugin payment-processing stripe

fastify-stripe

NPM version GitHub CI Coverage Status js-standard-style

Stripe Node.js Library instance initialization and encapsulation in fastify framework.

Install

Install the package with:

npm i stripe fastify-stripe --save

Usage

The 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 then call the Stripe API and you are done.

const fastify = require('fastify')({ logger: true })

fastify.register(require('fastify-stripe'), {
  apiKey: 'sk_test_...'
})

fastify.get('/customers/add', async (request, reply) => {
  const { stripe } = fastify
  const email = 'customer@exemple.com'

  try {
    // We create a new customer using Stripe API
    const customers = await stripe.customers.create({ email })

    reply.code(201)
    return {
      status: 'ok',
      message: `customer ${email} succesfully added`,
      customers
    }
  } catch (err) {
    reply.code(500)
    return err
  }
})

fastify.listen(3000, (err) => {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
})

and it works using Promises too:

const fastify = require('fastify')({ logger: true })

fastify.register(require('fastify-stripe'), {
  apiKey: 'sk_test_...'
})

fastify.get('/customers/add', function (request, reply) {
  const { stripe } = fastify
  const email = 'customer@exemple.com'

  // We create a new customer using Stripe API
  stripe.customers.create({ email })
    .then((customers) => {
      reply
        .code(201)
        .send({
          status: 'ok',
          message: `customer ${email} succesfully added`,
          customers
        })
    })
    .catch((err) => {
      reply
        .code(500)
        .send(err)
    })
})

fastify.listen(3000, function (err) {
  if (err) {
    fastify.log.error(err)
    process.exit(1)
  }
})

Options

fastify .register(require('fastify-stripe'), { apiKey: 'sktest...', name: 'test', timeout: 24000 // in ms (this is 24 seconds) }) .register(require('fastify-stripe'), { apiKey: 'skprod...', name: 'prod' })

fastify.get('/customers/test/add', async (request, reply) => { const { stripe } = fastify const email = 'customer@exemple.com'

try { // We create a new customer using the singular Stripe test instance // This instance has a request timeout of 4 minutes // and uses a Stripe test API key const customers = await stripe['test'].customers.create({ email })

reply.code(201)
return {
  status: 'ok',
  message: `customer ${email} succesfully added`,
  customers
}

} catch (err) { reply.code(500) return err }

fastify.get('/customers/prod/add', async (request, reply) => { const { stripe } = fastify const email = 'customer@exemple.com'

try { // We create a new customer using the singular Stripe prod instance // This instance has the default request timeout of 2 minutes // and uses a Stripe production API key const customers = await stripe.prod.customers.create({ email })

reply.code(201)
return {
  status: 'ok',
  message: `customer ${email} succesfully added`,
  customers
}

} catch (err) { reply.code(500) return err } })

fastify.listen(3000, (err) => { if (err) { fastify.log.error(err) process.exit(1) } })


* `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).
```js
// Retry this request once before giving up
fastify.stripe.customers.create(
  {
    email: 'customer@example.com'
  },
  {
    maxNetworkRetries: 1
  }
)

Note for TypeScript users: If you are a TypeScript user, follow Stripe Node.js Library maintainers recommendations: "we recommend upgrading your account's API Version to the latest version. If 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."

You can see the other options in Node Stripe config object initialization documentation.

Documentation

See the Node Stripe API docs.

Acknowledgements

This project is kindly sponsored by coopflow.

License

Licensed under MIT