<commit_before>//Inspiration from http://gaarf.info/2013/06/25/securing-stripe-webhooks-with-node-js/


module.exports = function (config) {

  var stripe = require('stripe')(config.secret),
    models = require('../../models'),
    stripeTransactions = {
      'unhandled':          require('./unhandled.js'),
      'charge.succeeded':   require('./charge.succeeded.js')
    };

  return {

    authEvent: function(req, res, next) {
      
      if (req.body.object !== 'event') {
        return res.send(400); // invalid data    
      }

      stripe.events.retrieve(req.body.id, function(err, event) {
        
        // err = invalid event, !event = error from stripe
        if (err || !event) {
          return res.send(401); 
        }

        req.stripeEvent = event;
        next();

      });

    },

    handleTransaction: function(req, res) {

      var event = req.stripeEvent,
          data = event.data.object; 

      // takes care of unexpected event types
      var transactionMethod = stripeTransactions[event.type] || stripeTransactions.unhandled;

      transactionMethod(data, function(err){
        if ( !err ) {
          res.send(200);
        }
      });

    },

    handleSubscription: function(req, res) {
      var user = req.session.user,
          stripeToken = req.body.stripeToken;
      if (!user) {
        // please login before subscribing to jsbin
        // TODO
      }

      stripe.customers.create({
        card: stripeToken,
        plan: 'test_pro'
      }).then(function (err, customer) {
        if (!err) {
          models.user.setProAccount(user.name, true, function(err) {
            if (!err) {
              req.session.user.pro = true;
              res.redirect('/');
            }
          });
          models.customer.setCustomer({
            user: user.name,
            stripeId: customer.id
          });
        }
      });

<commit_msg>updating customer creation fulfilment<commit_after>//Inspiration from http://gaarf.info/2013/06/25/securing-stripe-webhooks-with-node-js/


module.exports = function (config) {

  var stripe = require('stripe')(config.secret),
    models = require('../../models'),
    stripeTransactions = {
      'unhandled':                  require('./unhandled.js'),
      'charge.succeeded':           require('./charge.succeeded.js'),
      'invoice.payment_succeeded':  require('./invoice.payment_succeeded.js')
    };

  return {

    authEvent: function(req, res, next) {
      
      if (req.body.object !== 'event') {
        return res.send(400); // invalid data    
      }

      stripe.events.retrieve(req.body.id, function(err, event) {
        
        // err = invalid event, !event = error from stripe
        if (err || !event) {
          return res.send(401); 
        }

        req.stripeEvent = event;
        next();

      });

    },

    handleTransaction: function(req, res) {

      var event = req.stripeEvent,
          data = event.data.object; 

      // takes care of unexpected event types
      var transactionMethod = stripeTransactions[event.type] || stripeTransactions.unhandled;

      transactionMethod(data, function(err){
        if ( !err ) {
          res.send(200);
        }
      });

    },

    handleSubscription: function(req, res) {
      var user = req.session.user,
          stripeToken = req.body.stripeToken;
      if (!user) {
        // please login before subscribing to jsbin
        // TODO
      }

      stripe.customers.create({
        card: stripeToken,
        plan: 'test_pro'
      }).then(function (customer) {

        models.user.setProAccount(user.name, true, function(err) {
          if (!err) {
            req.session.user.pro = true;
            req.flash('notification', 'Welcome to the pro user lounge. Your seat is waiting');
          } else {
            req.flash('error', 'Something went wrong, please try signing in and out again');
          }
          res.redirect('/');
        });

        models.customer.setCustomer({
          user: user.name,
          stripeId: customer.id
        });

      }, function(err) {
        // error with customer creation
        err;
      });


{'fraction_non_alphanumeric': 0.13501199040767387, 'fraction_numerical': 0.00815347721822542, 'mean_word_length': 1.8316361167684996, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2240221', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1186, 'n_words': 249}