Skip to content

Instantly share code, notes, and snippets.

@abovedave
Created February 28, 2020 20:28
Show Gist options
  • Save abovedave/0087e2ddfb4e8a0ddba4aee25be371fa to your computer and use it in GitHub Desktop.
Save abovedave/0087e2ddfb4e8a0ddba4aee25be371fa to your computer and use it in GitHub Desktop.
const config = require(__dirname + '/../../../stripe.json')
const stripe = require('stripe')(config.secret)
const apiCongig = require('@dadi/api').Config
const Model = require('@dadi/api').Model
module.exports = (obj, type, data) => {
switch(type) {
case 'beforeCreate':
const stripecall = new Promise((resolve, reject) => {
// Get the brand name from the id
Model('brands')
.find({'_id': obj.brand[0]}, { limit: 1 }, (err, brand) => {
if (err) console.log(err)
// Product object for stripe
let product = {
name: brand.results && brand.results[0] ? `${brand.results[0].name} ${obj.name}` : obj.name,
active: obj.published || false
}
return stripe
.products
.create(Object.assign({
type: 'service'
}, product))
.then(response => {
// Add the stripe id to the object
obj.stripe = response.id
// Good to sell as one-off
return stripe.products.create(Object.assign({
attributes: ['size', 'sizeUnit'],
type: 'good'
}, product))
})
.then(response => {
// Add the id to the object
obj.stripeGood = response.id
return resolve(obj)
})
.catch(err => {
throw err
})
})
})
// Return the result of the above
return stripecall.then(obj => {
return obj
})
break
default:
return obj
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment