<commit_before>'use strict';
//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
// };

var Promise = require('rsvp').Promise;

module.exports = function (config) {

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

  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;
      var data = event.data.object;

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

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

    },

    handleSubscription: function (req, res) {
      var user = req.session.user;
      var customerOptions = {
        plan: req.params.plan,
        card: req.body.stripeToken
      };

      if (!user) {
        res.flash('error', 'Please login before attempting to signup');
        res.redirect('/');
        return;
      }

      function handleCardError(err) {
        // error with customer creation
        res.flash('error', err.message);<commit_msg>Add coupon to customer if it exists<commit_after>'use strict';
//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
// };

var Promise = require('rsvp').Promise;

module.exports = function (config) {

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

  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;
      var data = event.data.object;

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

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

    },

    handleSubscription: function (req, res) {
      var user = req.session.user;
      var customerOptions = {
        plan: req.params.plan,
        card: req.body.stripeToken
      };
      var couponCode = req.url.indexOf('?') !== -1 ? req.url.split('?').pop() : null;
      if (couponCode) {
        customerOptions.coupon = couponCode;
      }

      if (!user) {
        res.flash('error', 'Please login before attempting to signup');
        res.redirect('/');
        return;
      }

      function handleCardError(err) {
        // error with customer creation
        res.flash('error', err.message);
{'fraction_non_alphanumeric': 0.14585971131932135, 'fraction_numerical': 0.023043808559128895, 'mean_word_length': 2.272576636288318, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6787482', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1172, 'n_words': 273}