diff --git a/lib/StripeMethod.js b/lib/StripeMethod.js index c3d850f717..a1b5dac6bf 100644 --- a/lib/StripeMethod.js +++ b/lib/StripeMethod.js @@ -72,7 +72,12 @@ function stripeMethod(spec) { urlData[param] = args.shift(); } - var data = encode(utils.getDataFromArgs(args)); + var data; + try { + data = encode(utils.getDataFromArgs(args)); + } catch(e) { + reject(e); + } var opts = utils.getOptionsFromArgs(args); if (args.length) { diff --git a/lib/utils.js b/lib/utils.js index b936bb35da..a435683557 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -53,14 +53,30 @@ var utils = module.exports = { /** * Return the data argument from a list of arguments */ - getDataFromArgs: function(args) { - if (args.length > 0) { - if (isPlainObject(args[0]) && !utils.isOptionsHash(args[0])) { + getDataFromArgs: function(args) { + if (args.length < 1 || !isPlainObject(args[0])) { + return {}; + } + + if (!utils.isOptionsHash(args[0])) { return args.shift(); } - } - return {}; - }, + + var argKeys = Object.keys(args[0]); + + var optionKeysInArgs = argKeys.filter(function(key) { + return ['api_key', 'idempotency_key', 'stripe_account'].indexOf(key) > -1; + }); + + if (argKeys.length > optionKeysInArgs.length) { + throw new Error( + 'Stripe: Options found in arguments (' + optionKeysInArgs.join(', ') + '). Did you mean to pass an options ' + + 'object? See https://github.com/stripe/stripe-node/wiki/Passing-Options.' + ); + } + + return {}; + }, /** * Return the options hash from a list of arguments diff --git a/test/utils.spec.js b/test/utils.spec.js index 323531f974..11c290c49c 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -106,11 +106,15 @@ describe('utils', function() { expect(utils.getDataFromArgs(args)).to.deep.equal({}); expect(args.length).to.equal(2); }); - it('ignores an options hash', function() { + it('ignores a hash with only options', function() { var args = [{api_key: 'foo'}]; expect(utils.getDataFromArgs(args)).to.deep.equal({}); expect(args.length).to.equal(1); }); + it('throws an error if the hash contains both data and options', function() { + var args = [{foo: 'bar', api_key: 'foo', idempotency_key: 'baz'}]; + expect(function() { utils.getDataFromArgs(args); }).to.throw(/Options found in arguments/); + }); it('finds the data', function() { var args = [{foo: 'bar'}, {api_key: 'foo'}]; expect(utils.getDataFromArgs(args)).to.deep.equal({foo: 'bar'});