Permalink
Browse files

Updated to API v2

  • Loading branch information...
1 parent d732a91 commit fcaf63f1249d4c90adbe1de71b08ce2d7dfdda15 @IvanGuardado IvanGuardado committed Sep 30, 2012
Showing with 386 additions and 342 deletions.
  1. +1 −0 .gitignore
  2. +0 −79 example.js
  3. +1 −0 index.js
  4. +105 −0 lib/client.js
  5. +158 −98 lib/recurly.js
  6. +12 −0 lib/routes/index.js
  7. +79 −0 lib/routes/v2.js
  8. +0 −156 lib/tools.js
  9. +19 −0 lib/utils.js
  10. +2 −1 package.json
  11. +9 −8 { → test}/config-example.js
View
@@ -1,2 +1,3 @@
config.js
test.js
+node_modules
View
@@ -1,79 +0,0 @@
-var sys = require('sys');
-var Recurly = require('./lib/recurly');
-var callback = function(result){
- console.log(sys.inspect(result));
-};
-
-recurly = new Recurly(require('./config'));
-// recurly.charges.chargeAccount('robrighter@gmail.com',{
-// amount_in_cents: '850',
-// description: 'testing the charge'
-// },callback);
-
-//recurly.charges.listAll('robrighter@gmail.com',callback);
-
-// recurly.coupons.redeemOnAccount('robrighter@gmail.com', {
-// coupon_code: '50PERCENTOFF'
-// },callback);
-
-//recurly.coupons.removeFromAccount('robrighter@gmail.com',callback);
-
-//recurly.coupons.getAssociatedWithAccount('robrighter@gmail.com',callback);
-
-// recurly.credits.creditAccount('robrighter@gmail.com',{
-// amount_in_cents: 550,
-// description: 'Cutting you a break 3'
-// }, callback);
-
-//recurly.credits.listAll('robrighter@gmail.com',callback);
-
-//recurly.invoices.getAssociatedWithAccount('robrighter@gmail.com',callback);
-//recurly.invoices.get('7aba9e26feae42c1acb078fea1024c6f',callback);
-//recurly.invoices.invoiceAccount('robrighter@gmail.com',callback);
-
-//recurly.subscriptions.getAssociatedWithAccount('robrighter@gmail.com',callback);
-
-//recurly.subscriptionPlans.listAll(callback);
-//recurly.subscriptions.refund('robrighter@gmail.com',callback,'partial');
-
-// recurly.subscriptions.create('robrighter@gmail.com',{
-// plan_code: 'test-plan',
-// quantity: 1,
-// account: {
-// billing_info: {
-// first_name: 'berty',
-// last_name: 'tester',
-// address1: '123 my street',
-// address2: '',
-// city: 'Chattanooga',
-// state: 'TN',
-// zip: '37408',
-// country: 'US'
-// }
-// }
-// },callback);
-
-// recurly.transactions.createImmediateOneTimeTransaction('robrighter@gmail.com',{
-// account:{
-// account_code: 'robrighter'
-// },
-// amount_in_cents: 600,
-// description: 'just testing things out'
-// },callback)
-
-var data = {
- 'account[account_code]': 'demo-1301435036',
- 'account[username]': 'username123',
- 'redirect_url': 'http://localhost/subscribe.php',
- 'subscription[plan_code]': 'test-plan'
-}
-
-console.log(recurly.transparent.subscribeUrl());
-
-console.log(recurly.transparent.hidden_field(data));
-
-recurly.transparent.getResults('dfd82a741b3e5f15e32439fb66f7696046138105',//confirm
- '31c6f6c96f3045cdbc126934295e889b',//result
- '422',//status
- 'subscription',//type
- callback)
View
@@ -0,0 +1 @@
+module.exports = require('./lib/recurly');
View
@@ -0,0 +1,105 @@
+(function(){
+
+var https = require('https'),
+ Xml2js = require('xml2js'),
+ parser = new Xml2js.Parser({explicitArray: false});
+
+exports.create = function(config){
+ config.RECURLY_HOST = 'api.recurly.com';
+ return {
+
+ request: function(route, callback, data){
+ var endpoint = route[0];
+ var method = route[1];
+ var that = this;
+ var options = {
+ host: config.RECURLY_HOST,
+ port: 443,
+ path: endpoint,
+ method: method,
+ headers: {
+ Authorization: "Basic "+(new Buffer(config.API_USERNAME+":"+config.API_PASSWORD)).toString('base64'),
+ Accept: 'application/xml',
+ 'Content-Length' : (data) ? data.length : 0
+ }
+ };
+
+ if(method.toLowerCase() == 'post' || method.toLowerCase() == 'put' ){
+ options.headers['Content-Type'] = 'application/xml';
+ that.debug(data);
+ }
+ that.debug(options);
+ var req = https.request(options, function(res) {
+
+ var responsedata = '';
+ res.on('data', function(d) {
+ responsedata+=d;
+ });
+ res.on('end', function(){
+ responsedata = that.trim(responsedata);
+ that.debug('Response is: ' + res.statusCode);
+ that.debug(responsedata);
+ try{
+ var toreturn = {status: 'ok', data: '' };
+ if((res.statusCode == 404) || (res.statusCode == 422) || (res.statusCode == 500) || (res.statusCode == 412)){
+ toreturn.status = 'error';
+ parser.parseString(responsedata, function(err, result){
+ toreturn.data = result;
+ callback(toreturn);
+ });
+ }
+ else if(res.statusCode >= 400){
+ toreturn.status = 'error';
+ toreturn.data = res.statusCode;
+ toreturn.additional = responsedata;
+ callback(toreturn);
+ }
+ else{
+ if(responsedata != ''){
+ parser.parseString(responsedata, function(err, result){
+ toreturn.data = result;
+ callback(toreturn);
+ });
+ }
+ else{
+ callback({status: 'ok', description: res.statusCode });
+ }
+ }
+ return;
+ }
+ catch(e){
+ throw e
+ callback({status: 'error', description: e });
+ }
+ });
+ });
+ if(data){
+ req.write(data);
+ }
+ req.end();
+ req.on('error', function(e) {
+ callback({status: 'error', description: e });
+ });
+ },
+
+ debug: function(s){
+ if(config.DEBUG){
+ console.log(s);
+ }
+ },
+
+ trim: function(str) {
+ str = str.replace(/^\s+/, '');
+ for (var i = str.length - 1; i >= 0; i--) {
+ if (/\S/.test(str.charAt(i))) {
+ str = str.substring(0, i + 1);
+ break;
+ }
+ }
+ return str;
+ }
+
+ }
+}
+
+})();
Oops, something went wrong.

0 comments on commit fcaf63f

Please sign in to comment.