Class: Gold::AfterAuthenticateJob

Inherits:
ApplicationJob show all
Includes:
Outcomes
Defined in:
app/jobs/gold/after_authenticate_job.rb

Overview

Run when the app is installed, reinstalled, or a user logs in. This is essentially how Gold is bootstrapped, so there is an assumption made here (the shop class has a ‘#with_shopify_session` method). This is true with the default Shop class that Shopify sets up, but if that is different for your environment, you will need to create your own custom version of this job.

Constant Summary

Constants included from Outcomes

Outcomes::AcceptedCharge, Outcomes::AcceptedTerms, Outcomes::ActiveCharge, Outcomes::CannotApplyDiscount, Outcomes::CannotIssueCredit, Outcomes::CannotProcessCharge, Outcomes::CannotSelectTier, Outcomes::ChargeNeeded, Outcomes::ChargeNotNeeded, Outcomes::DeclinedCharge, Outcomes::DeclinedTerms, Outcomes::ExpiredCharge, Outcomes::FrozenCharge, Outcomes::MismatchCharge, Outcomes::MissingCharge, Outcomes::PendingCharge, Outcomes::SameDiscount, Outcomes::SameTier, Outcomes::TierApplied, Outcomes::TierNotFound, Outcomes::Uninstalled

Instance Method Summary collapse

Instance Method Details

#perform(shop_domain:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/jobs/gold/after_authenticate_job.rb', line 11

def perform(shop_domain:)
  # Look up the shop associated with the Shopify session.
  shop = Gold.shop_class.find_by!(Gold.shop_domain_attribute => shop_domain)

  # Create a billing instance if this store is newly installed or already
  # cleaned up.
  billing = Billing.find_or_create_by!(shop: shop)

  shop.with_shopify_session do
    outcome = InstallOp.new(billing).call

    case outcome
    when ChargeNeeded, MissingCharge
      process_charge_url = Engine.routes.url_helpers.process_charge_url
      logger.info("#{shop_domain} has reinstalled and needs to approve a " \
                  "charge for their selected tier")
      ChargeOp.new(billing, process_charge_url).call
    when Success
      logger.info("#{shop_domain} successfully installed the app")
    end

    outcome
  end
end