<commit_before>#!/usr/bin/env node

var async = require("async");
var currencies = Object.keys(require("../data/currencies"));
var Stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

Stripe.plans.list({
  include: ["total_count"],
  limit: 100
}, function(list_error, plans) {
  if (list_error) {
    throw list_error;
  }

  var stripe_plans = {};
  plans.data.forEach(function(plan) {
    stripe_plans[plan.id] = plan;
  });

  var plans_to_create = currencies.filter(function(currency) {
    return !stripe_plans[currency];
  }).map(function(currency) {
    return {
      amount: 1,
      currency: currency,
      id: currency,
      interval: "month",
      interval_count: 1,
      // This is the invoice name that appears on an email invoice from Stripe
      name: "Mozilla Foundation Donation",
      // This is the line item name that appears on a credit card statement
      // This needs to be limited to 22 ASCII characters
      // https://stripe.com/docs/api/node#create_plan
      statement_descriptor: "Mozilla Foundation"
    };
  });

  if (plans_to_create === 0) {
    console.log("No plans to create");
    return;
  }

  create_plan_q.push(plans_to_create);
});

var create_plan_q = async.queue(function(plan, done) {
  Stripe.plans.create(plan, function(create_error) {
    if (create_error) {
      throw create_error;
    }

    console.log("Created %s plan", plan.id);
    done();
  });
});<commit_msg>Enable updating of existing Plans<commit_after>#!/usr/bin/env node

require("habitat").load();

var async = require("async");
var Stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);

var currencies = Object.keys(require("../data/currencies"));
var local_plans = {};
currencies.forEach((currency) => {
  local_plans[currency] = {
    amount: 1,
    currency: currency,
    id: currency,
    interval: "month",
    interval_count: 1,
    // This is the invoice name that appears on an email invoice from Stripe
    name: "Monthly Donation " + currency,
    // This is the line item name that appears on a credit card statement
    // This needs to be limited to 22 ASCII characters
    // https://stripe.com/docs/api/node#create_plan
    statement_descriptor: "Mozilla Foundation"
  };
});

Stripe.plans.list({
  include: ["total_count"],
  limit: 100
}, function(list_error, plans) {
  if (list_error) {
    throw list_error;
  }

  var stripe_plans = {};
  plans.data.forEach(function(plan) {
    stripe_plans[plan.id] = plan;
  });

  var plans_to_create = currencies.filter(function(currency) {
    return !stripe_plans[currency];
  }).map(function(currency) {
    return local_plans[currency];
  });

  var plans_to_update = currencies.filter(function(currency) {
    return !!stripe_plans[currency] &&
           ( stripe_plans[currency].name != local_plans[currency].name ||
           stripe_plans[currency].statement_descriptor != local_plans[currency].statement_descriptor);
  }).map(function(currency) {
    return {
      id: currency,
      name: local_plans[currency].name,
      statement_descriptor: local_plans[currency].statement_descriptor
    };
  });

  if (plans_to_create.length === 0) {
    console.log("No plans to create");
  } else {
    create_plan_q.push(plans_to_create);
  }

  if (plans_to_update.length === 0) {
    console.log("No plans to update");
  } else {
    update_plan_q.push(plans_to_update);
  }
});

var create_plan_q = async.queue(function(plan, done) {
  Stripe.plans.create(plan, function(create_error) {
    if (create_error) {
      throw create_error;
    }

    console.log("Created %s plan", plan.id);
    done();
  });
});

var update_plan_q = async.queue(function(plan, done) {
  Stripe.plans.update(plan.id, {
    name: plan.name,
    statement_descriptor: plan.statement_descriptor
  }, function(update_error) {
    if (update_error) {
      throw update_error;
    }

    console.log("Updated %s plan", plan.id);
    done();
  });
});
{'fraction_non_alphanumeric': 0.13726487036095578, 'fraction_numerical': 0.004321301474326385, 'mean_word_length': 3.502288329519451, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '2087193', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1279, 'n_words': 277}