Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
1,438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. # Downsides
  2. # - no coupons
  3. # - no paypal
  4. # - no VAT
  5. # - no approved/paid statements/adjustments
  6. # - no locks, etc
  7. # - no teachable payments
  8. # - prolly some other things
  9.  
  10.  
  11. # Running it:
  12. # - make sure you've exited your rails server, console, and sidekiq
  13. # - in your terminal, clear and reseed your local DB:
  14. `bundle exec rake db:drop db:create db:migrate && bundle exec rake db:seed`
  15.  
  16. # - open a rails console
  17. `rails c`
  18.  
  19. # - copy the entire script below and type `paste` to shove it all into your rails console and execute
  20. `paste`
  21.  
  22.  
  23. class SeedEarningsStatements
  24.  
  25. include Service
  26.  
  27. NUM_USERS = 100
  28. NUM_PURCHASES_PER_USER = 3 # up to 10
  29. NUM_MONTHS = 6
  30.  
  31. def initialize
  32. @school = School.first; nil
  33. @author = @school.users.author.first.update!(author_revenue_share: 0.5)
  34. @affiliates = generate_affiliates
  35. end
  36.  
  37. def call
  38. Rails.logger.level = :error
  39.  
  40. puts "generating products"
  41. @products = generate_products.compact
  42. puts "products: #{@products.size}"
  43. puts " "
  44.  
  45. @stripe_customer = create_stripe_customer
  46.  
  47. puts "generating users"
  48. @users = generate_users.compact
  49. puts "users: #{@users.size}"
  50. puts " "
  51.  
  52. puts "buying stuff"
  53. buy_stuff
  54. finalize_and_distribute_transactions
  55. puts "adjustments: #{EarningsAdjustment.all.size}"
  56. puts " "
  57.  
  58. puts "creating refunds and chargebacks"
  59. refunds
  60. chargebacks
  61. puts " "
  62.  
  63. puts "generating statements"
  64. generate_statements
  65. puts "statements: #{EarningsStatement.all.size}"
  66. puts "payout attempts: #{PayoutAttempt.all.size}"
  67. end
  68.  
  69. def generate_affiliates
  70. (1..4).map do |i|
  71. User.create!(
  72. school: @school,
  73. name: "Affiliate #{i}",
  74. email: "#{SecureRandom.hex(4)}@example.com",
  75. password: "password",
  76. password_confirmation: "password",
  77. agreed_to_terms: true,
  78. affiliate_revenue_share: 0.2,
  79. is_affiliate: true
  80. )
  81. end
  82. end
  83.  
  84. def generate_products
  85. (1..10).map do |i|
  86. course = Course.create!(
  87. name: "Course #{i}",
  88. author_bio: @school.author_bios.sample,
  89. school: @school,
  90. friendly_url: SecureRandom.hex
  91. )
  92. course.products.create!(
  93. name: "Product #{i}",
  94. price: (rand*100_00).round,
  95. school: @school,
  96. currency: ["USD","EUR", "JPY"].sample,
  97. is_published: true
  98. )
  99. end
  100. end
  101.  
  102. def create_stripe_customer
  103. 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)
  104. stripe_record = Stripe::Customer.retrieve("cus_CvERVZH1vJ27Wp")
  105. card = Stripe::Customer.retrieve("cus_CvERVZH1vJ27Wp").sources.create(source: "tok_visa")
  106. stripe_record.default_source = card.id
  107. stripe_record.save
  108. stripe_customer
  109. rescue
  110. StripeCustomer.last
  111. end
  112.  
  113. def generate_users
  114. (1..NUM_USERS).map do |i|
  115. user = User.create!(
  116. school: @school,
  117. name: "Student #{i}",
  118. email: "#{SecureRandom.hex(4)}@example.com",
  119. password: "password",
  120. password_confirmation: "password",
  121. agreed_to_terms: true
  122. )
  123. PaymentMethods::Store.add_for_user(user, @stripe_customer)
  124. user
  125. rescue
  126. end
  127. end
  128.  
  129. def buy_stuff
  130. @users.each do |user|
  131. @products.sample(NUM_PURCHASES_PER_USER).each do |product|
  132. Invoice.new(product: product, user: user, affiliate: @affiliates.sample).purchase!.tap(&:finalize!)
  133. rescue
  134. end
  135. end
  136. end
  137.  
  138. def finalize_and_distribute_transactions
  139. Transaction.all.each do |transaction|
  140. transaction.capture_stripe_charge
  141. Transactions::AdjustEarnings.call(transaction, :earnings)
  142. transaction.update!(purchased_at: rand(NUM_MONTHS.months.ago..Time.zone.now))
  143. transaction.earnings_adjustments.each { |ea| ea.update!(created_at: transaction.purchased_at) }
  144. # rescue
  145. end
  146. end
  147.  
  148. def refunds
  149. num = (Transaction.all.size.to_f * 0.2).round
  150. Transaction.all.sample(num).each do |tr|
  151. Transactions::Refund.call(tr, amount: tr.net_charge, refunder_id: 1)
  152. date = (tr.purchased_at.to_date..Date.today).to_a.sample
  153. tr.earnings_adjustments.refund.each { |ea| ea.update!(created_at: date) }
  154. end
  155. end
  156.  
  157. def chargebacks
  158. num = (Transaction.all.size.to_f * 0.1).round
  159. Transaction.where(amount_refunded: 0).sample(num).each do |tr|
  160. Transactions::Chargeback.call(tr)
  161. date = (tr.purchased_at.to_date..Date.today).to_a.sample
  162. tr.earnings_adjustments.chargeback.each { |ea| ea.update!(created_at: date) }
  163. end
  164. end
  165.  
  166. def generate_statements
  167. first_date = Transaction.order(:purchased_at).first.purchased_at.beginning_of_month.to_date
  168. # this is dumb
  169. # EarningsStatement.create!(school_id: @school.id, payee: User.affiliates.first, total_amount: 0, internal_gateway_amount: 0, custom_gateway_amount: 0)
  170. (0..NUM_MONTHS - 1).map { |i| first_date + i.months }.each { |date| Finance::MonthlyPayout.call(date: date) }
  171. end
  172.  
  173. end
  174.  
  175. SeedEarningsStatements.call
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement