moleculer-stripe

2.0.0 • Public • Published

moleculer-stripe

Build Coverage Status Codacy Badge Maintainability Known Vulnerabilities Libraries.io dependency status for latest release Downloads FOSSA Status

Stripe product available in this service

Ready for new PSD2 EU reglementations (SCA)

Product Implemented SCA
Payments
    Checkout
✔️
✔️
    Charges
✔️
(Use Checkout or PaymentIntents)
    PaymentIntents
✔️
✔️
Billing
    Customers
✔️
    Subscriptions
✔️
    Invoices
    Taxes
Connect
✔️
Issuing
Terminal
Webhooks - With signature check
✔️

How to use it

const StripeMixin = require('moleculer-stripe')

module.exports = {
  name: 'stripe',
  mixins: [StripeMixin],
  settings: {
    stripe: {
      /** @type {String} Secret key from Stripe dashboard. */
      secret: 'pk_',
      /* Before using webhook look in the README how to integrate it */
      webhook: {
        /** @type {Object?} Platefrom webhook (Default webhooks) */
        platform: {
          /** @type {String} Webhook signing from Stripe dashboard. */
          key: 'whsec_',
          /** @type {String?} Which action will be call when a webhook is received */
          action: undefined,
          /** @type {String?} Which event will be emitted when a webhook is received */
          event: undefined
        },
        /** @type {Object?} Connect webhook (Only if you use connect) */
        connect: {
          /** @type {String} Webhook signing from Stripe dashboard. */
          key: 'whsec_',
          /** @type {String?} Which action will be call when a webhook is received */
          action: undefined,
          /** @type {String?} Which event will be emitted when a webhook is received */
          event: undefined
        }
      },
      /** @type {String?} Version of this API (Default : latest). */
      version: undefined,
      /** @type {Boolean?} Enable/Disable telemetry for stripe (Default : true). */
      telemetry: true,
      /** @type {Function?} Custom function for config stripe, like setAppInfo for your plugin, ... (First args is stripe instance). */
      custom: undefined
    }
  }
}

All this options can be per call based :

const customStripeOptions = {
  secret: 'pk_',
  version: '2019-08-26',
  telemetry: false
}
broker.call('stripe.payment.intents.create', charge, { meta: { stripe: customStripeOptions } })

When using with Connect you can have an account meta (It's the stripe_account from the Connect documentation)

broker.call('stripe.payment.intents.create', charge, { meta: { stripe: { account: 'acc_' } } })

You can go further with stripe and there idempotency key (Because the Stripe API is freaking well thought)

// Get autogenerated idempotency key (UUID V4)
await ctx.call('stripe.payment.intents.create', charge)
console.log(ctx.meta.stripe.idempotency)

// Or you want to use from your side
await ctx.call('stripe.payment.intents.create', charge, { meta: { stripe: { idempotency: UUIDV4() } } })

Webhooks

Integration with moleculer-web

Declare your stripe service like above :

const StripeMixin = require('moleculer-stripe')

module.exports = {
  name: 'my.stripe.service',
  mixins: [StripeMixin],
  settings: { ... }
}

And in your web service add one route with the path of your webhook and the name of the stripe service :

const MoleculerWeb = require('moleculer-web')
const { StripeRoute } = require('moleculer-stripe')

module.exports = {
  name: 'web',
  mixins: [MoleculerWeb],
  settings: {
    routes: [StripeRoute('/webhook/stripe', 'my.stripe.service')]
  }
}

If you want to use Connect webhook, just add a third arguement at true :

const MoleculerWeb = require('moleculer-web')
const { StripeRoute } = require('moleculer-stripe')

module.exports = {
  name: 'web',
  mixins: [MoleculerWeb],
  settings: {
    routes: [StripeRoute('/webhook/stripe/platform', 'my.stripe.service'), StripeRoute('/webhook/stripe/connect', 'my.stripe.service', true)]
  }
}

If you want more "Pimp my ride" options on this route, look at src/index.js#78

Integration with any other web gateway

Declare your stripe service like above :

const StripeMixin = require('moleculer-stripe')

module.exports = {
  name: 'my.stripe.service',
  mixins: [StripeMixin],
  settings: { ... }
}

Then you need to call a specific action with the body of the webhook (⚠️ RAW Body, not json parsed ⚠️)

// Example with express (If you use that, checkout the offical 'moleculer-web')
// Do your express init things + moleculer init broker
app.post('/webhook/stripe', bodyParser.raw({ type: 'application/json' }), async (request, response) => {
  const result = await broker.call('my.stripe.service.webhook', { body: request.body, signature: request.headers['stripe-signature'], connect: false })
  return result ? response.json(result) : response.status(400).end()
})

API

A possiblity for every list actions, is to use 'autoPagination' from stripe node lib :

// When it's a number it will use `autoPagingToArray` and use the value of `pagination` as the limit
ctx.call('stripe.payment.intents.list', {}, { meta: { pagination: 10000 } })
// When i's a string it will use `autoPagingEach` and use the value of `pagination` as the action to call for each and pass the item as parameter
ctx.call('stripe.payment.intents.list', {}, { meta: { pagination: 'my.service.each.payment' } })
// Service will receive item from list
module.exports = {
  name: 'my.service',
  actions: {
    // Params is the stripe item
    'each.payment': ({ params }) => console.log(params.id, params.amount)
  }
}
Action Arguments
accounts.capabilities.list account
accounts.capabilities.retrieve account, id
accounts.capabilities.update account, capability, requested
accounts.create
accounts.del id
accounts.list
accounts.login id
accounts.reject id
accounts.retrieve id
accounts.update id, account
application.fees.list
application.fees.refunds.create fee
application.fees.refunds.list fee
application.fees.refunds.retrieve fee, id
application.fees.refunds.update fee, id, refund
application.fees.retrieve id
balance.retrieve id
balance.transactions.list
balance.transactions.retrieve id
charges.create
charges.del id
charges.list
charges.retrieve id
charges.update id, charge
checkout.sessions.create
checkout.sessions.del id
checkout.sessions.list
checkout.sessions.retrieve id
checkout.sessions.update id, session
country.specs.list
country.specs.retrieve id
customers.create
customers.del id
customers.list
customers.retrieve id
customers.update id, customer
payment.intents.cancel id
payment.intents.capture id
payment.intents.confirm id
payment.intents.create
payment.intents.list
payment.intents.retrieve id
payment.intents.update id, intent
payouts.cancel id
payouts.create
payouts.list
payouts.retrieve id
payouts.update id, payout
products.create
products.del id
products.list
products.retrieve id
products.update id, product
refunds.create
refunds.list
refunds.retrieve id
refunds.update id, refund
setup.intents.cancel id
setup.intents.confirm id
setup.intents.create
setup.intents.list
setup.intents.retrieve id
setup.intents.update id, intent
skus.create
skus.del id
skus.list
skus.retrieve id
skus.update id, sku
tax.rates.create
tax.rates.list
tax.rates.retrieve id
tax.rates.update id, rate
tokens.create
tokens.retrieve id
topups.cancel id
topups.create
topups.list
topups.retrieve id
topups.update id, topup
transfers.create
transfers.list
transfers.retrieve id
transfers.update id, transfer

Readme

Keywords

Package Sidebar

Install

npm i moleculer-stripe

Weekly Downloads

10

Version

2.0.0

License

MIT

Unpacked Size

31 kB

Total Files

9

Last publish

Collaborators

  • hugome