Class: Tang::ImportCustomersJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/tang/import_customers_job.rb

Instance Method Summary collapse

Instance Method Details

#import_customer(stripe_customer) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/jobs/tang/import_customers_job.rb', line 13

def import_customer(stripe_customer)
  customer = Tang.customer_class.find_by(email: stripe_customer.email)
  if customer.present?
    customer = update_customer(customer, stripe_customer)

    # import card
    if stripe_customer.default_source.present?
      stripe_card = Stripe::Customer.retrieve_source(
        stripe_customer.id,
        stripe_customer.default_source,
      )
      Card.from_stripe(stripe_card, customer)
    end
  end
end

#perform(starting_after = nil) ⇒ Object



5
6
7
8
9
10
11
# File 'app/jobs/tang/import_customers_job.rb', line 5

def perform(starting_after = nil)
  stripe_customers = Stripe::Customer.list(limit: 100, starting_after: starting_after)
  stripe_customers.each do |stripe_customer|
    import_customer(stripe_customer)
  end
  Tang::ImportCustomersJob.perform_now(stripe_customers.data.last.id) if stripe_customers.has_more
end

#update_customer(customer, stripe_customer) ⇒ Object



29
30
31
32
33
34
# File 'app/jobs/tang/import_customers_job.rb', line 29

def update_customer(customer, stripe_customer)
  customer.stripe_id = stripe_customer.id if customer.stripe_id.nil?
  customer.coupon = Coupon.find_by(stripe_id: stripe_customer.discount.coupon.id) if stripe_customer.discount.present?
  customer.save
  return customer
end