stripe-fire
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -28,4 +28,4 @@ /* | ||
var refundsRef = ref instanceof Firebase ? ref : new Firebase(ref); | ||
refundsRef.on('child_added', Charge.createChild(chargesRef, 'Refund', callback, accessToken, alterRequest)); | ||
refundsRef.on('child_added', Charge.child(chargesRef, 'Refund', ['metadata'], callback, accessToken, alterRequest)); | ||
return { | ||
@@ -44,3 +44,4 @@ off: refundsRef.off | ||
couponsRef.on('child_changed', Coupon.update(['metadata'], callback, accessToken)); | ||
couponsRef.on('child_removed', Coupon.delete(callback, accessToken)); | ||
return { | ||
@@ -56,2 +57,3 @@ off: couponsRef.off | ||
customersRef.on('child_changed', Customer.update(['account_balance', 'card', 'coupon', 'default_card', 'description', 'email', 'metadata'], callback, accessToken)); | ||
customersRef.on('child_removed', Customer.delete(callback, accessToken)); | ||
@@ -61,4 +63,4 @@ return { | ||
var cardsRef = ref instanceof Firebase ? ref : new Firebase(ref); | ||
cardsRef.on('child_added', Customer.createChild(customersRef, 'Card', callback, accessToken, alterRequest)); | ||
cardsRef.on('child_added', Customer.child(customersRef, 'Card', ['address_city', 'address_country', 'address_line1', 'address_line2', 'address_state', 'address_zip', 'exp_month', 'exp_year', 'name'], callback, accessToken, alterRequest)); | ||
return { | ||
@@ -70,4 +72,4 @@ off: cardsRef.off | ||
var subscriptionsRef = ref instanceof Firebase ? ref : new Firebase(ref); | ||
subscriptionsRef.on('child_added', Customer.createChild(customersRef, 'Subscription', callback, accessToken, alterRequest)); | ||
subscriptionsRef.on('child_added', Customer.createChild(customersRef, 'Subscription', ['plan', 'coupon', 'prorate', 'trial_end', 'card', 'quantity', 'application_fee_percent', 'metadata'], callback, accessToken, alterRequest)); | ||
return { | ||
@@ -86,3 +88,4 @@ off: subscriptionsRef.off | ||
plansRef.on('child_changed', Plan.update(['name', 'metadata', 'statement_description'], callback, accessToken)); | ||
plansRef.on('child_removed', Plan.delete(callback, accessToken)); | ||
return { | ||
@@ -89,0 +92,0 @@ off: plansRef.off |
@@ -14,6 +14,6 @@ /* | ||
// Creates a function to handle responses from Stripe | ||
var responseHandler = function(err, obj) { | ||
var callback = typeof this.callback === 'function' ? this.callback : function() {}; | ||
var responseHandler = function (err, obj) { | ||
var callback = typeof this.callback === 'function' ? this.callback : function () {}; | ||
delete this.callback; | ||
if (err) { | ||
@@ -23,3 +23,5 @@ this.childSnapshot.ref().set({ | ||
}); | ||
} else { | ||
} else if (this.child) { | ||
this.childSnapshot.ref().remove(); | ||
} else if (this.action !== 'delete') { | ||
obj._hash = hash(obj); | ||
@@ -37,3 +39,3 @@ this.childSnapshot.ref().set(obj); | ||
StripeObject.prototype.create = function (callback, accessToken, alterRequest) { | ||
StripeObject.prototype.child = function (parentRef, childObject, keys, callback, accessToken, alterRequest) { | ||
var stripe = this.stripe; | ||
@@ -44,27 +46,129 @@ var object = this.object; | ||
var data = childSnapshot.val(); | ||
var name = childSnapshot.name(); | ||
// Check if object already has Stripe id | ||
if (data.id) { | ||
// Retrieve stripe object and store in location | ||
stripe[object.toLowerCase() + 's'].retrieve(data.id, function (err, obj) { | ||
if (err) { | ||
childSnapshot.ref().set({ | ||
err: err.raw | ||
var action = data.id ? (data.deleted ? 'delete' : 'update') : 'create'; | ||
// Generate access token if a function is provided | ||
if (typeof accessToken === 'function') { | ||
accessToken = accessToken.call({ | ||
action: action, | ||
childSnapshot: childSnapshot | ||
}, data); | ||
} | ||
// Alter request if function is provided | ||
if (typeof alterRequest === 'function') { | ||
data = alterRequest.call({ | ||
accessToken: accessToken, | ||
action: action, | ||
childSnapshot: childSnapshot | ||
}, data); | ||
} | ||
// Specify context for response handler | ||
var context = { | ||
accessToken: accessToken, | ||
action: action, | ||
callback: callback, | ||
child: true, | ||
childSnapshot: childSnapshot | ||
}; | ||
// Get Stripe id of parent object | ||
parentRef.child(name).once('value', function (dataSnapshot) { | ||
var parentObjectId = dataSnapshot.val().id; | ||
// Create args array to send to Stripe function | ||
var args = [parentObjectId]; | ||
if (action === 'create') { | ||
args.push(data); | ||
} else if (action === 'update') { | ||
args.push(data.id); | ||
var request = {}; | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (data[key]) { | ||
request[key] = data[key]; | ||
} | ||
} | ||
args.push(request); | ||
} else { | ||
args.push(data.id); | ||
} | ||
if (accessToken) { | ||
args.push(accessToken); | ||
} | ||
args.push(function (err, obj) { | ||
responseHandler.call(context, err, obj); | ||
if (accessToken) { | ||
stripe[object.toLowerCase() + 's'].retrieve(obj[object.toLowerCase()], accessToken, function (err, obj) { | ||
responseHandler.call({ | ||
childSnapshot: dataSnapshot | ||
}, err, obj); | ||
}); | ||
} else { | ||
obj._hash = hash(obj); | ||
childSnapshot.ref().set(obj); | ||
stripe[object.toLowerCase() + 's'].retrieve(obj[object.toLowerCase()], function (err, obj) { | ||
responseHandler.call({ | ||
childSnapshot: dataSnapshot | ||
}, err, obj); | ||
}); | ||
} | ||
}); | ||
// Call Stripe child action function | ||
if (args.length === 5) { | ||
stripe[object.toLowerCase() + 's'][action + childObject](args[0], args[1], args[2], args[3], args[4]); | ||
} else if (args.length === 4) { | ||
stripe[object.toLowerCase() + 's'][action + childObject](args[0], args[1], args[2], args[3]); | ||
} else { | ||
stripe[object.toLowerCase() + 's'][action + childObject](args[0], args[1], args[2]); | ||
} | ||
}); | ||
}; | ||
}; | ||
StripeObject.prototype.create = function (callback, accessToken, alterRequest) { | ||
var stripe = this.stripe; | ||
var object = this.object; | ||
return function (childSnapshot) { | ||
var data = childSnapshot.val(); | ||
// Generate access token if a function is provided | ||
if (typeof accessToken === 'function') { | ||
accessToken = accessToken.call({ | ||
action: 'create', | ||
childSnapshot: childSnapshot | ||
}, data); | ||
} | ||
// Specify context for response handler | ||
var context = { | ||
accessToken: accessToken, | ||
callback: callback, | ||
childSnapshot: childSnapshot | ||
}; | ||
// Check if object already has Stripe id | ||
if (data.id) { | ||
// Specify context action for response handler | ||
context.action = 'retrieve'; | ||
// Check if access token should be used on retrieve | ||
if (accessToken) { | ||
stripe[object.toLowerCase() + 's'].retrieve(data.id, accessToken, function (err, obj) { | ||
responseHandler.call(context, err, obj); | ||
}); | ||
} else { | ||
stripe[object.toLowerCase() + 's'].retrieve(data.id, function (err, obj) { | ||
responseHandler.call(context, err, obj); | ||
}); | ||
} | ||
} else { | ||
// Generate access token if a function is provided | ||
if(accessToken === 'function') { | ||
accessToken = accessToken.call({ | ||
action: 'create', | ||
childSnapshot: childSnapshot | ||
}, data); | ||
} | ||
// Alter request if function is provided | ||
if(typeof alterRequest === 'function') { | ||
if (typeof alterRequest === 'function') { | ||
data = alterRequest.call({ | ||
@@ -76,21 +180,22 @@ accessToken: accessToken, | ||
} | ||
// Check if access token should be used on create | ||
// Specify context action for response handler | ||
context.action = 'create'; | ||
// Create args array to send to Stripe function | ||
var args = [data]; | ||
if (accessToken) { | ||
stripe[object.toLowerCase() + 's'].create(data, accessToken, function(err, obj) { | ||
responseHandler.call({ | ||
accessToken: accessToken, | ||
action: 'create', | ||
callback: callback, | ||
childSnapshot: childSnapshot | ||
}, err, obj); | ||
}); | ||
args.push(accessToken); | ||
} | ||
args.push(function (err, obj) { | ||
responseHandler.call(context, err, obj); | ||
}); | ||
// Call Stripe create function | ||
if (args.length === 3) { | ||
stripe[object.toLowerCase() + 's'].create(args[0], args[1], args[2]); | ||
} else { | ||
stripe[object.toLowerCase() + 's'].create(data, function(err, obj) { | ||
responseHandler.call({ | ||
action: 'create', | ||
callback: callback, | ||
childSnapshot: childSnapshot | ||
}, err, obj); | ||
}); | ||
stripe[object.toLowerCase() + 's'].create(args[0], args[1]); | ||
} | ||
@@ -101,67 +206,41 @@ } | ||
StripeObject.prototype.createChild = function (parentRef, childObject, callback, accessToken, alterRequest) { | ||
StripeObject.prototype.delete = function (callback, accessToken) { | ||
var stripe = this.stripe; | ||
var object = this.object; | ||
return function (childSnapshot) { | ||
var data = childSnapshot.val(); | ||
var parentName = childSnapshot.name(); | ||
return function (oldChildSnapshot) { | ||
var data = oldChildSnapshot.val(); | ||
var create = function (request) { | ||
// Handle response from Stripe | ||
var handleResponse = function (err, childObj) { | ||
if (err) { | ||
childSnapshot.ref().set({ | ||
err: err.raw | ||
}); | ||
callback(err); | ||
} else { | ||
childSnapshot.ref().remove(); | ||
stripe[object.toLowerCase() + 's'].retrieve(childObj[object.toLowerCase()], function (err, parentObj) { | ||
if (err) { | ||
parentRef.child(parentName).set({ | ||
err: err.raw | ||
}); | ||
callback(err); | ||
} else { | ||
parentObj._hash = hash(parentObj); | ||
parentRef.child(parentName).set(parentObj); | ||
callback(null, childObj); | ||
} | ||
}); | ||
} | ||
}; | ||
// Generate access token if a function is provided | ||
if (typeof accessToken === 'function') { | ||
accessToken = accessToken.call({ | ||
action: 'delete', | ||
childSnapshot: oldChildSnapshot | ||
}, data); | ||
} | ||
// Get Stripe id of parent object | ||
parentRef.child(parentName).once('value', function (dataSnapshot) { | ||
var parentObjectId = dataSnapshot.val().id; | ||
// Check if access token should be used | ||
if (accessToken) { | ||
accessToken = typeof accessToken === 'string' ? accessToken : accessToken(request); | ||
stripe[object.toLowerCase() + 's']['create' + childObject](parentObjectId, request, accessToken, handleResponse); | ||
} else { | ||
stripe[object.toLowerCase() + 's']['create' + childObject](parentObjectId, request, handleResponse); | ||
} | ||
}); | ||
// Specify context for response handler | ||
var context = { | ||
accessToken: accessToken, | ||
action: 'delete', | ||
callback: callback, | ||
childSnapshot: oldChildSnapshot | ||
}; | ||
// Alter request if user provided a function before creating | ||
create(alterRequest ? alterRequest(data) : data); | ||
}; | ||
}; | ||
// Create args array to send to Stripe function | ||
var args = [data.id]; | ||
StripeObject.prototype.delete = function (callback, accessToken) { | ||
var stripe = this.stripe; | ||
var object = this.object; | ||
if (accessToken) { | ||
args.push(accessToken); | ||
} | ||
return function (oldChildSnapshot) { | ||
var name = oldChildSnapshot.name(); | ||
args.push(function (err, obj) { | ||
responseHandler.call(context, err, obj); | ||
}); | ||
// Check if access token should be used | ||
if (accessToken) { | ||
accessToken = typeof accessToken === 'string' ? accessToken : accessToken(name); | ||
stripe[object.toLowerCase() + 's'].del(name, accessToken, responseHandler(oldChildSnapshot, callback, 'delete')); | ||
// Call Stripe delete function | ||
if (args.length === 3) { | ||
stripe[object.toLowerCase() + 's'].del(args[0], args[1], args[2]); | ||
} else { | ||
stripe[object.toLowerCase() + 's'].del(name, responseHandler(oldChildSnapshot, callback, 'delete')); | ||
stripe[object.toLowerCase() + 's'].del(args[0], args[1]); | ||
} | ||
@@ -179,5 +258,5 @@ }; | ||
delete data._hash; | ||
// Check if object has changed via sha1 hash | ||
if (_hash !== hash(data)) { | ||
// Check if object has changed via sha1 hash (or is an error) | ||
if (!data.err && _hash !== hash(data)) { | ||
// Create update request to send to Stripe | ||
@@ -191,5 +270,5 @@ var request = {}; | ||
} | ||
// Generate access token if a function is provided | ||
if(accessToken === 'function') { | ||
if (typeof accessToken === 'function') { | ||
accessToken = accessToken.call({ | ||
@@ -200,5 +279,5 @@ action: 'update', | ||
} | ||
// Alter request if function is provided | ||
if(typeof alterRequest === 'function') { | ||
if (typeof alterRequest === 'function') { | ||
request = alterRequest.call({ | ||
@@ -210,21 +289,27 @@ accessToken: accessToken, | ||
} | ||
// Check if access token should be used on update | ||
// Specify context for response handler | ||
var context = { | ||
accessToken: accessToken, | ||
action: 'update', | ||
callback: callback, | ||
childSnapshot: childSnapshot | ||
}; | ||
// Create args array to send to Stripe function | ||
var args = [data.id, request]; | ||
if (accessToken) { | ||
stripe[object.toLowerCase() + 's'].update(data.id, request, accessToken, function(err, obj) { | ||
responseHandler.call({ | ||
accessToken: accessToken, | ||
action: 'update', | ||
callback: callback, | ||
childSnapshot: childSnapshot | ||
}, err, obj); | ||
}); | ||
args.push(accessToken); | ||
} | ||
args.push(function (err, obj) { | ||
responseHandler.call(context, err, obj); | ||
}); | ||
// Call Stripe update function | ||
if (args.length === 4) { | ||
stripe[object.toLowerCase() + 's'].update(args[0], args[1], args[2], args[3]); | ||
} else { | ||
stripe[object.toLowerCase() + 's'].update(data.id, request, function(err, obj) { | ||
responseHandler.call({ | ||
action: 'update', | ||
callback: callback, | ||
childSnapshot: childSnapshot | ||
}, err, obj); | ||
}); | ||
stripe[object.toLowerCase() + 's'].update(args[0], args[1], args[2]); | ||
} | ||
@@ -231,0 +316,0 @@ } |
{ | ||
"name": "stripe-fire", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"main": "lib/stripe-fire.js", | ||
"description": "A Node.js module that handles Stripe API calls for Firebase references.", | ||
"homepage": "https://github.com/davidcaseria/StripeFire", | ||
"bugs": "https://github.com/davidcaseria/stripe-fire/issues", | ||
"bugs": "https://github.com/davidcaseria/StripeFire/issues", | ||
"author": { | ||
@@ -14,3 +14,3 @@ "name": "Dave", | ||
"type": "git", | ||
"url": "https://github.com/davidcaseria/stripe-fire" | ||
"url": "https://github.com/davidcaseria/StripeFire" | ||
}, | ||
@@ -43,5 +43,5 @@ "license": "MIT", | ||
"firebase": "~1.1.2", | ||
"stripe": "^2.8.0", | ||
"stripe": "~2.8.0", | ||
"hash-object": "~0.1.7" | ||
} | ||
} |
@@ -41,4 +41,5 @@ # StripeFire | ||
The `callback`, `accessToken`, and `alterRequest` functions may be called with the `this` variable set with the following properties: | ||
- `accessToken`: the access token used in the request | ||
- `action`: create/update as appropriate | ||
- `action`: create/delete/update as appropriate | ||
- `childSnapshot`: the Firebase [DataSnapshot](https://www.firebase.com/docs/web/api/datasnapshot/) used to generate the request | ||
@@ -117,3 +118,3 @@ | ||
stripeFire.coupons("https://stripe-fire.firebaseio.com/coupons", function(err, coupon) { | ||
// Called after a create/update coupon request is sent to Stripe | ||
// Called after a create/delete/update coupon request is sent to Stripe | ||
}, "ACCESS_TOKEN", function(couponData) { | ||
@@ -139,3 +140,3 @@ // Called before a create/update coupon request is sent to Stripe | ||
metadata: { | ||
key: 'value' | ||
key: "value" | ||
} | ||
@@ -152,3 +153,3 @@ }); | ||
var customers = stripeFire.customers("https://stripe-fire.firebaseio.com/customers", function(err, customer) { | ||
// Called after a create/update customer request is sent to Stripe | ||
// Called after a create/delete/update customer request is sent to Stripe | ||
}, "ACCESS_TOKEN", function(customerData) { | ||
@@ -228,3 +229,3 @@ // Called before a create/update customer request is sent to Stripe | ||
stripeFire.plans("https://stripe-fire.firebaseio.com/plans", function(err, plan) { | ||
// Called after a create/update plan request is sent to Stripe | ||
// Called after a create/delete/update plan request is sent to Stripe | ||
}, "ACCESS_TOKEN", function(planData) { | ||
@@ -253,3 +254,3 @@ // Called before a create/update plan request is sent to Stripe | ||
metadata: { | ||
key: 'value' | ||
key: "value" | ||
} | ||
@@ -273,3 +274,3 @@ }); | ||
Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com). | ||
In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com). | ||
@@ -276,0 +277,0 @@ |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
24702
334
275
0
+ Addedstripe@2.8.0(transitive)
- Removedstripe@2.9.0(transitive)
Updatedstripe@~2.8.0