Class: Gold::BillingMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/gold/billing_migrator.rb

Overview

Migrates previous systems to Gold for a specific shop

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shop, tier_id, trial_starts_at = nil) ⇒ BillingMigrator

Returns a new instance of BillingMigrator.



11
12
13
14
15
# File 'lib/gold/billing_migrator.rb', line 11

def initialize(shop, tier_id, trial_starts_at = nil)
  @shop = shop
  @tier_id = tier_id.to_sym
  @trial_starts_at = trial_starts_at
end

Instance Attribute Details

#billingObject (readonly)

Returns the value of attribute billing.



6
7
8
# File 'lib/gold/billing_migrator.rb', line 6

def billing
  @billing
end

#shopObject (readonly)

Returns the value of attribute shop.



6
7
8
# File 'lib/gold/billing_migrator.rb', line 6

def shop
  @shop
end

#tierObject (readonly)

Returns the value of attribute tier.



6
7
8
# File 'lib/gold/billing_migrator.rb', line 6

def tier
  @tier
end

#tier_idObject (readonly)

Returns the value of attribute tier_id.



6
7
8
# File 'lib/gold/billing_migrator.rb', line 6

def tier_id
  @tier_id
end

Instance Method Details

#lookup_tier!Object

Finds an appropriate tier based on tier_id and current charge



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gold/billing_migrator.rb', line 18

def lookup_tier!
  charge = ShopifyAPI::RecurringApplicationCharge.current

  # First attempt to find a matching tier with name and price
  tier = Tier.all.find do |t|
    (t.id == @tier_id || t.parent&.id == @tier_id) &&
      tier_matches_charge_price(t, charge)
  end

  # Fallback on just tier id
  tier ||= Tier.find(@tier_id)

  # Raise an exception if no tier is found
  raise Outcomes::TierNotFound unless tier

  @tier = tier
end

#migrate!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gold/billing_migrator.rb', line 36

def migrate!
  if Billing.find_by(shop_id: shop.id)
    Gold.logger.warn("Attempted to migrate shop '#{@shop.shopify_domain}'" \
                     ", but Billing record already exists")
    return false
  end

  @billing = Billing.create!(
    shop: @shop,
    trial_starts_at: @trial_starts_at
  )

  lookup_tier!
  accept_terms
  select_tier

  Gold.logger.info("Migrated shop '#{@shop.shopify_domain}', state is " \
                   "'#{@billing.current_state}', tier is '#{@tier.id}'")

  true
rescue ActiveResource::UnauthorizedAccess,
       ActiveResource::ForbiddenAccess,
       ActiveResource::ResourceNotFound
  # Shop is uninstalled, that's OK
  Gold.logger.info("Shop '#{@shop.shopify_domain}' is uninstalled")
  true
end