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
|
2019-12-02 11:57:25 +01:00
|
|
|
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|
|
2019-09-10 16:45:45 +02:00
|
|
|
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|
|
2020-08-26 11:35:27 +02:00
|
|
|
stp_invoice = Stripe::Invoice.retrieve(invoice.stp_invoice_id, api_key: Setting.get('stripe_secret_key'))
|
2019-02-13 12:59:28 +01:00
|
|
|
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)
|
2020-06-10 11:33:03 +02:00
|
|
|
cassette = cassette.gsub(Setting.get('stripe_secret_key'), 'sk_test_testfaketestfaketestfake')
|
2020-06-10 11:02:30 +02:00
|
|
|
cassette = cassette.gsub(Setting.get('stripe_public_key'), 'pk_test_faketestfaketestfaketest')
|
2019-02-13 12:59:28 +01:00
|
|
|
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
|
2020-04-29 10:57:32 +02:00
|
|
|
puts 'We create all non-existing customers on stripe. This may take a while, please wait...'
|
2020-07-01 16:26:59 +02:00
|
|
|
SyncMembersOnStripeWorker.new.perform
|
2020-04-29 10:57:32 +02:00
|
|
|
puts 'Done'
|
|
|
|
end
|
|
|
|
|
2020-10-27 16:46:38 +01:00
|
|
|
desc 'set stp_price_id to plans'
|
|
|
|
task plans_prices: :environment do
|
|
|
|
puts 'No plans, exiting...' and return if Plan.count.zero?
|
|
|
|
|
|
|
|
w = StripeWorker.new
|
|
|
|
Plan.all.each do |p|
|
|
|
|
w.perform(:create_stripe_price, p)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-29 10:57:32 +02:00
|
|
|
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
|