Module: ODDB::Util::Ipn

Defined in:
src/util/ipn.rb

Constant Summary

RECIPIENTS =
[ 'hwyss@ywesee.com', ]

Class Method Summary (collapse)

Class Method Details

+ (Object) format_invoice(invoice, lookandfeel)



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'src/util/ipn.rb', line 173

def Ipn.format_invoice(invoice, lookandfeel)
  lines = [lookandfeel.lookup(:invoice_origin), nil]
  qsizes = []
  tsizes = []
  nsizes = []
  downloads = invoice.items.values.collect { |item|
    qstr = sprintf('%i x', item.quantity)
    qsizes.push(qstr.size)
    tstr = item.text
    tsizes.push(tstr.size)
    nstr = sprintf('%3.2f', item.total_netto)
    nsizes.push(nstr.size)
    [qstr, tstr, nstr]
  }
  tstr = lookandfeel.lookup(:total_netto)
  tsizes.push(tstr.size)
  nstr = sprintf('%3.2f', invoice.total_netto)
  nsizes.push(nstr.size)
  netto_line = [nil, tstr, nstr]
  tstr = lookandfeel.lookup(:vat)
  tsizes.push(tstr.size)
  nstr = sprintf('%3.2f', invoice.vat)
  nsizes.push(nstr.size)
  vat_line = [nil, tstr, nstr]
  tstr = lookandfeel.lookup(:total_brutto)
  tsizes.push(tstr.size)
  nstr = sprintf('%3.2f', invoice.total_brutto)
  nsizes.push(nstr.size)
  brutto_line = [nil, tstr, nstr]

  sizes = [qsizes.max, tsizes.max, nsizes.max]

  width = sizes.inject(7) { |a,b| a + b }
  
  dline = "=" * width
  sline = "-" * width

  lines.push(dline)
  lines += downloads.collect { |data|
    format_line(sizes, data)
  }
  lines.push(sline)
  lines.push(format_line(sizes, netto_line))
  lines.push(sline)
  lines.push(format_line(sizes, vat_line))
  lines.push(dline)
  lines.push(format_line(sizes, brutto_line))
  lines.push(dline)
  lines.push(nil)
  lines.join("\n")
end

+ (Object) format_line(sizes, data)



224
225
226
227
# File 'src/util/ipn.rb', line 224

def Ipn.format_line(sizes, data)
  sprintf("%#{sizes.at(0)}s %-#{sizes.at(1)}s  EUR %#{sizes.at(2)}s",
    *data)
end

+ (Object) lookandfeel_stub



10
11
12
13
14
# File 'src/util/ipn.rb', line 10

def Ipn.lookandfeel_stub
  session = Plugin::SessionStub.new($oddb)
  session.language = 'de'
  session.lookandfeel = LookandfeelBase.new(session)
end

+ (Object) process(notification, system)



15
16
17
18
19
20
21
22
23
24
25
# File 'src/util/ipn.rb', line 15

def Ipn.process(notification, system)
  id = notification.params["invoice"]
  invoice = system.invoice(id.to_i) or raise "unknown invoice '#{id}'"
  if(notification.complete?)
    Ipn.process_invoice invoice, system
  else
    invoice.ipn = notification
  end
  invoice.odba_store
  invoice
end

+ (Object) process_invoice(invoice, system)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'src/util/ipn.rb', line 26

def Ipn.process_invoice(invoice, system)
  invoice.payment_received!
  yus_name = invoice.yus_name
  invoice.items.each_value { |item|
    case item.type
    when :poweruser
      system.yus_set_preference(yus_name, 'poweruser_duration',
                               invoice.max_duration)
      system.yus_grant(yus_name, 'login', 'org.oddb.PowerUser')
      system.yus_grant(yus_name, 'view', 'org.oddb', item.expiry_time)
    when :download
      system.yus_grant(yus_name, 'download', item.text, 
                       item.expiry_time)
    end
  }
  YdimPlugin.new(system).inject(invoice)
  invoice.types.each { |type|
    case type
    when :poweruser
      send_poweruser_notification(invoice)
    else
      send_download_notification(invoice)
      send_download_seller_notification(invoice)
    end
  }
end

+ (Object) send_download_notification(invoice)



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
83
84
85
86
87
88
89
# File 'src/util/ipn.rb', line 52

def Ipn.send_download_notification(invoice)
  send_notification(invoice) { |outgoing, recipient, lookandfeel|
    outgoing.subject = lookandfeel.lookup(:download_mail_subject)
    urls = invoice.items.values.collect { |item|
      data = {
        :email      =>  recipient,
        :invoice    =>  invoice.oid,
        :filename    =>  item.text,
      }
      url = lookandfeel._event_url(:download, data)
      protocol = DOWNLOAD_PROTOCOLS.find do |prt|
        %r{#{prt}}.match(item.text)
      end
      if protocol
        parsed = URI.parse url
        parsed.scheme = protocol
        [url, parsed.to_s]
      else
        url
      end
    }.flatten
    salut = lookandfeel.lookup(yus(recipient, :salutation))
    suffix = (urls.size == 1) ? 's' : 'p'
    lines = [
      lookandfeel.lookup(:download_mail_body),
      lookandfeel.lookup("download_mail_instr_#{suffix}"),
    ]
    parts = [
      lookandfeel.lookup(:download_mail_salut, salut, 
                         yus(recipient, :name_last)),
      lines.join("\n"),
      urls.join("\n"), 
      lookandfeel.lookup(:download_mail_feedback),
      format_invoice(invoice, lookandfeel),
    ]
    outgoing.body = parts.join("\n\n")
  }
end

+ (Object) send_download_seller_notification(invoice)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'src/util/ipn.rb', line 90

def Ipn.send_download_seller_notification(invoice)
  if(name = invoice.yus_name)
    config = ODDB.config
    lookandfeel = lookandfeel_stub
    recipient = PAYPAL_RECEIVER
    outgoing = TMail::Mail.new
    outgoing.set_content_type('text', 'plain', 'charset'=>'UTF-8')
    outgoing.to = [recipient]
    outgoing.from = config.mail_from
    outgoing.subject = lookandfeel.lookup(:download_mail_subject)
    salut = lookandfeel.lookup(yus(name, :salutation))
    company = yus(name, :company_name)
    business = lookandfeel.lookup(yus(name, :business_area))
    if(!company.to_s.strip.empty?)
      business = "#{company} (#{business})"
    end
    body = [
      [salut, yus(name, :name_first), yus(name, :name_last)].join(' '),
      business,
      yus(name, :address),
      [yus(name, :plz), yus(name, :city)].join(' '),
      yus(name, :phone),
      yus(name, :email),
    ].compact
    body.push(nil)
    body.push(format_invoice(invoice, lookandfeel))
    outgoing.body = body.join("\n")
    outgoing.date = Time.now
    outgoing['User-Agent'] = 'ODDB Download'
    recipients = [recipient] + RECIPIENTS
    Net::SMTP.start(config.smtp_server, config.smtp_port, config.smtp_domain,
                    config.smtp_user, config.smtp_pass,
                    config.smtp_authtype) { |smtp|
      smtp.sendmail(outgoing.encoded, config.smtp_user, recipients)
    }
  end
rescue StandardError => e
  puts e.class
  puts e.message
  puts e.backtrace
end

+ (Object) send_notification(invoice, &block)



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'src/util/ipn.rb', line 149

def Ipn.send_notification(invoice, &block)
  if(recipient = invoice.yus_name)
    lookandfeel = lookandfeel_stub
    outgoing = TMail::Mail.new
    outgoing.set_content_type('text', 'plain', 'charset'=>'UTF-8')
    outgoing.to = [recipient]
    outgoing.from = MAIL_FROM
    outgoing.date = Time.now
    outgoing['User-Agent'] = 'ODDB Paypal-IPN'

    block.call(outgoing, recipient, lookandfeel) 

    recipients = ([recipient] + RECIPIENTS).uniq
    Net::SMTP.start(config.smtp_server, config.smtp_port, config.smtp_domain,
                    config.smtp_user, config.smtp_pass,
                    config.smtp_authtype) { |smtp|
      smtp.sendmail(outgoing.encoded, config.smtp_user, recipients)
    }
  end
rescue StandardError => e
  puts e.class
  puts e.message
  puts e.backtrace
end

+ (Object) send_poweruser_notification(invoice)



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'src/util/ipn.rb', line 131

def Ipn.send_poweruser_notification(invoice)
  send_notification(invoice) { |outgoing, recipient, lookandfeel|
    outgoing.subject = lookandfeel.lookup(:poweruser_mail_subject)
    salut = lookandfeel.lookup(yus(recipient, :salutation))
    item = invoice.item_by_text('unlimited access')
    dkey = "poweruser_duration_#{item.duration.to_i}"
    duration = lookandfeel.lookup(dkey)
    parts = [
      lookandfeel.lookup(:poweruser_mail_salut, salut, 
                         yus(recipient, :name_last)),
      lookandfeel.lookup(:poweruser_mail_body),
      lookandfeel.lookup(:poweruser_mail_instr, duration,
        lookandfeel._event_url(:login_form)),
      lookandfeel.lookup(:poweruser_regulatory),
    ]
    outgoing.body = parts.join("\n\n")
  }
end

+ (Object) yus(recipient, key)



228
229
230
# File 'src/util/ipn.rb', line 228

def Ipn.yus(recipient, key)
  $oddb.yus_get_preference(recipient, key)
end