Class: DiscoApp::ChargesService

Inherits:
Object
  • Object
show all
Defined in:
app/services/disco_app/charges_service.rb

Class Method Summary collapse

Class Method Details

.activate(shop, charge) ⇒ Object

Attempt to activate the given Shopify charge for the given Shop using the Shopify API. Returns true on successful activation, false otherwise.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/services/disco_app/charges_service.rb', line 39

def self.activate(shop, charge)
  begin
    # Start by fetching the Shopify charge to check that it was accepted.
    shopify_charge = shop.temp {
      charge.subscription.shopify_charge_class.find(charge.shopify_id)
    }

    # Update the status of the local charge based on the Shopify charge.
    charge.send("#{shopify_charge.status}!") if charge.respond_to? "#{shopify_charge.status}!"

    # If the charge wasn't accepted, fail and return.
    return false unless charge.accepted?

    # If the charge was indeed accepted, activate it via Shopify.
    charge.shop.temp {
      shopify_charge.activate
    }

    # If the charge was recurring, make sure that all other local recurring
    # charges are marked inactive.
    if charge.recurring?
      self.cancel_recurring_charges(shop, charge)
    end

    charge.active!

    true
  rescue
    false
  end
end

.cancel_recurring_charges(shop, charge = nil) ⇒ Object

Cancel all recurring charges for the given shop. If the optional charge parameter is given, it will be excluded from the cancellation.



73
74
75
76
77
78
79
# File 'app/services/disco_app/charges_service.rb', line 73

def self.cancel_recurring_charges(shop, charge = nil)
  charges = DiscoApp::RecurringApplicationCharge.where(shop: shop)
  if charge.present?
    charges = charges.where.not(id: charge)
  end
  charges.update_all(status: DiscoApp::RecurringApplicationCharge.statuses[:cancelled])
end

.create(shop, subscription) ⇒ Object

Create the appropriate type of Shopify charge for the given subscription (either one-time or recurring) and return.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/disco_app/charges_service.rb', line 5

def self.create(shop, subscription)
  # Create the charge object locally first.
  charge = subscription.charge_class.create!(
    shop: shop,
    subscription: subscription,
  )

  # Create the charge object on Shopify.
  shopify_charge = shop.temp {
    subscription.shopify_charge_class.create(
      name: subscription.plan.name,
      price: '%.2f' % (subscription.amount.to_f / 100.0),
      trial_days: subscription.plan.has_trial? ? subscription.trial_period_days : nil,
      return_url: charge.activate_url,
      test: !DiscoApp.configuration.real_charges?
    )
  }

  # If we couldn't create the charge on Shopify, return nil.
  if shopify_charge.nil?
    return nil
  end

  # Update the local record of the charge from Shopify's created charge, then
  # return it.
  charge.update(
    shopify_id: shopify_charge.id,
    confirmation_url: shopify_charge.confirmation_url
  )
  charge
end