Class: MailUncapturedDonationsJob

Inherits:
ApplicationJob show all
Defined in:
/var/apps/qpixel/app/jobs/mail_uncaptured_donations_job.rb

Instance Method Summary collapse

Methods inherited from ApplicationJob

#exec, #initialize, #logger

Constructor Details

This class inherits a constructor from ApplicationJob

Instance Method Details

#perform(*_args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File '/var/apps/qpixel/app/jobs/mail_uncaptured_donations_job.rb', line 4

def perform(*_args)
  intents = Stripe::PaymentIntent.list
  mailed = 0
  errors = 0
  intents.auto_paging_each do |pi|
    break unless Time.at(pi.created) >= 24.hours.ago
    next unless pi.status == 'requires_payment_method'
    next unless pi.['user_id'].present?
    next if pi.['emailed'].present?

    user = User.find(pi.['user_id'])
    symbol = { 'GBP' => '£', 'USD' => '$', 'EUR' => '' }[pi.currency.upcase]
    amount = pi.amount / 100
    DonationMailer.with(currency: symbol, amount: amount, email: user.email, name: user.username, intent: pi)
                  .donation_uncaptured.deliver_now
    Stripe::PaymentIntent.update(pi.id, { metadata: { emailed: true } })
    logger.debug "Mailed ##{user.id} for PaymentIntent #{pi.id}"
    mailed += 1
  rescue => e
    Stripe::PaymentIntent.update(pi.id, { metadata: { email_error: e.message.to_s } })
    logger.error "Error sending email for user #{user.id}, PaymentIntent #{pi.id}: #{e.message}"
    errors += 1
  end
  logger.info "Mailed #{mailed} donations, #{errors} errors"
end