-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugin.js
51 lines (42 loc) · 1.33 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict'
const fp = require('fastify-plugin')
const fs = require('fs')
const path = require('path')
function fastifyStripe (fastify, options, next) {
const { apiKey, name, ...stripeOptions } = options
if (!apiKey) {
return next(new Error('You must provide a Stripe API key'))
}
const config = Object.assign(
{
appInfo: {
name: 'fastify-stripe',
url: 'https://github.com/coopflow/fastify-stripe',
version: JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'))).version
}
},
stripeOptions
)
const stripe = require('stripe')(apiKey, config)
if (name) {
if (stripe[name]) {
return next(new Error(`fastify-stripe '${name}' is a reserved keyword`))
} else if (!fastify.stripe) {
fastify.decorate('stripe', Object.create(null))
} else if (Object.prototype.hasOwnProperty.call(fastify.stripe, name)) {
return next(new Error(`Stripe '${name}' instance name has already been registered`))
}
fastify.stripe[name] = stripe
} else {
if (fastify.stripe) {
return next(new Error('fastify-stripe has already been registered'))
} else {
fastify.decorate('stripe', stripe)
}
}
next()
}
module.exports = fp(fastifyStripe, {
fastify: '>=2.11.0',
name: 'fastify-stripe'
})