mizner
7/28/2017 - 4:12 PM

Creating mass dummy data in Stripe for testing

Creating mass dummy data in Stripe for testing

/*
  We get the dummy tokens from
  https://stripe.com/docs/testing#cards-responses
*/
const stripe = require('stripe')(process.env.STRIPE_KEY);
const faker = require('faker');

const createAmount = () => Math.floor(Math.random() * 2000) + 500

const makeCharge = ({ source }) => {
  const amount = createAmount();
  const tenant_id = Math.floor(Math.random() * 50) + 1;
  const count_name = faker.address.county();
  const metadata = {
    'tenant_id': tenant_id,
    'order_id': Math.floor(Math.random() * 10000) + 1,
  };

  return stripe.charges.create({
      description: `tenant_id:${tenant_id};county_name=${count_name}`,
      metadata,
      amount,
      source,
      currency: 'usd',
    });
}

const methods = [
  'tok_bypassPending',
  'tok_domesticPricing',
];

const errorMethods = [
  'tok_avsFail',
  'tok_avsLine1Fail',
  'tok_avsZipFail',
  'tok_avsUnchecked',
  'tok_cvcCheckFail',
  'tok_chargeCustomerFail',
  'tok_riskLevelElevated',
  'tok_chargeDeclined',
  'tok_chargeDeclinedFraudulent',
  'tok_chargeDeclinedIncorrectCvc',
  'tok_chargeDeclinedExpiredCard',
  'tok_chargeDeclinedProcessingError',
];

const amountPerType = 100;

methods.forEach(source => {
  for (let i = 0; i < amountPerType; i++) {
    makeCharge({
      source,
    })
      .catch(err => {
        console.log(err);
      })
  }
})

errorMethods.forEach(source => {
  for (let i = 0; i < 20; i++) {
    makeCharge({
      source,
    })
      .catch(err => {
        console.log(err);
      })
  }
})