summaryrefslogtreecommitdiff
path: root/server/lib/webhook/webhook.js
blob: 2e5e6275368976b5c0f81e1e3e00624ec40e395f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// // where should this live?
// app.get('/subscribe/webhook', views.subscription.webhook);

/*
app.use(express.basicAuth(function(user, pass, callback) {
 var result = (user === 'testUser' && pass === 'testPass');
 callback(null, result);
}));
*/


/* jshint node: true */

var User = require('../schemas/User'),
	Subscription = require('../schemas/Subscription'),
	config = require('../../../config'),
	middleware = require('../middleware'),
	util = require('../util'),
	_ = require('lodash'),
	moment = require('moment'),
	xml2js = require('xml2js'),
	Recurly = require('node-recurly'),
	recurly = new Recurly(require('./config'));

var parser = new xml2js.Parser();

var subscribe = module.exports = {
  plan_level: {
    free: 0,
    basic: 1,
    pro: 2,
  },
	
  callbacks: {
/*
		// accounts
		new_account_notification: function(data, user){
			// fires on successful signup
		},
		canceled_account_notification: function(data, user){
		},
		billing_info_updated_notification: function(data, user){
		},
		reactivated_account_notification: function(data, user){
		},
		
		// invoices
		new_invoice_notification: function(data, user){
		},
		closed_invoice_notification: function(data, user){
		},
		past_due_invoice_notification: function(data, user){
		},
*/

		// subscriptions
		new_subscription_notification: function(data, user){
			var account = data.account
			Subscription.findOne({ "uuid": data.subscription.uuid }, function(err, subscription){
			  if (err || subscription) return;
       
        var plan = data.subscription.plan.plan_code.split("-")
        var plan_type = plan[0]
        var plan_period = plan[1]
        
        user.plan_type = plan_type
        user.plan_period = plan_period
        user.plan_level = subscribe.plan_level[plan_type]

				var subscriber = new Subscription ()
				subscriber.user_id = user._id
        subscriber.uuid = data.subscription.uuid
        subscriber.add_ons = subscription.add_ons.map(function(add_on){
        	return {
        		name: add_on.plan,
        		quantity: add_on.quantity,
        	}
        })
        subscriber.save(function(err, data){
        	if (err) return;
					user.save(function(err){
						// saved!
					})
        })
			})
		},

/*
		updated_subscription_notification: function(data, user){
		},
		canceled_subscription_notification: function(data, user){
		},
		expired_subscription_notification: function(data, user){
		},
		renewed_subscription_notification: function(data, user){
		},
*/		
		// payments
		successful_payment_notification: function(data, user){
			var account = data.account
			user.last_charged = new Date(data.transaction.date)
			user.save(function(){
			})
		},
/*
		failed_payment_notification: function(data, user){
		},
		successful_refund_notification: function(data, user){
		},
		void_payment_notification: function(data, user){
		},
*/
  },
	
	execute: function(action, data){
		User.findOne({ _id: data.account.account_code }, function(err, user){
			if (err) { return }
			subscribe.callbacks[action](data, user)
		})
	},
	
  // then calls to get appropriate info from the recurly api
	handle: function(req, res){
    res.status(200).end()
		parser.parseString(data, function (err, result) {
			console.log(inspect(result, { colors: true, depth: Infinity }));
			for (var action in result) {
			  if (subscribe.callbacks[action]) {
			  	subscribe.execute(action, result[action]);
			  }
			}
		});
	},
	
	route: function(app){
		app.post('/subscribe/webhook', subscribe.handle);
	},
}