From 16087d3aa3fdff0fe66c869316eccc3bbf00a671 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 21 Nov 2022 16:56:28 +0100 Subject: [PATCH] (bug) objects not sync on Stripe --- CHANGELOG.md | 1 + app/services/setting_service.rb | 2 +- app/workers/sync_objects_on_stripe_worker.rb | 40 ++++++++++++++------ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0deb6f449..320ead133 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Fix a bug: broken display of machines pages - Fix a bug: some automated tests were randomly failing because ElasticSearch was not synced +- Fix a bug: payment related objects are not synced on Stripe when enabling the online payment module ## v5.5.4 2022 November 17 diff --git a/app/services/setting_service.rb b/app/services/setting_service.rb index e8b1ba092..c90048ac6 100644 --- a/app/services/setting_service.rb +++ b/app/services/setting_service.rb @@ -46,7 +46,7 @@ class SettingService # sync all objects on stripe def sync_stripe_objects(setting) - return unless setting.name == 'stripe_secret_key' + return unless %w[stripe_secret_key online_payment_module].include?(setting.name) SyncObjectsOnStripeWorker.perform_async(setting.history_values.last&.invoicing_profile&.user&.id) end diff --git a/app/workers/sync_objects_on_stripe_worker.rb b/app/workers/sync_objects_on_stripe_worker.rb index 62cf8826b..a1950a8b4 100644 --- a/app/workers/sync_objects_on_stripe_worker.rb +++ b/app/workers/sync_objects_on_stripe_worker.rb @@ -7,22 +7,45 @@ class SyncObjectsOnStripeWorker sidekiq_options lock: :until_executed, on_conflict: :reject, queue: :stripe def perform(notify_user_id = nil) + 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...' total = User.online_payers.count User.online_payers.each_with_index do |member, index| Rails.logger.info "#{index} / #{total}" begin stp_customer = member.payment_gateway_object&.gateway_object&.retrieve - StripeWorker.new.create_stripe_customer(member.id) if stp_customer.nil? || stp_customer[:deleted] + worker.perform(:create_stripe_customer, member.id) if stp_customer.nil? || stp_customer[:deleted] rescue Stripe::InvalidRequestError begin - StripeWorker.new.create_stripe_customer(member.id) + 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 end end + end + def sync_coupons Rails.logger.info 'We create all non-existing coupons on stripe. This may take a while...' total = Coupon.all.count Coupon.all.each_with_index do |coupon, index| @@ -35,23 +58,16 @@ class SyncObjectsOnStripeWorker Rails.logger.error "Unable to create coupon #{coupon.code} on stripe: #{e}" end end + end - w = StripeWorker.new + def sync_objects(worker) [Machine, Training, Space, Plan].each do |klass| Rails.logger.info "We create all non-existing #{klass} on stripe. This may take a while..." total = klass.all.count klass.all.each_with_index do |item, index| Rails.logger.info "#{index} / #{total}" - w.perform(:create_or_update_stp_product, klass.name, item.id) + worker.perform(:create_or_update_stp_product, klass.name, item.id) end end - 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 end