<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'),
      '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
      }

      var cardToFail = {
        number: '4000000000000341',
        exp_month: 08,<commit_msg>Adding helper objects for testing<commit_after>//Inspiration from http://gaarf.info/2013/06/25/securing-stripe-webhooks-with-node-js/

// Remove once finished testing
var cardToFail = {
  number: '4000000000000341',
  exp_month: 08,
  exp_year: 2018,
  cvc: 857
};

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,
        customerOptions = {
          plan: req.params.plan,
          card: req.body.stripeToken
        };
      if (!user) {
        // please login before subscribing to jsbin
        // TODO
      }

      var cardToFail = {
        number: '4000000000000341',
        exp_month: 08,
{'fraction_non_alphanumeric': 0.1335666375036454, 'fraction_numerical': 0.027704870224555263, 'mean_word_length': 2.0140597539543057, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, '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': '3801822', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1003, 'n_words': 208}