forked from mateodelnorte/node-paypal-masspayments
/
index.js
130 lines (105 loc) · 3.89 KB
/
index.js
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
78
79
80
81
82
83
84
85
86
87
88
89
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
var request = require('request'),
qs = require('qs'),
util = require('util'),
_ = require('underscore');
var requiredOptions = ['pwd', 'user', 'signature'],
receiverTypes = {
'EmailAddress': 'L_EMAIL',
'UserID' : 'L_RECEIVERID'
},
validReceiverTypes = Object.keys(receiverTypes),
receiverTypeIsValid = function(receiverType) {
var receiverTypeIsValid = _.some(validReceiverTypes, function(validReceiverType) {
return validReceiverType.toLowerCase() === receiverType.toLowerCase();
});
return receiverTypeIsValid;
},
verifyOptions = function(options) {
if (!options) throw new Error('options must be defined for MassPay including pwd, user, signature, and receiverType');
var optionKeys = Object.keys(options);
var missingRequiredOptions = _.difference(requiredOptions, optionKeys);
if (missingRequiredOptions.length > 0) {
throw new Error('MassPay options missing required param/s: ' + missingRequiredOptions.toString());
}
if (options.receiverType && !receiverTypeIsValid(options.receiverType)) {
throw new Error('options.receiverType must be one of the following: ' + validReceiverTypes.toString());
}
};
function MassPay(options) {
verifyOptions(options);
this.vars = {
METHOD: 'MassPay'
, VERSION: (options.version) ? options.version : '117'
, PWD: options.pwd
, USER: options.user
, SIGNATURE: options.signature
, RECEIVERTYPE: (options.receiverType) ? options.receiverType : 'EmailAddress'
, CURRENCYCODE: (options.currencyCode) ? options.currencyCode : 'USD'
};
if (options.emailsubject) {
this.vars.emailSubject = options.emailsubject;
}
this.environment = (process.env.PAYPAL_ENV === 'production') ? 'live' : 'sandbox';
};
MassPay.prototype.pay = function pay(paymentBatch, callback) {
var vars = _.extend(_.clone(this.vars), paymentBatch.params)
, base_url = (this.environment === 'live') ? 'https://api-3t.paypal.com/nvp' : 'https://api-3t.' + this.environment + '.paypal.com/nvp';
request.post({
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
url: base_url,
body: qs.stringify(vars)
}, function(err, res, body) {
var response;
if (err) {
callback(err);
} else {
response = new MassPaymentResponse(body);
if (response.ACK === 'Success' || response.ACK === 'SuccessWithWarning') {
callback(null, response);
} else {
callback(new MassPaymentError(body));
}
}
});
}
module.exports = MassPay;
function MassPaymentResponse(body) {
_.extend(this, qs.parse(body));
}
function MassPaymentError(body) {
_.extend(this, qs.parse(body));
}
util.inherits(MassPaymentError, Error);
module.exports.MassPaymentError = MassPaymentError;
function PaymentRequest(receiver, amount, uniqueId, note) {
if(amount <= 0) throw new PaymentRequestInputError('');
this.receiver = receiver;
this.amount = amount;
this.uniqueId = uniqueId;
this.note = note;
}
module.exports.PaymentRequest = PaymentRequest;
function PaymentRequestInputError(message) {
}
util.inherits(PaymentRequestInputError, Error);
function PaymentBatch(paymentRequests, receiverType) {
if (!receiverType || !receiverTypeIsValid(receiverType)) {
throw new Error('receiverType must be one of the following: ' + validReceiverTypes.toString());
}
var receiverParamName = receiverTypes[receiverType];
var index = 0;
var returnVal = {};
var values = _.map(paymentRequests, function(request) {
var val = {};
val[receiverParamName + index] = request.receiver;
val["L_AMT" + index] = request.amount;
if (request.uniqueId) val["L_UNIQUEID" + index] = request.uniqueId;
if (request.note) val["L_NOTE" + index] = request.note;
index++;
_.extend(returnVal, val);
});
this.params = returnVal;
}
module.exports.PaymentBatch = PaymentBatch;