Skip to content

Latest commit

 

History

History
211 lines (171 loc) · 7.39 KB

README.md

File metadata and controls

211 lines (171 loc) · 7.39 KB

fastify-stripe

NPM version GitHub CI Build Status Coverage Status js-standard-style Greenkeeper badge

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 (errors) {
    reply.code(500)
    return errors
  }
})

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(errors => {
      reply
        .code(500)
        .send(errors)
    })
})

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

Options

  • apiKey [ required ]: Your account's secret key wich is available in your Stripe Dashboard

  • 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.

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

fastify
  .register(require('fastify-stripe'), {
    apiKey: 'sk_test_...',
    name: 'test',
    timeout: 240000 // in ms (this is 24 seconds)
  })
  .register(require('fastify-stripe'), {
    apiKey: 'sk_prod_...',
    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 (errors) {
    reply.code(500)
    return errors
  }

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 (errors) {
    reply.code(500)
    return errors
  }
})

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.
// Retry this request once before giving up
fastify.stripe.customers.create(
  {
    email: 'customer@example.com'
  },
  {
    maxNetworkRetries: 1
  }
)
  • timeout [ optional ]: The request timeout is configurable and must be expressed in ms. By default Stripe Node.js Library 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.
fastify.stripe.customers.create(
  {
    email: 'customer@example.com'
  },
  {
    timeout: 20000 // in ms (this is 20 seconds)
  }
)
  • apiVersion [ optional ]: It is important for you to check what version of the Stripe REST API you're currently consuming (via the dashboard). You can explicitly set the version you wish to use like so: { apiVersion: '2019-02-19' }. You can later change this on a per-request basis with the new Stripe API configuration object property apiVersion.
fastify.stripe.customers.list({ apiVersion: '2019-02-19' })

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.

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