mongoose-stripe-customers
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,7 +0,7 @@ | ||
var mongoose = require('mongoose'), | ||
expect = require('chai').expect, | ||
fs = require('fs'), | ||
mongooseStripeCustomers = require('./mongoose-stripe-customers'), | ||
Schema = mongoose.Schema, | ||
connection; | ||
var mongoose = require('mongoose'); | ||
var expect = require('chai').expect; | ||
var fs = require('fs'); | ||
var mongooseStripeCustomers = require('./mongoose-stripe-customers'); | ||
var Schema = mongoose.Schema; | ||
var connection; | ||
@@ -12,2 +12,4 @@ var STRIPE_API_KEY = process.env.STRIPE_TEST_KEY; | ||
mongoose.Promise = global.Promise; | ||
function customerSchema() { | ||
@@ -36,6 +38,4 @@ return new Schema({ | ||
before(function (done) { | ||
connection = mongoose.createConnection('mongodb://localhost/unit_test'); | ||
connection.once('connected', function() { | ||
done(); | ||
}); | ||
connection = mongoose.createConnection(process.env.MONGO_URL || 'mongodb://localhost/unit_test'); | ||
connection.once('connected', done); | ||
}); | ||
@@ -45,5 +45,3 @@ | ||
connection.db.dropDatabase(function() { | ||
connection.close(function() { | ||
done(); | ||
}); | ||
connection.close(done); | ||
}); | ||
@@ -92,3 +90,3 @@ }); | ||
it('should assign a stripe_customer_id to the model during creation', function(done) { | ||
it('should assign a stripe_customer_id to the model during creation', function() { | ||
Customer = connection.model('Customer', testSchema); | ||
@@ -103,10 +101,5 @@ customer = new Customer({ | ||
customer.save(function(err) { | ||
if(err) { | ||
done(err); | ||
} else { | ||
expect(customer.stripe_customer_id).to.not.equal(undefined); | ||
expect(customer.stripe_customer_id.substr(0, 4)).to.equal('cus_'); | ||
done(); | ||
} | ||
return customer.save().then(function (savedCustomer) { | ||
expect(savedCustomer.stripe_customer_id).to.not.equal(undefined); | ||
expect(savedCustomer.stripe_customer_id.substr(0, 4)).to.equal('cus_'); | ||
}); | ||
@@ -131,3 +124,3 @@ }); | ||
it('should have all override data on the stripe customer', function(done) { | ||
it('should have all override data on the stripe customer', function() { | ||
CustomerOverrides = connection.model('CustomerOverrides', testSchema); | ||
@@ -142,29 +135,18 @@ customer = new CustomerOverrides({ | ||
customer.save(function(err) { | ||
if(err) { | ||
done(err); | ||
} else { | ||
stripe.customers.retrieve(customer.stripeCustomerID) | ||
.then(function(stripeCustomer) { | ||
expect(stripeCustomer.description).to.equal(customer.firstName + ' ' + customer.lastName); | ||
expect(stripeCustomer.email).to.equal(customer.email); | ||
expect(stripeCustomer.metadata.phone).to.equal(customer.phone); | ||
expect(stripeCustomer.metadata.customerType).to.equal(customer.customerType); | ||
expect(stripeCustomer.metadata._id).to.equal(customer.id); | ||
done(); | ||
}, done); | ||
} | ||
return customer.save().then(function(savedCustomer) { | ||
return stripe.customers.retrieve(savedCustomer.stripeCustomerID); | ||
}).then(function(stripeCustomer) { | ||
expect(stripeCustomer.description).to.equal(customer.firstName + ' ' + customer.lastName); | ||
expect(stripeCustomer.email).to.equal(customer.email); | ||
expect(stripeCustomer.metadata.phone).to.equal(customer.phone); | ||
expect(stripeCustomer.metadata.customerType).to.equal(customer.customerType); | ||
expect(stripeCustomer.metadata._id).to.equal(customer.id); | ||
}); | ||
}); | ||
it('should not attempt to create the Stripe customer if the doc is old', function(done) { | ||
it('should not attempt to create the Stripe customer if the doc is old', function() { | ||
var oldStripeCustomerID = customer.stripeCustomerID; | ||
customer.save(function(err) { | ||
if(err) { | ||
done(err); | ||
} else { | ||
expect(oldStripeCustomerID).to.equal(customer.stripeCustomerID); | ||
done(); | ||
} | ||
return customer.save().then(function(savedCustomer) { | ||
expect(savedCustomer.stripeCustomerID).to.equal(oldStripeCustomerID); | ||
}); | ||
@@ -178,2 +160,49 @@ }); | ||
before(function() { | ||
testSchema = new Schema({ | ||
name: { | ||
first: { type: String }, | ||
last: { type: String } | ||
}, | ||
customerType: { type: String }, | ||
phone: { type: String }, | ||
email: { type: String } | ||
}); | ||
testSchema.plugin(mongooseStripeCustomers, { | ||
stripeApiKey: STRIPE_API_KEY, | ||
stripeCustomerIdField: 'stripeCustomerID', | ||
firstNameField: 'name.first', | ||
lastNameField: 'name.last', | ||
emailField: 'email', | ||
metaData: [ 'customerType', 'phone', '_id' ] | ||
}); | ||
}); | ||
it('should allow override data to be embedded documents on the stripe customer', function() { | ||
CustomerEmbedOverrides = connection.model('CustomerEmbedOverrides', testSchema); | ||
customer = new CustomerEmbedOverrides({ | ||
name: { | ||
first: 'test', | ||
last: 'customer' | ||
}, | ||
customerType: 'testing', | ||
phone: '1112223333', | ||
email: 'test@testing.com' | ||
}); | ||
return customer.save().then(function(savedCustomer) { | ||
return stripe.customers.retrieve(savedCustomer.stripeCustomerID); | ||
}).then(function(stripeCustomer) { | ||
expect(stripeCustomer.description).to.equal(customer.name.first + ' ' + customer.name.last); | ||
expect(stripeCustomer.email).to.equal(customer.email); | ||
expect(stripeCustomer.metadata.phone).to.equal(customer.phone); | ||
expect(stripeCustomer.metadata.customerType).to.equal(customer.customerType); | ||
expect(stripeCustomer.metadata._id).to.equal(customer.id); | ||
}); | ||
}); | ||
}); | ||
describe('with default overrides', function() { | ||
var testSchema, customer; | ||
before(function() { | ||
testSchema = customerSchema(); | ||
@@ -190,3 +219,3 @@ testSchema.plugin(mongooseStripeCustomers, { | ||
it('should not use keys from the model that do not exist', function(done) { | ||
it('should not use keys from the model that do not exist', function() { | ||
var CustomerOverrides2 = connection.model('CustomerOverrides2', testSchema); | ||
@@ -201,12 +230,6 @@ customer = new CustomerOverrides2({ | ||
customer.save(function(err) { | ||
if(err) { | ||
done(err); | ||
} else { | ||
stripe.customers.retrieve(customer.stripeCustomerID) | ||
.then(function(stripeCustomer) { | ||
expect(stripeCustomer.metadata.address).to.equal(undefined); | ||
done(); | ||
}, done); | ||
} | ||
return customer.save().then(function(savedCustomer) { | ||
return stripe.customers.retrieve(savedCustomer.stripeCustomerID); | ||
}).then(function(stripeCustomer) { | ||
expect(stripeCustomer.metadata.address).to.equal(undefined); | ||
}); | ||
@@ -213,0 +236,0 @@ }); |
@@ -1,3 +0,3 @@ | ||
var _ = require('lodash'), | ||
util = require('util'); | ||
var _ = require('lodash'); | ||
var util = require('util'); | ||
@@ -59,42 +59,46 @@ /** | ||
var doc = this; | ||
var customer = { | ||
metadata: {} | ||
}; | ||
var firstName; | ||
var lastName; | ||
if(!doc.isNew) { | ||
// They already are a stripe customer, do nothing. | ||
return done(); | ||
} | ||
// If the document is new, or the document doesn't have a stripe customer yet, create one. | ||
if(doc.isNew) { | ||
var customer = { | ||
metadata: {} | ||
}; | ||
if(options.emailField) { | ||
customer.description = customer.email = doc.get(options.emailField); | ||
} | ||
if(options.emailField) { | ||
customer.email = doc[options.emailField]; | ||
customer.description = customer.email; | ||
} | ||
if(options.firstNameField && options.lastNameField) { | ||
firstName = doc.get(options.firstNameField); | ||
lastName = doc.get(options.lastNameField); | ||
if(options.firstNameField && options.lastNameField) { | ||
var firstName = doc[options.firstNameField], | ||
lastName = doc[options.lastNameField]; | ||
customer.description = firstName + ' ' + lastName; | ||
customer.metadata[options.firstNameField] = firstName; | ||
customer.metadata[options.lastNameField] = lastName; | ||
} | ||
customer.description = firstName + ' ' + lastName; | ||
if(options.metaData instanceof Array && options.metaData.length) { | ||
options.metaData.forEach(function(key) { | ||
if(doc[key]) { | ||
customer.metadata[key] = (key === '_id') ? doc[key].toString() : doc[key]; | ||
} | ||
}); | ||
} | ||
// Don't set via embedded path but as string for stripe metadata | ||
customer.metadata[options.firstNameField] = firstName; | ||
customer.metadata[options.lastNameField] = lastName; | ||
} | ||
stripe.customers.create(customer) | ||
.then(function(customer) { | ||
doc[options.stripeCustomerIdField] = customer.id; | ||
done(); | ||
}, done); | ||
if(options.metaData instanceof Array && options.metaData.length) { | ||
options.metaData.forEach(function(key) { | ||
var val = doc.get(key); | ||
// They already are a stripe customer, do nothing. | ||
} else { | ||
done(); | ||
if(val) { | ||
_.set(customer.metadata, key, (key === '_id') ? val.toString() : val); | ||
} | ||
}); | ||
} | ||
stripe.customers.create(customer) | ||
.then(function(customer) { | ||
doc.set(options.stripeCustomerIdField, customer.id); | ||
done(); | ||
}, done); | ||
}); | ||
}; |
{ | ||
"name": "mongoose-stripe-customers", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A mongoose plugin that creates a stripe customer when a new document is created and stores the Stripe customer id to that document.", | ||
@@ -42,5 +42,5 @@ "main": "mongoose-stripe-customers.js", | ||
"dependencies": { | ||
"lodash": "^3.10.1", | ||
"lodash": "^4.7.0", | ||
"stripe": "^4.0.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31836
346
4
+ Addedlodash@4.17.21(transitive)
- Removedlodash@3.10.1(transitive)
Updatedlodash@^4.7.0