Sha256: 53f432502d93fc22d529c5f4ad1031887ac487710432f66af5508eedf7fdbfba

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

class StripeModelCallbacks::BaseService < ServicePattern::Service
  def self.reported_execute!(*args, **opts, &blk)
    with_exception_notifications do
      response = execute(*args, **opts, &blk)
      raise response.errors.join(". ") unless response.success?

      response
    end
  end

  def self.with_exception_notifications
    yield
  rescue => e # rubocop:disable Style/RescueStandardError
    Rails.logger.error "ERROR: #{e.message}"

    cleaned = Rails.backtrace_cleaner.clean(e.backtrace)
    if cleaned.any?
      Rails.logger.error cleaned
    else
      Rails.logger.error e.backtrace.join("\n")
    end

    ExceptionNotifier.notify_exception(e) if Object.const_defined?("ExceptionNotifier")
    raise e
  end

  def self.execute_with_advisory_lock!(*args, **opts, &blk)
    # The difference between the stripe events is about a few milliseconds - with advisory_lock
    # we will prevent from creating duplicated objects due to race condition.
    # https://stripe.com/docs/webhooks/best-practices#event-ordering
    with_exception_notifications do
      StripeModelCallbacks::ApplicationRecord.with_advisory_lock(advisory_lock_name(*args, **opts)) do
        response = execute(*args, **opts, &blk)
        raise response.errors.join(". ") unless response.success?

        response
      end
    end
  end

  def self.advisory_lock_name(event:)
    stripe_event_data = event.data.object

    ["stripe", stripe_event_data.object, "id", advisory_lock_id(stripe_event_data)].join("-")
  end

  def self.advisory_lock_id(stripe_event_data)
    return stripe_event_data.id if stripe_event_data.respond_to?(:id)
    return stripe_event_data.coupon.id if stripe_event_data.object == "discount"
    return unless stripe_event_data.respond_to?(:customer)

    if stripe_event_data.customer.is_a?(String)
      stripe_event_data.customer
    else
      stripe_event_data.customer.id
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stripe_model_callbacks-0.1.4 app/services/stripe_model_callbacks/base_service.rb