Class: SubscriptionFu::Transaction

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/subscription_fu/transaction.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_activation(gateway, initiator) ⇒ Object



20
21
22
# File 'app/models/subscription_fu/transaction.rb', line 20

def self.create_activation(gateway, initiator)
  create!(:initiator => initiator, :gateway => gateway, :status => 'initiated', :action => "activation")
end

.create_cancellation(initiator, related_transaction, subscription) ⇒ Object



24
25
26
27
# File 'app/models/subscription_fu/transaction.rb', line 24

def self.create_cancellation(initiator, related_transaction, subscription)
  gateway = subscription.paypal_profile_id.blank? ? 'nogw' : 'paypal'
  create!(:initiator => initiator, :gateway => gateway, :status => 'initiated', :action => "cancellation", :related_transaction => related_transaction)
end

Instance Method Details

#abortObject



84
85
86
87
88
89
# File 'app/models/subscription_fu/transaction.rb', line 84

def abort
  raise "abort only available in initiated state, but: #{status}" unless status == "initiated"
  update_attributes(:status => "aborted")
  related_transactions.each { |t| t.abort }
  true
end

#complete(opts = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/subscription_fu/transaction.rb', line 48

def complete(opts = {})
  raise "complete only available in initiated state, but: #{status}" unless status == "initiated"

  success = true
  begin
    send("complete_#{action}_#{gateway}", opts)
    update_attributes!(:status => "complete")
  rescue Exception => err
    data = {
      :subscription => subscription.inspect,
      :transaction => self.inspect }
    if err.instance_of?(Paypal::Exception::APIError)
      data[:paypal_response] = err.response.inspect
      data[:paypal_error_message] = err.message
    elsif err.instance_of?(Paypal::Exception::HttpError)
      data[:http_error_code] = err.code
      data[:http_error_message] = err.message
    end

    if defined? ::ExceptionNotifier
      ::ExceptionNotifier::Notifier.background_exception_notification(err, :data => data).deliver
    elsif defined? ::HoptoadNotifier
      ::HoptoadNotifier.notify(err, :parameters => data)
    else
      logger.info("could not complete #{action} on #{gateway}")
      logger.info("details: #{data.inspect}")
      logger.warn(err)
      logger.debug(err.backtrace.join("\n"))
    end
    update_attributes!(:status => "failed")
    related_transactions.each { |t| t.abort }
    success = false
  end
  success
end

#initiator_emailObject



29
30
31
# File 'app/models/subscription_fu/transaction.rb', line 29

def initiator_email
  initiator.email if initiator.respond_to?(:email)
end

#needs_authorization?Boolean

billing API

Returns:

  • (Boolean)


35
36
37
# File 'app/models/subscription_fu/transaction.rb', line 35

def needs_authorization?
  gateway == "paypal" && action == "activation"
end

#start_checkout(return_url, cancel_url) ⇒ Object



39
40
41
42
43
44
45
46
# File 'app/models/subscription_fu/transaction.rb', line 39

def start_checkout(return_url, cancel_url)
  raise "start_checkout is only for activation" unless action == "activation"
  raise "start_checkout is only for non-activated subscriptions" if sub_activated?
  raise "start_checkout already called once, have a token" unless identifier.blank?
  raise "start_checkout only available in initiated state, but: #{status}" unless status == "initiated"

  send("start_checkout_#{gateway}", return_url, cancel_url)
end