Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Downsides
- # - no coupons
- # - no paypal
- # - no VAT
- # - no approved/paid statements/adjustments
- # - no locks, etc
- # - no teachable payments
- # - prolly some other things
- # Running it:
- # - make sure you've exited your rails server, console, and sidekiq
- # - in your terminal, clear and reseed your local DB:
- `bundle exec rake db:drop db:create db:migrate && bundle exec rake db:seed`
- # - open a rails console
- `rails c`
- # - copy the entire script below and type `paste` to shove it all into your rails console and execute
- `paste`
- class SeedEarningsStatements
- include Service
- NUM_USERS = 100
- NUM_PURCHASES_PER_USER = 3 # up to 10
- NUM_MONTHS = 6
- def initialize
- @school = School.first; nil
- @author = @school.users.author.first.update!(author_revenue_share: 0.5)
- @affiliates = generate_affiliates
- end
- def call
- Rails.logger.level = :error
- puts "generating products"
- @products = generate_products.compact
- puts "products: #{@products.size}"
- puts " "
- @stripe_customer = create_stripe_customer
- puts "generating users"
- @users = generate_users.compact
- puts "users: #{@users.size}"
- puts " "
- puts "buying stuff"
- buy_stuff
- finalize_and_distribute_transactions
- puts "adjustments: #{EarningsAdjustment.all.size}"
- puts " "
- puts "creating refunds and chargebacks"
- refunds
- chargebacks
- puts " "
- puts "generating statements"
- generate_statements
- puts "statements: #{EarningsStatement.all.size}"
- puts "payout attempts: #{PayoutAttempt.all.size}"
- end
- def generate_affiliates
- (1..4).map do |i|
- User.create!(
- school: @school,
- name: "Affiliate #{i}",
- email: "#{SecureRandom.hex(4)}@example.com",
- password: "password",
- password_confirmation: "password",
- agreed_to_terms: true,
- affiliate_revenue_share: 0.2,
- is_affiliate: true
- )
- end
- end
- def generate_products
- (1..10).map do |i|
- course = Course.create!(
- name: "Course #{i}",
- author_bio: @school.author_bios.sample,
- school: @school,
- friendly_url: SecureRandom.hex
- )
- course.products.create!(
- name: "Product #{i}",
- price: (rand*100_00).round,
- school: @school,
- currency: ["USD","EUR", "JPY"].sample,
- is_published: true
- )
- end
- end
- def create_stripe_customer
- stripe_customer = StripeCustomer.create!(stripe_token: "cus_CvERVZH1vJ27Wp", last_four: "1337", card_type: "Visa", is_active: true, exp_month: 12, exp_year: 2020, original_school_id: 1)
- stripe_record = Stripe::Customer.retrieve("cus_CvERVZH1vJ27Wp")
- card = Stripe::Customer.retrieve("cus_CvERVZH1vJ27Wp").sources.create(source: "tok_visa")
- stripe_record.default_source = card.id
- stripe_record.save
- stripe_customer
- rescue
- StripeCustomer.last
- end
- def generate_users
- (1..NUM_USERS).map do |i|
- user = User.create!(
- school: @school,
- name: "Student #{i}",
- email: "#{SecureRandom.hex(4)}@example.com",
- password: "password",
- password_confirmation: "password",
- agreed_to_terms: true
- )
- PaymentMethods::Store.add_for_user(user, @stripe_customer)
- user
- rescue
- end
- end
- def buy_stuff
- @users.each do |user|
- @products.sample(NUM_PURCHASES_PER_USER).each do |product|
- Invoice.new(product: product, user: user, affiliate: @affiliates.sample).purchase!.tap(&:finalize!)
- rescue
- end
- end
- end
- def finalize_and_distribute_transactions
- Transaction.all.each do |transaction|
- transaction.capture_stripe_charge
- Transactions::AdjustEarnings.call(transaction, :earnings)
- transaction.update!(purchased_at: rand(NUM_MONTHS.months.ago..Time.zone.now))
- transaction.earnings_adjustments.each { |ea| ea.update!(created_at: transaction.purchased_at) }
- # rescue
- end
- end
- def refunds
- num = (Transaction.all.size.to_f * 0.2).round
- Transaction.all.sample(num).each do |tr|
- Transactions::Refund.call(tr, amount: tr.net_charge, refunder_id: 1)
- date = (tr.purchased_at.to_date..Date.today).to_a.sample
- tr.earnings_adjustments.refund.each { |ea| ea.update!(created_at: date) }
- end
- end
- def chargebacks
- num = (Transaction.all.size.to_f * 0.1).round
- Transaction.where(amount_refunded: 0).sample(num).each do |tr|
- Transactions::Chargeback.call(tr)
- date = (tr.purchased_at.to_date..Date.today).to_a.sample
- tr.earnings_adjustments.chargeback.each { |ea| ea.update!(created_at: date) }
- end
- end
- def generate_statements
- first_date = Transaction.order(:purchased_at).first.purchased_at.beginning_of_month.to_date
- # this is dumb
- # EarningsStatement.create!(school_id: @school.id, payee: User.affiliates.first, total_amount: 0, internal_gateway_amount: 0, custom_gateway_amount: 0)
- (0..NUM_MONTHS - 1).map { |i| first_date + i.months }.each { |date| Finance::MonthlyPayout.call(date: date) }
- end
- end
- SeedEarningsStatements.call
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement