2.0.0 • Published 1 year ago

moleculer-stripe v2.0.0

Weekly downloads
6
License
MIT
Repository
github
Last release
1 year ago

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)

ProductImplementedSCA
Payments
    Checkout:heavy_check_mark::heavy_check_mark:
    Charges:heavy_check_mark::x: (Use Checkout or PaymentIntents)
    PaymentIntents:heavy_check_mark::heavy_check_mark:
Billing
    Customers:heavy_check_mark:
    Subscriptions:heavy_check_mark:
    Invoices
    Taxes
Connect:heavy_check_mark:
Issuing:x:
Terminal:x:
Webhooks - With signature check:heavy_check_mark:

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 (:warning: RAW Body, not json parsed :warning:)

// 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)
  }
}
ActionArguments
accounts.capabilities.listaccount
accounts.capabilities.retrieveaccount, id
accounts.capabilities.updateaccount, capability, requested
accounts.create
accounts.delid
accounts.list
accounts.loginid
accounts.rejectid
accounts.retrieveid
accounts.updateid, account
application.fees.list
application.fees.refunds.createfee
application.fees.refunds.listfee
application.fees.refunds.retrievefee, id
application.fees.refunds.updatefee, id, refund
application.fees.retrieveid
balance.retrieveid
balance.transactions.list
balance.transactions.retrieveid
charges.create
charges.delid
charges.list
charges.retrieveid
charges.updateid, charge
checkout.sessions.create
checkout.sessions.delid
checkout.sessions.list
checkout.sessions.retrieveid
checkout.sessions.updateid, session
country.specs.list
country.specs.retrieveid
customers.create
customers.delid
customers.list
customers.retrieveid
customers.updateid, customer
payment.intents.cancelid
payment.intents.captureid
payment.intents.confirmid
payment.intents.create
payment.intents.list
payment.intents.retrieveid
payment.intents.updateid, intent
payouts.cancelid
payouts.create
payouts.list
payouts.retrieveid
payouts.updateid, payout
products.create
products.delid
products.list
products.retrieveid
products.updateid, product
refunds.create
refunds.list
refunds.retrieveid
refunds.updateid, refund
setup.intents.cancelid
setup.intents.confirmid
setup.intents.create
setup.intents.list
setup.intents.retrieveid
setup.intents.updateid, intent
skus.create
skus.delid
skus.list
skus.retrieveid
skus.updateid, sku
tax.rates.create
tax.rates.list
tax.rates.retrieveid
tax.rates.updateid, rate
tokens.create
tokens.retrieveid
topups.cancelid
topups.create
topups.list
topups.retrieveid
topups.updateid, topup
transfers.create
transfers.list
transfers.retrieveid
transfers.updateid, transfer