Module: ODDB::State::PayPal::Checkout

Includes:
Admin::LoginMethods
Included in:
Drugs::RegisterDownload, User::RegisterDownload, User::RegisterPowerUser, User::RenewPowerUser
Defined in:
src/state/paypal/checkout.rb

Constant Summary

CURRENCY =
'EUR'

Instance Method Summary (collapse)

Methods included from Admin::LoginMethods

#autologin, #login

Instance Method Details

- (Object) ajax_autofill



19
20
21
22
23
24
25
26
27
# File 'src/state/paypal/checkout.rb', line 19

def ajax_autofill
   = @session.user_input(:email)
  prefs = {}
  keys = checkout_keys()
  keys.delete(:email)
  prefs.update @session.yus_get_preferences(, keys)
  prefs.store(:email, ) unless(prefs.empty?)
  AjaxCheckout.new(@session, prefs)
end

- (Object) checkout



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'src/state/paypal/checkout.rb', line 28

def checkout
  ## its possible that we know this user already -> log them in.
  missing_keys = [:email, :pass] - @session.input_keys
  if(@session.logged_in?)
    @user = @session.user
  elsif(missing_keys.empty?)
    begin
      @user ||= @session.
      reconsider_permissions(@user, self)
      @session.(@user)
    rescue Yus::UnknownEntityError 
      # ignore: in this case we simply create a new user in 'create_user'
    rescue Yus::AuthenticationError
      @errors.store(:pass, create_error(:e_authentication_error, :pass, nil))
    end
  end
  input = user_input(checkout_keys(), checkout_mandatory())
  if(error?)
    self
  else
    create_user(input)
    State::PayPal::Redirect.new(@session, create_invoice(input))
  end
rescue SBSM::ProcessingError => err
  @errors.store(err.key, err)
  self
end

- (Object) checkout_keys



62
63
64
# File 'src/state/paypal/checkout.rb', line 62

def checkout_keys
  checkout_mandatory()
end

- (Object) checkout_mandatory



55
56
57
58
59
60
61
# File 'src/state/paypal/checkout.rb', line 55

def checkout_mandatory
  keys = [ :salutation, :name_last, :name_first, ]
  unless(@session.logged_in?)
    keys.push(:email, :pass, :set_pass_2 )
  end
  keys
end

- (Object) create_invoice(input)



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'src/state/paypal/checkout.rb', line 65

def create_invoice(input)
  pointer = Persistence::Pointer.new([:invoice])
  args = {
    :currency => currency,
    :yus_name => input[:email] || @user.,
  }
  invoice = @session.app.update(pointer.creator, args, unique_email)
  @model.items.each { |abstract|
    item_ptr = invoice.pointer + [:item]
    time = Time.now
    file = abstract.text
    duration = abstract.duration
    expiry = InvoiceItem.expiry_time(duration, time)
    data = {
      :duration     => duration,
      :expiry_time  => expiry,
      :price        => abstract.price,
      :quantity     => abstract.quantity,
      :text         => file,
      :time         => time,
      :type         => abstract.type,
      :data         => abstract.data,
      :vat_rate     => VAT_RATE,
    }
    item = @session.app.update(item_ptr.creator, data, unique_email)
  }
  invoice
end

- (Object) create_user(input)



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'src/state/paypal/checkout.rb', line 93

def create_user(input)
  hash = input.dup 
  ## don't store passwords in cookie vars...
  hash.delete(:set_pass_2)
  pass = hash.delete(:pass)
  ## but store the rest of the input there
  hash.each { |key, val| @session.set_cookie_input(key, val) }
   = hash.delete(:email)
  unless(@user.is_a?(YusUser))
    @user = @session.app.yus_create_user(, pass)
  end
  hash.delete_if { |key, value| value.to_s.empty? }
  @user.set_preferences(hash) unless(hash.empty?)
  reconsider_permissions(@user, self)
  @session.(@user)
rescue Yus::DuplicateNameError => e
  raise create_error(:e_duplicate_email, :email, input[:email])
rescue RuntimeError, Yus::YusError => e
  raise create_error(e.message, :email, input[:email])
end

- (Object) currency



113
114
115
# File 'src/state/paypal/checkout.rb', line 113

def currency
  self.class.const_get(:CURRENCY)
end

- (Object) user_input(keys, mandatory)



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'src/state/paypal/checkout.rb', line 116

def user_input(keys, mandatory)
  input = super
  pass1 = input[:pass]
  pass2 = input[:set_pass_2]
  unless(@user || pass1 == pass2)
    err1 = create_error(:e_non_matching_set_pass, :pass, pass1)
    err2 = create_error(:e_non_matching_set_pass, :set_pass_2, pass2)
    @errors.store(:pass, err1)
    @errors.store(:set_pass_2, err2)
  end
  msg = 'e_need_all_input'
  @errors.each { |key, err|
    if(err.message.match(/^e_missing_/u))
      @errors.store(key, create_error(msg, key, err.value))
    end
  }
  input
end