Module: Pay::Sync

Overview

Shared skeleton for the processor Charge.sync, Subscription.sync, and PaymentMethod.sync class methods

Instance Method Summary collapse

Instance Method Details

#find_pay_customer(processor_id) ⇒ Object

The Pay::Customer for this processor, or nil when the ID is blank or the customer isn't in the database



20
21
22
# File 'lib/pay/sync.rb', line 20

def find_pay_customer(processor_id)
  Pay::Customer.find_by(processor: pay_processor, processor_id: processor_id) if processor_id.present?
end

#pay_processorObject

"stripe" for Pay::Stripe::Charge, "paddle_billing" for Pay::PaddleBilling::Subscription, and so on



25
26
27
# File 'lib/pay/sync.rb', line 25

def pay_processor
  name.deconstantize.demodulize.underscore
end

#sync_with_retries(retries: 1) ⇒ Object

Runs the sync block, retrying it when a webhook and an API call race to create the same record. The block retrieves the processor's object itself when the caller didn't pass one in, so each retry gets a fresh read.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pay/sync.rb', line 7

def sync_with_retries(retries: 1)
  try = 0
  begin
    yield
  rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
    try += 1
    raise if try > retries
    sleep 0.15 * try
    retry
  end
end