1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/app/workers/sync_objects_on_stripe_worker.rb

74 lines
2.6 KiB
Ruby
Raw Normal View History

2020-06-09 18:51:57 +02:00
# frozen_string_literal: true
# This worker perform various requests to the Stripe API (payment service)
2021-06-04 18:26:20 +02:00
class SyncObjectsOnStripeWorker
2021-06-21 10:10:55 +02:00
require 'stripe/service'
2020-06-09 18:51:57 +02:00
include Sidekiq::Worker
sidekiq_options lock: :until_executed, on_conflict: :reject, queue: :stripe
def perform(notify_user_id = nil)
2022-11-21 16:56:28 +01:00
unless Stripe::Helper.enabled?
Rails.logger.warn('A request to sync payment related objects on Stripe will not run because Stripe is not enabled')
return false
end
w = StripeWorker.new
sync_customers(w)
sync_coupons
sync_objects(w)
Rails.logger.info 'Sync is done'
return unless notify_user_id
Rails.logger.info "Notify user #{notify_user_id}"
user = User.find(notify_user_id)
NotificationCenter.call type: :notify_admin_objects_stripe_sync,
receiver: user,
attached_object: user
end
def sync_customers(worker)
Rails.logger.info 'We create all non-existing customers on stripe. This may take a while...'
2020-06-09 18:51:57 +02:00
total = User.online_payers.count
User.online_payers.each_with_index do |member, index|
Rails.logger.info "#{index} / #{total}"
2020-06-09 18:51:57 +02:00
begin
stp_customer = member.payment_gateway_object&.gateway_object&.retrieve
2022-11-21 16:56:28 +01:00
worker.perform(:create_stripe_customer, member.id) if stp_customer.nil? || stp_customer[:deleted]
2020-06-09 18:51:57 +02:00
rescue Stripe::InvalidRequestError
begin
2022-11-21 16:56:28 +01:00
worker.perform(:create_stripe_customer, member.id)
rescue Stripe::InvalidRequestError => e
Rails.logger.error "Unable to create the customer #{member.id} do to a Stripe error: #{e}"
end
2020-06-09 18:51:57 +02:00
end
end
2022-11-21 16:56:28 +01:00
end
2021-06-04 18:26:20 +02:00
2022-11-21 16:56:28 +01:00
def sync_coupons
Rails.logger.info 'We create all non-existing coupons on stripe. This may take a while...'
2021-06-04 18:26:20 +02:00
total = Coupon.all.count
Coupon.all.each_with_index do |coupon, index|
Rails.logger.info "#{index} / #{total}"
2021-06-16 15:48:45 +02:00
Stripe::Coupon.retrieve(coupon.code, api_key: Setting.get('stripe_secret_key'))
2021-06-04 18:26:20 +02:00
rescue Stripe::InvalidRequestError
2021-06-21 10:50:37 +02:00
begin
Stripe::Service.new.create_coupon(coupon.id)
rescue Stripe::InvalidRequestError => e
Rails.logger.error "Unable to create coupon #{coupon.code} on stripe: #{e}"
2021-06-21 10:50:37 +02:00
end
2021-06-04 18:26:20 +02:00
end
2022-11-21 16:56:28 +01:00
end
2021-06-04 18:26:20 +02:00
2022-11-21 16:56:28 +01:00
def sync_objects(worker)
2021-06-04 18:26:20 +02:00
[Machine, Training, Space, Plan].each do |klass|
Rails.logger.info "We create all non-existing #{klass} on stripe. This may take a while..."
2021-06-04 18:26:20 +02:00
total = klass.all.count
klass.all.each_with_index do |item, index|
Rails.logger.info "#{index} / #{total}"
2022-11-21 16:56:28 +01:00
worker.perform(:create_or_update_stp_product, klass.name, item.id)
2021-06-04 18:26:20 +02:00
end
end
2020-06-09 18:51:57 +02:00
end
end