1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/lib/tasks/fablab/stripe.rake

71 lines
2.6 KiB
Ruby
Raw Normal View History

2019-02-13 12:59:28 +01:00
# frozen_string_literal: true
# Stripe relative tasks
namespace :fablab do
namespace :stripe do
desc 'Cancel stripe subscriptions'
task cancel_subscriptions: :environment do
Subscription.where('expiration_date >= ?', DateTime.current.at_beginning_of_day).each do |s|
2019-02-13 12:59:28 +01:00
puts "-> Start cancel subscription of #{s.user.email}"
s.cancel
puts '-> Done'
end
end
desc 'find any invoices with incoherent total between stripe and DB'
task :find_incoherent_invoices, [:start_date] => :environment do |_task, args|
puts 'DEPRECATION WARNING: Will not work for invoices created from version 4.1.0 and above'
2019-02-13 12:59:28 +01:00
date = Date.parse('2017-05-01')
if args.start_date
begin
date = Date.parse(args.start_date)
rescue ArgumentError => e
raise e
end
end
Invoice.where('created_at > ? AND stp_invoice_id IS NOT NULL', date).each do |invoice|
stp_invoice = Stripe::Invoice.retrieve(invoice.stp_invoice_id)
next if invoice.amount_paid == stp_invoice.total
puts "Id: #{invoice.id}, reference: #{invoice.reference}, stripe id: #{stp_invoice.id}, " \
"invoice total: #{invoice.amount_paid / 100.0}, stripe invoice total: #{stp_invoice.total / 100.0}, " \
"date: #{invoice.created_at}"
end
end
desc 'clean stripe secrets from VCR cassettes'
task clean_cassettes_secrets: :environment do
Dir['test/vcr_cassettes/*.yml'].each do |cassette_file|
cassette = File.read(cassette_file)
cassette = cassette.gsub(Rails.application.secrets.stripe_api_key, 'sk_test_testfaketestfaketestfake')
cassette = cassette.gsub(Rails.application.secrets.stripe_publishable_key, 'pk_test_faketestfaketestfaketest')
puts cassette
File.write(cassette_file, cassette)
end
end
2019-12-03 16:32:59 +01:00
desc 'sync users to the stripe database'
task sync_members: :environment do
puts 'We create all non-existing customers on stripe. This may take a while, please wait...'
total = User.online_payers.count
User.online_payers.each_with_index do |member, index|
print_on_line "#{index} / #{total}"
2019-12-03 16:32:59 +01:00
begin
stp_customer = Stripe::Customer.retrieve member.stp_customer_id
StripeWorker.perform_async(:create_stripe_customer, member.id) if stp_customer.nil? || stp_customer[:deleted]
rescue Stripe::InvalidRequestError
StripeWorker.perform_async(:create_stripe_customer, member.id)
end
end
puts 'Done'
end
def print_on_line(str)
print "#{str}\r"
$stdout.flush
2019-12-03 16:32:59 +01:00
end
2019-02-13 12:59:28 +01:00
end
end