From 3532ef103ae97bfa15b3ac79c4c397005f0e02ac Mon Sep 17 00:00:00 2001 From: C J Silverio Date: Wed, 14 Nov 2012 14:09:07 -0800 Subject: [PATCH] Initial import of recurring into git. --- .gitignore | 3 + .jshintrc | 99 +++++ LICENSE | 7 + README.md | 50 +++ lib/index.js | 16 + lib/parser.js | 95 +++++ lib/recurly.js | 577 ++++++++++++++++++++++++++++++ lib/signer.js | 62 ++++ package.json | 19 + test/config.sample.json | 3 + test/fixtures/account.xml | 21 ++ test/fixtures/billing_info_cc.xml | 22 ++ test/fixtures/billing_info_pp.xml | 18 + test/fixtures/plan.xml | 32 ++ test/fixtures/plan_addon.xml | 15 + test/fixtures/plans.xml | 111 ++++++ test/fixtures/single-item.xml | 7 + test/fixtures/subscription.xml | 28 ++ test/fixtures/transactions.xml | 51 +++ test/fixtures/types.xml | 27 ++ test/helpers.js | 28 ++ test/test-01-parser.js | 143 ++++++++ test/test-02-api.js | 201 +++++++++++ test/test-03-signer.js | 93 +++++ 24 files changed, 1728 insertions(+) create mode 100644 .gitignore create mode 100644 .jshintrc create mode 100644 LICENSE create mode 100644 README.md create mode 100644 lib/index.js create mode 100644 lib/parser.js create mode 100644 lib/recurly.js create mode 100644 lib/signer.js create mode 100644 package.json create mode 100644 test/config.sample.json create mode 100644 test/fixtures/account.xml create mode 100644 test/fixtures/billing_info_cc.xml create mode 100644 test/fixtures/billing_info_pp.xml create mode 100644 test/fixtures/plan.xml create mode 100644 test/fixtures/plan_addon.xml create mode 100644 test/fixtures/plans.xml create mode 100644 test/fixtures/single-item.xml create mode 100644 test/fixtures/subscription.xml create mode 100644 test/fixtures/transactions.xml create mode 100644 test/fixtures/types.xml create mode 100644 test/helpers.js create mode 100644 test/test-01-parser.js create mode 100644 test/test-02-api.js create mode 100644 test/test-03-signer.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a4e6641 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +node_modules +config.json diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..d7239fc --- /dev/null +++ b/.jshintrc @@ -0,0 +1,99 @@ +{ + // == Enforcing Options =============================================== + // + // These options tell JSHint to be more strict towards your code. Use + // them if you want to allow only a safe subset of JavaScript, very + // useful when your codebase is shared with a big number of developers + // with different skill levels. + + "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.). + "curly" : false, // Require {} for every new block or scope. + "eqeqeq" : true, // Require triple equals i.e. `===`. + "forin" : true, // Tolerate `for in` loops without `hasOwnPrototype`. + "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );` + "latedef" : true, // Prohibit variable use before definition. + "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`. + "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`. + "noempty" : true, // Prohibit use of empty blocks. + "nonew" : true, // Prohibit use of constructors for side-effects. + "plusplus" : false, // Prohibit use of `++` & `--`. + "regexp" : false, // Prohibit `.` and `[^...]` in regular expressions. + "undef" : true, // Require all non-global variables be declared before they are used. + "strict" : false, // Require `use strict` pragma in every file. + "trailing" : true, // Prohibit trailing whitespaces. + + // == Relaxing Options ================================================ + // + // These options allow you to suppress certain types of warnings. Use + // them only if you are absolutely positive that you know what you are + // doing. + + "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons). + "boss" : true, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments. + "debug" : false, // Allow debugger statements e.g. browser breakpoints. + "eqnull" : false, // Tolerate use of `== null`. + "es5" : true, // Allow EcmaScript 5 syntax. + "esnext" : false, // Allow ES.next specific features such as `const` and `let`. + "evil" : false, // Tolerate use of `eval`. + "expr" : true, // Tolerate `ExpressionStatement` as Programs. + "funcscope" : false, // Tolerate declarations of variables inside of control structures while accessing them later from the outside. + "globalstrict" : false, // Allow global "use strict" (also enables 'strict'). + "iterator" : false, // Allow usage of __iterator__ property. + "lastsemic" : false, // Tolerat missing semicolons when the it is omitted for the last statement in a one-line block. + "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons. + "laxcomma" : true, // Suppress warnings about comma-first coding style. + "loopfunc" : false, // Allow functions to be defined within loops. + "multistr" : false, // Tolerate multi-line strings. + "onecase" : false, // Tolerate switches with just one case. + "proto" : false, // Tolerate __proto__ property. This property is deprecated. + "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`. + "scripturl" : false, // Tolerate script-targeted URLs. + "smarttabs" : true, // Tolerate mixed tabs and spaces when the latter are used for alignmnent only. + "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`. + "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`. + "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`. + "validthis" : false, // Tolerate strict violations when the code is running in strict mode and you use this in a non-constructor function. + + // == Environments ==================================================== + // + // These options pre-define global variables that are exposed by + // popular JavaScript libraries and runtime environments—such as + // browser or node.js. + + "browser" : false, // Standard browser globals e.g. `window`, `document`. + "couch" : false, // Enable globals exposed by CouchDB. + "devel" : false, // Allow development statements e.g. `console.log();`. + "dojo" : false, // Enable globals exposed by Dojo Toolkit. + "jquery" : false, // Enable globals exposed by jQuery JavaScript library. + "mootools" : false, // Enable globals exposed by MooTools JavaScript framework. + "node" : true, // Enable globals available when code is running inside of the NodeJS runtime environment. + "nonstandard" : false, // Define non-standard but widely adopted globals such as escape and unescape. + "prototypejs" : false, // Enable globals exposed by Prototype JavaScript framework. + "rhino" : false, // Enable globals available when your code is running inside of the Rhino runtime environment. + "wsh" : false, // Enable globals available when your code is running as a script for the Windows Script Host. + + // == JSLint Legacy =================================================== + // + // These options are legacy from JSLint. Aside from bug fixes they will + // not be improved in any way and might be removed at any point. + + "nomen" : false, // Prohibit use of initial or trailing underbars in names. + "onevar" : false, // Allow only one `var` statement per function. + "passfail" : false, // Stop on first error. + "white" : false, // Check against strict whitespace and indentation rules. + + // == Undocumented Options ============================================ + // + // While I've found these options in [example1][2] and [example2][3] + // they are not described in the [JSHint Options documentation][4]. + // + // [4]: http://www.jshint.com/options/ + + "maxerr" : 100, // Maximum errors before stopping. + "predef" : [ // Extra globals. + //"exampleVar", + //"anotherCoolGlobal", + //"iLoveDouglas" + ], + "indent" : 4 // Specify indentation spacing +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a1fafaf --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2012 C J Silverio + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f8e12c5 --- /dev/null +++ b/README.md @@ -0,0 +1,50 @@ +A node client for [recurly](https://recurly.com)'s v2 api, with support for secure parameter signing for [recurly.js](https://docs.recurly.com/recurlyjs) embedded forms. + +__This code is still in development and is not ready for production use.__ In particular, the example usage below might radically change. + +## Recurly API + +A work in progress. + +```javascript +var recurly = require('recurring'); +recurly.setAPIKey('your-api-key'); + +var account = new recurly.Account('account-id'); +account.fetch(function(err) +{ + account.fetchSubscriptions(function(err, subscriptions) + { + console.log(subscriptions[0].plan); + }); +}); + +recurly.Account.all(function(accounts) +{ + // accounts is an array containing all customer accounts +}); + +recurly.Plan.all(function(plans) +{ + // plans is an array containing all plans set up for your account +}); + +``` + +## SignedQuery + +This provides the back-end support for signing parameters for forms embedded using recurly.js. See Recurly's [signature documentation](https://docs.recurly.com/api/recurlyjs/signatures) for details on which parameters must be signed for each form type. + +```javascript +var recurly = require('recurring'); + +var signer = new recurly.SignedQuery('your-private-api-key'); +signer.set('account', { account_code: 'account-id' }); +var signedParameters = signer.toString(); +``` + +The `nonce` & `timestamp` parameters are generated for you if you don't provide them. The nonce is created using [node-uuid](https://github.com/broofa/node-uuid). + +## License + +MIT. See accompanying LICENSE file. \ No newline at end of file diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..affc82f --- /dev/null +++ b/lib/index.js @@ -0,0 +1,16 @@ +var recurly = require('./recurly'); + +exports.setAPIKey = recurly.setAPIKey; +exports.Account = recurly.Account; +exports.Addon = recurly.Addon; +exports.BillingInfo = recurly.BillingInfo; +exports.Coupon = recurly.Coupon; +exports.FormResponseToken = recurly.FormResponseToken; +exports.Invoice = recurly.Invoice; +exports.Plan = recurly.Plan; +exports.Subscription = recurly.Subscription; +exports.Transaction = recurly.Transaction; + +exports.SignedQuery = require('./signer').SignedQuery; +exports.createParser = require('./parser').createParser; + diff --git a/lib/parser.js b/lib/parser.js new file mode 100644 index 0000000..f414ca6 --- /dev/null +++ b/lib/parser.js @@ -0,0 +1,95 @@ +/*jshint node:true */ + +var + assert = require('assert'), + querystring = require('querystring'), + xml2js = require('xml2js') + ; + + +function parseTypes(input) +{ + var result = {}; + var item, key; + var keys = Object.keys(input); + var mode = 'normal'; + + try + { + for (var i = 0; i < keys.length; i++) + { + key = keys[i]; + item = input[key]; + + if (mode === 'array') + { + mode = 'normal'; + + if (Array.isArray(item)) + { + result = []; + for (var j = 0; j < item.length; j++) + result.push(parseTypes(item[j])); + } + else + result = [ parseTypes(item) ]; + + } + else if (typeof item === 'object') + { + if (item['#'] && item.type && (item.type === 'datetime')) + result[key] = new Date(item['#']); + else if (item['#'] && item.type && (item.type === 'integer')) + result[key] = parseInt(item['#'], 10); + else if (item['#'] && item.type && (item.type === 'boolean')) + result[key] = ( item.type['#'] === 'true' ? true : false ); + else if (item.nil && (item.nil === 'nil')) + result[key] = ''; + else + result[key] = parseTypes(item); + } + else if ((typeof item === 'string') && (key === 'type')) + mode = 'array'; // this is pure hackery + else + result[key] = item; + } + } + catch (exception) + { + // console.error('recurly.parseTypes: @ ' + key + ' ' + item + ' ' + JSON.stringify(exception)); + result = input; + } + + return result; +} + +function RecurlyParser() +{ + this.parser = new xml2js.Parser( + { + mergeAttrs: true, + attrkey: '#', + charkey: '#', + explicitArray: false, + explicitRoot: false, + }); +} + +RecurlyParser.prototype.parseXML = function(input, callback) +{ + this.parser.parseString(input, function (err, json) + { + if (err) + return callback(err); + + callback(null, parseTypes(json)); + }); +}; + +function createParser() +{ + return new RecurlyParser(); + +} + +exports.createParser = createParser; diff --git a/lib/recurly.js b/lib/recurly.js new file mode 100644 index 0000000..2d74b94 --- /dev/null +++ b/lib/recurly.js @@ -0,0 +1,577 @@ +/*jshint node:true */ + +var + _ = require('lodash'), + data2xml = require('data2xml'), + path = require('path'), + querystring = require('querystring'), + request = require('request'), + rparser = require('./parser.js'), + util = require('util') + ; + +//---------------------------------------------------------------------------------------- + +var + ENDPOINT = 'https://api.recurly.com/v2/', + APIKEY, AUTH_BASIC, + parser = rparser.createParser() + ; + +function setAPIKey(key) +{ + APIKEY = key; + AUTH_BASIC = 'Basic ' + (new Buffer(APIKEY + ':', "ascii")).toString('base64'); +} + +//---------------------------------------------------------------------------------------- + +function RecurlyData() { } + +RecurlyData.base_options = function () +{ + return { + headers: + { + 'Accept': 'application/xml', + 'Authorization': AUTH_BASIC, + }, + }; +}; + +RecurlyData.get = function(uri, queryargs, callback) +{ + var options = RecurlyData.base_options(); + options.uri = uri; + + request(options, function(err, response, body) + { + if (err) + { + console.error('recurly.get', 'request.get() error ' + JSON.stringify(err)); + return callback(err); + } + if (response.statusCode !== 200) + return callback(new Error('statusCode: ' + response.statusCode)); + + parser.parseXML(body, function(err, result) + { + if (err) + { + console.error('recurly.get', 'xml parsing error ' + JSON.stringify(err)); + return callback(err, response.headers, {}); + } + + callback(null, response.headers, result); + }); + }); +}; + +RecurlyData.put = function(uri, postargs, callback) +{ + var options = RecurlyData.base_options(); + options.uri = uri; + options.body = postargs; + + request.put(options, function(err, response, body) + { + if (err) + { + console.error('recurly.put', 'request.put() error ' + JSON.stringify(err)); + return callback(err); + } + if (response.statusCode !== 200) + return callback(new Error('statusCode: ' + response.statusCode)); + + parser.parseXML(body, function(err, result) + { + if (err) + { + console.error('recurly.put', 'xml parsing error ' + JSON.stringify(err)); + return callback(err, response.headers, {}); + } + + callback(null, response.headers, result); + }); + }); +}; + +RecurlyData.post = function(uri, postargs, callback) +{ + callback('unimplemented'); +}; + +RecurlyData.delete = function(uri, postargs, callback) +{ + callback('unimplemented'); +}; + +RecurlyData.prototype.serialize = function() +{ + return data2xml(this.properties); +}; + +RecurlyData.prototype.inflate = function(json) +{ + if (typeof json !== 'object') + { + console.log(json); + return; + } + + var keys = Object.keys(json); + for (var i = 0; i < keys.length; i++) + { + var prop = keys[i]; + if ('a' === prop) + { + // Hackery. 'a' is a list of named anchors. We treat them specially. + this.a = {}; + var anchors = Object.keys(json[prop]); + for (var j = 0; j < anchors.length; j++) + { + var anchor = json[prop][anchors[j]]; + this.a[anchor.name] = anchor; + } + } + else + this[prop] = json[prop]; + } +}; + +RecurlyData.prototype.fetch = function(callback) +{ + var self = this; + + if (!self.href) + throw(new Error('cannot fetch a record without an href')); + + RecurlyData.get(self.href, {}, function(err, headers, payload) + { + if (err) + return callback(err); + self.inflate(payload); + callback(); + }); +}; + +RecurlyData.fetchAll = function(Model, uri, callback) +{ + var result = []; + var done = false; + var total = -1; + + var finished = function(err) + { + callback(err, result); + }; + + var continuer = function(err, headers, records) + { + if (err) + return finished(err); + + // link header in response points to next page of results + // X-Records header says how many total + if (total < 0) + total = parseInt(headers['x-records'], 10); + + _.each(records, function(record) + { + var item = new Model(); + item.inflate(record); + result.push(item); + }); + + if ((result.length >= total) || !headers.link ) + return finished(null); + + uri = headers.link; + RecurlyData.get(uri, { per_page: 200 }, continuer); + }; + + RecurlyData.get(uri, { per_page: 200 }, continuer); +}; + +//---------------------------------------------------------------------------------------- + +RecurlyData.buildPrototype = function(options) +{ + var constructor = function() + { + this.properties = {}; + }; + util.inherits(constructor, RecurlyData); + + for (var i = 0; i < options.properties.length; i++) + RecurlyData.addProperty(constructor, options.properties[i]); + constructor.prototype.proplist = options.properties; + + constructor.ENDPOINT = ENDPOINT + options.plural; + constructor.plural = options.plural; // used when generating xml + constructor.singular = options.singular; + + constructor.prototype.__defineGetter__('id', function() { return this.properties[options.idField]; }); + var idSetter = function() + { + var newval = arguments['0']; + this.properties[options.idField] = newval; + if (!this.href) + this.href = constructor.ENDPOINT + '/' + newval; + }; + constructor.prototype.__defineSetter__('id', idSetter); + constructor.prototype.__defineSetter__(options.idField, idSetter); + + return constructor; +}; + +RecurlyData.addProperty = function(constructor, propname) +{ + var getterFunc = function() { return this.properties[propname]; }; + var setterFunc = function() + { + var newval = arguments['0']; + this.properties[propname] = newval; + }; + + constructor.prototype.__defineGetter__(propname, getterFunc); + constructor.prototype.__defineSetter__(propname, setterFunc); +}; + +// ---------------------------------------------------------------------- + +var Account = RecurlyData.buildPrototype( +{ + properties: [ 'account_code', 'state', 'username', 'email', 'first_name', 'last_name', 'accept_language', 'created_at'], + idField: 'account_code', + plural: 'accounts', + singular: 'account' +}); + +Account.all = function(state, callback) +{ + if (typeof state === 'function') + { + callback = state; + state = 'active'; + } + + if (Account.__all && Account.__all[state]) + return callback(Account.__all[state]); + + if (!Account.__all) + Account.__all = {}; + if (!Account.__all[state]) + Account.__all[state] = {}; + + RecurlyData.fetchAll(Account, Account.ENDPOINT, function(err, results) + { + var cache = Account.__all[state]; + _.each(results, function(account) + { + cache[account.account_code] = account; + }); + callback(cache); + }); +}; + +var Subscription; // forward reference + +Account.prototype.fetchSubscriptions = function(callback) +{ + var self = this; + if (!self.cache) + self.cache = { billingInfo: null, subscriptions: [] }; + + if (self.cache.subscriptions.length) + return callback(null, self.cache.subscriptions); + + if (!self.subscriptions || !self.subscriptions.href) + return callback(null, self.cache.subscriptions); + + RecurlyData.fetchAll(Subscription, this.subscriptions.href, function(err, results) + { + if (err) + return callback(err); + + self.cache.subscriptions = results; + callback(null, self.cache.subscriptions); + }); +}; + +var BillingInfo; // forward reference + +Account.prototype.fetchBillingInfo = function(callback) +{ + var self = this; + if (!self.cache) + self.cache = { billingInfo: null, subscriptions: [] }; + + if (self.cache.billingInfo) + return callback(null, self.cache.billingInfo); + + var binfo = new BillingInfo(); + binfo.href = self.billing_info.href; + + binfo.fetch(function(err) + { + if (err) + return callback(err); + + self.cache.billingInfo = binfo; + callback(null, self.cache.billingInfo); + }); +}; + +// ---------------------------------------------------------------------- + +var Addon = RecurlyData.buildPrototype({ + properties: [ 'add_on_code', 'name', 'display_quantity_on_hosted_page', 'default_quantity', 'unit_amount_in_cents', 'created_at', 'href'], + idField: 'add_on_code', + plural: 'add_ons', + singular: 'add_on' +}); + +// ---------------------------------------------------------------------- + +var BillingInfo = RecurlyData.buildPrototype({ + properties: [ 'account', 'first_name', 'last_name', 'company', + 'address1', 'address2', 'city', 'state', 'zip', 'country', 'phone', + 'vat_number', 'ip_address', 'ip_address_country', + 'card_type', 'year', 'month', 'first_six', 'last_four', 'href' ], + idField: 'add_on_code', + plural: 'billing_info', + singular: 'billing_info' +}); + +BillingInfo.prototype.__defineGetter__('account_code', function() { return this.account_code; }); +BillingInfo.prototype.__defineSetter__('account_code', function(account_code) +{ + this._account_code = account_code; + if (!this.href) + this.href = Account.ENDPOINT + '/' + account_code + '/billing_info'; +}); + +// ---------------------------------------------------------------------- +// TODO + +var Coupon = RecurlyData.buildPrototype({ + properties: [ 'href' ], + idField: 'add_on_code', + plural: 'coupons', + singular: 'coupon' +}); + +// ---------------------------------------------------------------------- +// TODO + +var Invoice = RecurlyData.buildPrototype({ + properties: [ 'href' ], + idField: 'add_on_code', + plural: 'invoices', + singular: 'invoice' +}); + +// ---------------------------------------------------------------------- +// A list of plans associated with this recurly payment provider. + +var Plan = RecurlyData.buildPrototype({ + properties: [ 'add_ons', 'plan_code', 'name', 'description', 'success_url', + 'cancel_url', 'display_donation_amounts', 'display_quantity', + 'display_phone_number', 'bypass_hosted_confirmation', 'unit_name', + 'payment_page_tos_link', 'plan_interval_length', + 'plan_interval_unit', 'trial_interval_length', + 'trial_interval_unit', 'accounting_code', 'created_at', + 'unit_amount_in_cents', 'setup_fee_in_cents' ], + idField: 'plan_code', + plural: 'plans', + singular: 'plan' +}); + +Plan.all = function(callback) +{ + if (Plan.__all) + return callback(Plan.__all); + + RecurlyData.fetchAll(Plan, Plan.ENDPOINT, function(err, results) + { + Plan.__all = {}; + _.each(results, function(plan) + { + Plan.__all[plan.plan_code] = plan; + }); + callback(Plan.__all); + }); +}; + +Plan.prototype.fetchAddOns = function(callback) +{ + if (this._addons) + return callback(this._addons); + + RecurlyData.fetchAll(Addon, this.addons, function(err, results) + { + this.addons = {}; + _.each(results, function(addon) + { + this._addons[addon.add_on_code] = addon; + }); + callback(this._addons); + }); +}; + +// ---------------------------------------------------------------------- + +var Subscription = RecurlyData.buildPrototype({ + properties: [ 'href', 'account', 'plan', 'uuid', 'state', + 'unit_amount_in_cents', 'currency', 'quantity', 'activated_at', + 'canceled_at', 'expires_at', 'current_period_started_at', + 'current_period_ends_at', 'trial_started_at', 'trial_ends_at', + 'subscription_add_ons', ], + idField: 'uuid', + plural: 'subscriptions', + singular: 'subscription' +}); + +Subscription.prototype.__defineGetter__('account_id', function() +{ + if (!this.account) + return undefined; + + // The account property points to a hash with an href that can be used to fetch + // the account, but sometimes I want the id. + this._account_id = this.account.href.match(/\/([^\/]*)$/)[1]; + return this._account_id; +}); + +Subscription.prototype.update = function(options, callback) +{ + if (!options.timeframe) + throw(new Error('subscription update must include "timeframe" parameter')); + if (!this.href) + throw(new Error('cannot update a subscription without an href ' + self.id)); + + var self = this; + var body = data2xml(Subscription.singular, options); + + RecurlyData.put(self.href, body, function(err, respHeaders, payload) + { + self.inflate(payload); + console.log(self); + + callback(null, self); + }); +}; + +Subscription.prototype.serialize = function() +{ + return data2xml(Subscription.singular, this.properties) +}; + +Subscription.prototype.cancel = function(callback) +{ + // this.a.cancel object + callback('unimplemented'); +}; + +Subscription.prototype.terminate = function(callback) +{ + // this.a.terminate object + callback('unimplemented'); +}; + +Subscription.prototype.postpone = function(callback) +{ + // this.a.postpone object + + callback('unimplemented'); +}; + +// ---------------------------------------------------------------------- + +var Transaction = RecurlyData.buildPrototype({ + properties: [ 'href', 'account', 'invoice', 'subscription', 'uuid', 'action', + 'amount_in_cents', 'tax_in_cents', 'currency', 'status', + 'reference', 'test', 'voidable', 'refundable', 'cvv_result', + 'avs_result', 'avs_result_street', 'avs_result_postal', + 'created_at', 'details', ], + idField: 'uuid', + plural: 'transactions', + singular: 'transaction' +}); + +Transaction.prototype.refund = function(callback) +{ + callback('unimplemented'); +}; + +// ---------------------------------------------------------------------- + +function FormResponseToken(token, transactionType) +{ + this.kind = transactionType; + switch (transactionType) + { + case 'billing-info': + this.builder = BillingInfo; + break; + + case 'one-time-transaction': + this.builder = Transaction; + break; + + case 'subscription': + this.builder = Subscription; + break; + + default: + throw(new Error('unknown recurly transaction type ' + transactionType)); + } + + this.token = token; +} +util.inherits(FormResponseToken, RecurlyData); + +FormResponseToken.prototype.process = function(callback) +{ + // Fetch the transaction response pointed to by this token & return the + // appropriate object + + var self = this; + var uri = ENDPOINT + 'recurly_js/result/' + this.token; + + RecurlyData.get(uri, {}, function(err, headers, payload) + { + if (err) + callback(err); + + if (!payload || (typeof payload !== 'object')) + return callback(null, payload); + + try + { + var result = new self.builder(); + result.inflate(payload); + callback(null, result); + } + catch (ex) + { + callback(null, payload); + } + }); +}; + + +// ---------------------------------------------------------------------- + +exports.Account = Account; +exports.Addon = Addon; +exports.BillingInfo = BillingInfo; +exports.Coupon = Coupon; +exports.Invoice = Invoice; +exports.Plan = Plan; +exports.Subscription = Subscription; +exports.Transaction = Transaction; +exports.FormResponseToken = FormResponseToken; +exports.setAPIKey = setAPIKey; + diff --git a/lib/signer.js b/lib/signer.js new file mode 100644 index 0000000..942cb19 --- /dev/null +++ b/lib/signer.js @@ -0,0 +1,62 @@ +// Copyright 2010-2012 Voxer IP LLC. All rights reserved. +// https://docs.recurly.com/api/recurlyjs/signatures + +var + crypto = require('crypto'), + qs = require('qs'), + uuid = require('node-uuid') + ; + +function SignedQuery(key) +{ + this.params = {}; + this.key = key; +} + +SignedQuery.prototype.serialize = function() +{ + if (!this.qs) + { + if (!this.params.nonce) + this.params.nonce = uuid.v4(); + if (!this.params.timestamp) + this.params.timestamp = Math.ceil(Date.now() / 1000); + + // alphabetize keys + var tmp = {}; + var keys = Object.keys(this.params).sort(); + for (var i = 0; i < keys.length; i++) + tmp[keys[i]] = this.params[keys[i]]; + this.params = tmp; + + this.qs = qs.stringify(this.params); + } + + return this.qs; +}; + +SignedQuery.prototype.set = function(key, value) +{ + this.qs = null; + + if ((typeof key === 'object') && !value) + this.params = key; + else + this.params[key] = value; +}; + +SignedQuery.prototype.HMAC = function(data) +{ + var hmac = crypto.createHmac('sha1', this.key); + hmac.update(data); + return hmac.digest('hex'); +}; + +SignedQuery.prototype.toString = function() +{ + var query = encodeURI(this.serialize()); + return this.HMAC(query) + '|' + query; +}; + + +exports.SignedQuery = SignedQuery; diff --git a/package.json b/package.json new file mode 100644 index 0000000..aca4af0 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name" : "recurring", + "description" : "a recurly v2 api client for node.js", + "version" : "0.0.1", + "author" : "C J Silverio", + "license" : "MIT", + "keywords" : ["recurly", "payments" ], + "dependencies" : { + "data2xml": "0.5.x", + "lodash": "0.8.x", + "node-uuid": "1.3.x", + "qs": "0.5.x", + "request": "2.11.x", + "xml2js": "0.2.x" + }, + "main" : "lib", + "devDependencies" : { "mocha": "1.7.x", "chai": "1.3.x" }, + "scripts" : { "test": "mocha -R spec test/test*.js" } +} diff --git a/test/config.sample.json b/test/config.sample.json new file mode 100644 index 0000000..b05de7f --- /dev/null +++ b/test/config.sample.json @@ -0,0 +1,3 @@ +{ + "apikey": "your-recurly-api-key-here" +} diff --git a/test/fixtures/account.xml b/test/fixtures/account.xml new file mode 100644 index 0000000..1651860 --- /dev/null +++ b/test/fixtures/account.xml @@ -0,0 +1,21 @@ + + + + + + + + + + 1 + active + + verena@example.com + Verena + Example + + a92468579e9c4231a6c0031c4716c01d + 2011-10-25T12:00:00 + + + diff --git a/test/fixtures/billing_info_cc.xml b/test/fixtures/billing_info_cc.xml new file mode 100644 index 0000000..0d18a89 --- /dev/null +++ b/test/fixtures/billing_info_cc.xml @@ -0,0 +1,22 @@ + + + + Verena + Example + + 123 Main St. + + San Francisco + CA + 94105 + US + + US1234567890 + 127.0.0.1 + US + Visa + 2015 + 11 + 411111 + 1111 + diff --git a/test/fixtures/billing_info_pp.xml b/test/fixtures/billing_info_pp.xml new file mode 100644 index 0000000..ac70a13 --- /dev/null +++ b/test/fixtures/billing_info_pp.xml @@ -0,0 +1,18 @@ + + + + Verena + Example + + 123 Main St. + + San Francisco + CA + 94105 + US + + US1234567890 + 127.0.0.1 + US + B-1234567890 + diff --git a/test/fixtures/plan.xml b/test/fixtures/plan.xml new file mode 100644 index 0000000..2dd102f --- /dev/null +++ b/test/fixtures/plan.xml @@ -0,0 +1,32 @@ + + + + + gold + Gold plan + + + + false + false + false + false + unit + + 1 + months + 0 + days + + 2011-04-19T07:00:00Z + + 1000 + 800 + + + 6000 + 4500 + + + + diff --git a/test/fixtures/plan_addon.xml b/test/fixtures/plan_addon.xml new file mode 100644 index 0000000..c0c9682 --- /dev/null +++ b/test/fixtures/plan_addon.xml @@ -0,0 +1,15 @@ + + + + + ipaddresses + IP Addresses + false + 1 + + 200 + + 2011-06-28T12:34:56Z + + + diff --git a/test/fixtures/plans.xml b/test/fixtures/plans.xml new file mode 100644 index 0000000..73651f3 --- /dev/null +++ b/test/fixtures/plans.xml @@ -0,0 +1,111 @@ + + + + + kip-test-voxer-plan + kip-test-voxer-plan + Test plan for the kip.voxer.com cluster + https://kip-01.voxer.com:10443/business/billing/success/{{account_code}}/{{plan_code}} + https://kip-01.voxer.com:10443/business/billing/cancel/{{account_code}}/{{plan_code}} + false + false + false + false + unit + + 1 + months + 0 + days + + + 2012-09-26T18:09:17Z + + 0 + + + 0 + + + + + stage-test-plan + stage-test-plan + test plan for the stage.voxer.com cluster + https://stage-06.voxer.com:10443/business/billing/success/{{account_code}}/{{plan_code}} + https://stage-06.voxer.com:10443/business/billing/cancel/{{account_code}}/{{plan_code}} + false + false + false + false + unit + + 1 + months + 0 + days + + + 2012-09-26T18:08:03Z + + 0 + + + 0 + + + + + ceej-test-voxer-plan + ceej-test-voxer-plan + test plan for the ceej.voxer.com cluster + https://ceej-01.voxer.com:10443/business/billing/success/{{account_code}}/{{plan_code}} + https://ceej-01.voxer.com:10443/business/billing/cancel/{{account_code}}/{{plan_code}} + false + false + false + false + unit + + 1 + months + 0 + days + + + 2012-09-26T18:06:46Z + + 0 + + + 0 + + + + + voxer-test + voxer-test + This is the test plan used by Voxer development boxes. + + + false + false + false + false + unit + + 1 + months + 0 + days + + + 2012-09-24T23:13:40Z + + 0 + + + 0 + + + diff --git a/test/fixtures/single-item.xml b/test/fixtures/single-item.xml new file mode 100644 index 0000000..f12a926 --- /dev/null +++ b/test/fixtures/single-item.xml @@ -0,0 +1,7 @@ + + + + The Only Blort + Infinite + + diff --git a/test/fixtures/subscription.xml b/test/fixtures/subscription.xml new file mode 100644 index 0000000..da12ce2 --- /dev/null +++ b/test/fixtures/subscription.xml @@ -0,0 +1,28 @@ + + + + + + gold + Gold plan + + 44f83d7cba354d5b84812419f923ea96 + active + 800 + EUR + 1 + 2011-05-27T07:00:00Z + + + 2011-06-27T07:00:00Z + 2010-07-27T07:00:00Z + + + + + + + + + + diff --git a/test/fixtures/transactions.xml b/test/fixtures/transactions.xml new file mode 100644 index 0000000..d280dfd --- /dev/null +++ b/test/fixtures/transactions.xml @@ -0,0 +1,51 @@ + + + + + + + a13acd8fe4294916b79aec87b7ea441f + purchase + 1000 + 0 + USD + success + + true + true + true + + + + + 2011-06-27T12:34:56Z +
+ + verena100 + Verena + Example + + verena@test.com + + + + + + + + + + + + Visa + 2015 + 11 + 411111 + 1111 + + +
+
+ + + diff --git a/test/fixtures/types.xml b/test/fixtures/types.xml new file mode 100644 index 0000000..0776c51 --- /dev/null +++ b/test/fixtures/types.xml @@ -0,0 +1,27 @@ + + + + + gold + + false + 2 + 2011-04-19T07:00:00Z + + 1000 + 800 + + + + + silver + + true + 3 + 2012-10-16T08:00:00Z + + 1000 + 800 + + + diff --git a/test/helpers.js b/test/helpers.js new file mode 100644 index 0000000..4169c90 --- /dev/null +++ b/test/helpers.js @@ -0,0 +1,28 @@ +// Helpers for test suites. + +var + fs = require('fs'), + path = require('path') + ; + +var testdir = __dirname; +if (path.basename(testdir) !== 'test') + testdir = path.join(testdir, 'test'); + +function readFixture(fixture) +{ + var fpath = path.join(testdir, 'fixtures', fixture); + var data = fs.readFileSync(fpath, 'utf8'); + return data; +} + +function readTestConfig() +{ + // The tests require a config file with an API key. + var fpath = path.join(testdir, 'config.json'); + var data = fs.readFileSync(fpath, 'utf8'); + return JSON.parse(data); +} + +exports.readFixture = readFixture; +exports.readTestConfig = readTestConfig; diff --git a/test/test-01-parser.js b/test/test-01-parser.js new file mode 100644 index 0000000..0a00bc3 --- /dev/null +++ b/test/test-01-parser.js @@ -0,0 +1,143 @@ +/*global describe:true, it:true, before:true, after:true */ + +var + chai = require('chai'), + assert = chai.assert, + expect = chai.expect, + should = chai.should() + ; + +var + helpers = require('./helpers'), + parser = require('../lib/parser'), + util = require('util') + ; + + +// ---------------------------------------------------------------------- + +var rparser; + +before(function() +{ + rparser = parser.createParser(); +}); + + +describe('recurly xml parser', function() +{ + var data = helpers.readFixture('types.xml'); + var typesResult; + + it('can parse basic data types', function(done) + { + rparser.parseXML(data, function(err, result) + { + should.not.exist(err); + typesResult = result; + done(); + }); + }); + + it('can parse subarrays', function() + { + typesResult.should.be.an('array'); + typesResult.length.should.equal(2); + }); + + it('can parse single-item subarrays', function(done) + { + var blortdata = helpers.readFixture('single-item.xml'); + rparser.parseXML(blortdata, function(err, result) + { + should.not.exist(err); + result.should.be.an('array'); + result.length.should.equal(1); + result[0].should.be.an('object'); + result[0].should.have.property('name'); + result[0].name.should.equal('The Only Blort'); + done(); + }); + }); + + it('can parse boolean types', function() + { + var item = typesResult[0]; + item.boolean_value.should.be.a('boolean'); + item.boolean_value.should.equal(false); + }); + + it('can parse integer types', function() + { + var item = typesResult[1]; + item.integer_value.should.be.a('number'); + item.integer_value.should.equal(3); + }); + + it('can parse nil types', function() + { + var item = typesResult[0]; + item.should.have.property('nil_value'); + item.nil_value.should.equal(''); + }); + + it('can parse datetype types', function() + { + var item = typesResult[0]; + item.datetime_value.should.be.a('date'); + item.datetime_value.toString().should.equal('Tue Apr 19 2011 00:00:00 GMT-0700 (PDT)'); + }); + + it('can parse subobjects', function() + { + var item = typesResult[1]; + + item.hash_value.should.be.an('object'); + item.hash_value.should.have.property('one'); + item.hash_value.should.have.property('two'); + item.hash_value.one.should.equal(1000); + }); + + it('can parse sample plan xml', function(done) + { + var data = helpers.readFixture('plans.xml'); + rparser.parseXML(data, function(err, result) + { + should.not.exist(err); + result.should.be.an('array'); + result.should.be.an('array'); + result.length.should.equal(4); + done(); + }); + }); + + it('can parse sample subscription xml', function(done) + { + var data = helpers.readFixture('subscription.xml'); + rparser.parseXML(data, function(err, result) + { + should.not.exist(err); + result.should.be.an('array'); + result.length.should.equal(1); + var subscription = result[0]; + subscription.should.have.property('uuid'); + subscription.uuid.should.equal('44f83d7cba354d5b84812419f923ea96'); + done(); + }); + }); + + it('can parse sample transaction xml', function(done) + { + var data = helpers.readFixture('transactions.xml'); + rparser.parseXML(data, function(err, result) + { + should.not.exist(err); + result.should.be.an('array'); + result.length.should.equal(1); + var transaction = result[0]; + transaction.should.have.property('uuid'); + transaction.uuid.should.equal('a13acd8fe4294916b79aec87b7ea441f'); + done(); + }); + }); +}); diff --git a/test/test-02-api.js b/test/test-02-api.js new file mode 100644 index 0000000..c23c4ca --- /dev/null +++ b/test/test-02-api.js @@ -0,0 +1,201 @@ +/*global describe:true, it:true, before:true, after:true */ + +var + chai = require('chai'), + assert = chai.assert, + expect = chai.expect, + should = chai.should() + ; + +var + helpers = require('./helpers'), + parser = require('../lib/parser'), + recurly = require('../lib/recurly'), + util = require('util') + ; + + +var config, rparser; +var plan, account, subscription; + +before(function() +{ + rparser = parser.createParser(); + config = helpers.readTestConfig(); + recurly.setAPIKey(config.apikey); +}); + +describe('Plan', function() +{ + var cached; + + it('can fetch all plans from the test Recurly account', function(done) + { + recurly.Plan.all(function(plans) + { + plans.should.be.an('object'); + var plan_codes = Object.keys(plans); + expect(plan_codes.length).to.be.above(0); + plan_codes[0].should.not.equal('undefined'); + cached = plan_codes; + done(); + }); + }); + + it('can fetch a single plan', function(done) + { + plan = new recurly.Plan(); + plan.id = cached[0]; + plan.fetch(function(err) + { + should.not.exist(err); + plan.href.length.should.be.above(0); + plan.should.have.property('name'); + plan.should.have.property('description'); + plan.name.should.be.ok; + plan.description.should.be.ok; + done(); + }); + }); +}); + +describe('Account', function() +{ + var cached; + + it('can fetch all accounts from the test Recurly account', function(done) + { + recurly.Account.all(function(accounts) + { + accounts.should.be.an('object'); + var uuids = Object.keys(accounts); + expect(uuids.length).to.be.above(0); + uuids[0].should.not.equal('undefined'); + cached = uuids; + done(); + }); + }); + + it('can fetch a single account', function(done) + { + account = new recurly.Account(); + account.id = cached[0]; + account.fetch(function(err) + { + should.not.exist(err); + account.should.be.an('object'); + done(); + }); + }); + + it('can serialize an account to xml', function(done) + { + var xml = account.serialize(); + console.log(xml); + done(); + }); +}); + +describe('Subscription', function() +{ + var cached, subscription; + + it('can fetch all subscriptions associated with an account', function(done) + { + account.fetchSubscriptions(function(err, subscriptions) + { + should.not.exist(err); + subscriptions.should.be.an('array'); + cached = subscriptions; + done(); + }); + }); + + it('can fetch a single subscription', function(done) + { + var uuid = cached[0].uuid; + subscription = new recurly.Subscription(); + subscription.id = uuid; + subscription.fetch(function(err) + { + should.not.exist(err); + subscription.account.should.be.an('object'); + subscription.account.should.have.property('href'); + subscription.account_id.should.equal(account.id); + + // TODO + done(); + }); + }); + + it('can create a subscription', function(done) + { + //subscription = new recurly.Subscription(); + //subscription.plan_code = ''; + //subscription.account = ''; + //subscription.currency = ''; + done(); + }); + + + it('throws an error when attempting to modify a subscription without a timeframe', function(done) + { + var wrong = function() + { + subscription.update({ inadequate: true }, function() {} ); + }; + expect(wrong).to.throw(Error); + + + done(); + }); + + it('can modify a subscription', function(done) + { + var mods = { + timeframe: 'now', + quantity: subscription.quantity + 3, + }; + + subscription.update(mods, function(err, updated) + { + should.not.exist(err); + updated.should.be.an('object'); + updated.quantity.should.equal(mods.quantity); + done(); + }); + }); + + it('can postpone a subscription', function(done) + { + done(); + }); + + it('can cancel a subscription', function(done) + { + done(); + }); + + it('can terminate a subscription', function(done) + { + done(); + }); +}); + + +describe('BillingInfo', function() +{ + var binfo; + + it('can fetch the billing info for an account', function(done) + { + account.fetchBillingInfo(function(err, info) + { + should.not.exist(err); + binfo = info; + // er, what to test? + done(); + }); + }); +}); + diff --git a/test/test-03-signer.js b/test/test-03-signer.js new file mode 100644 index 0000000..06ed42c --- /dev/null +++ b/test/test-03-signer.js @@ -0,0 +1,93 @@ +/*global describe:true, it:true, before:true, after:true */ + +var + chai = require('chai'), + assert = chai.assert, + expect = chai.expect, + should = chai.should() + ; + +var + helpers = require('./helpers'), + qs = require('qs'), + SignedQuery = require('../lib/signer').SignedQuery, + util = require('util') + ; + + +// These values are borrowed from recurly's own Ruby gem test suite, so I can +// be sure I'm generating the same things they are. +var testAPIKey = '0123456789abcdef0123456789abcdef'; +var testNonce = 'unique'; +var testTimestamp = 1329942896; + +// Fixture for a query used in several tests. +function transactionTestFixture() +{ + var query = new SignedQuery(testAPIKey); + query.set('account', {'account_code': '123'}); + query.set('nonce', testNonce); + query.set('timestamp', testTimestamp); + query.set('transaction', {'amount_in_cents': 5000, 'currency': 'USD' }); + + return query; +} + + +describe('recurly secure parameters signer', function() +{ + var recurlyUnencoded = 'account[account_code]=123&nonce=unique×tamp=1329942896&transaction[amount_in_cents]=5000&transaction[currency]=USD'; + var recurlyEncoded = 'account%5Baccount_code%5D=123&nonce=unique×tamp=1329942896&transaction%5Bamount_in_cents%5D=5000&transaction%5Bcurrency%5D=USD'; + + it('generates a query string exactly like the Recurly example', function() + { + var recurlyTest = transactionTestFixture(); + var str = recurlyTest.serialize(); + + assert.equal(str, recurlyUnencoded, 'parameter stringifying does not work identically to the ruby/php versions'); + assert.equal(encodeURI(str), recurlyEncoded, 'encodeURI() broke it'); + }); + + it('the generated HMAC is identical to the one generated by Recurly', function() + { + var recurlyTest = transactionTestFixture(); + var str = encodeURI(qs.stringify(recurlyTest.params)); + var hmac = recurlyTest.HMAC(str); + assert.equal(hmac, '95c000d2aa045cb20596b8a751b08c8dfaee8cf2', 'generated hmac did not match the recurly example'); + }); + + it('generates a final signed parameter string in the expected format', function() + { + var recurlyTest = transactionTestFixture(); + var result = recurlyTest.toString(); + var transactionTestExpected = '95c000d2aa045cb20596b8a751b08c8dfaee8cf2|account%5Baccount_code%5D=123&nonce=unique×tamp=1329942896&transaction%5Bamount_in_cents%5D=5000&transaction%5Bcurrency%5D=USD'; + + assert.equal(result, transactionTestExpected, 'final signed string is in the wrong format'); + }); + + it('passes the recurly subscription signing test', function() + { + var query = new SignedQuery(testAPIKey); + query.set('account', {'account_code': '123'}); + query.set('nonce', testNonce); + query.set('subscription', {'plan_code': 'gold'}); + query.set('timestamp', testTimestamp); + + var subscriptionTestExpected = '295bd0626ab03fd01053fb0784bd5187b563cbeb|account%5Baccount_code%5D=123&nonce=unique&subscription%5Bplan_code%5D=gold×tamp=1329942896'; + var result = query.toString(); + assert.equal(result, subscriptionTestExpected, 'signed subscription transaction incorrect'); + }); + + it('can sign update billing info requests', function() + { + var query = new SignedQuery(testAPIKey); + query.set('account', {'account_code': '123'}); + query.set('nonce', testNonce); + query.set('timestamp', testTimestamp); + + var expected = '86509e315e8396423e420839a9c4cbafd5f230f3|account%5Baccount_code%5D=123&nonce=unique×tamp=1329942896'; + var result = query.toString(); + assert.equal(result, expected, 'billing info signature failure'); + }); + +});