-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.coffee
78 lines (56 loc) · 1.92 KB
/
index.coffee
1
2
3
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
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
request = require 'request'
querystring = require 'querystring'
_ = require 'underscore'
util = require 'util'
API_LIVE_URL = 'https://api-3t.paypal.com/nvp'
API_SANDBOX_URL = 'https://api-3t.sandbox.paypal.com/nvp'
API_VERSION = 94
class PayPal
constructor: (options) ->
@apiUrl = if options.live then API_LIVE_URL else API_SANDBOX_URL
@username = options.username
@password = options.password
@signature = options.signature
call: (method, parameters, callback) =>
processResponse = (text) ->
data = querystring.decode text
return (callback? 'invalid server response') if not data?
response = {}
extractValue = (key, value) ->
if not isNaN(value)
value = parseFloat(value)
if key == 'TIMESTAMP' or /.+DATE$/.test key
date = new Date(value)
value = date if date and not isNaN date.getYear()
return value
for key in ((k for k of data).filter (k) -> /^[A-Z]+$/.test(k))
response[key] = extractValue(key, data[key])
rx = /^L_([A-Z]+)(\d+)$/
params = ((k for k of data).map (key) ->
return if not rx.test(key)
parts = rx.exec(key)
if parts then [parts[1], parseInt(parts[2]), key] else null
).filter (p) -> p?
ids = (p[1] for p in params)
ids = _.uniq _.flatten ids
response["objects"] = ids.map (id) ->
obj = {}
for param in params
if id == param[1]
obj[param[0]] = extractValue param[0], data[param[2]]
return obj
return response
args = {
USER: @username,
PWD: @password,
SIGNATURE: @signature,
METHOD: method,
VERSION: API_VERSION,
}
for k, v of parameters
args[k] = v
request.post { url: @apiUrl, body: querystring.encode args }, (error, response, body) ->
if error
return callback?error
callback? null, processResponse body
module.exports = PayPal