forked from Kevnz/pxpay
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
157 lines (144 loc) · 5.35 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var request = require('request');
var xml = require('xml');
var url = 'https://sec.paymentexpress.com/pxaccess/pxpay.aspx';
var txtTypes = ['Purchase', 'Auth', 'Complete', 'Refund', 'Validate'];
var SUCCESS_STATUS = 1;
var FAIL_STATUS = 0;
var logOutput = false;
module.exports = {
generateRequest: function (details) {
if (details.logOutput) {
logOutput = details.logOutput;
}
var dataPayload = [
{ PxPayUserId: details.user },
{ PxPayKey: details.password },
{ TxnType: details.transactionType || 'Purchase' },
{ AmountInput: details.amount },
{ CurrencyInput: details.currency || 'NZD' }
];
if (details.successURL ) {
dataPayload.push( { UrlSuccess: details.successURL });
}
if (details.failURL ) {
dataPayload.push( { UrlFail: details.failURL });
}
if (details.transactionId) {
dataPayload.push({ TxnId: details.transactionId });
}
if (details.reference) {
dataPayload.push({ MerchentReference: details.reference });
}
if (details.email) {
dataPayload.push({ EmailAddress: details.email });
}
if (details.line1) {
dataPayload.push({ TxnData1: details.line1 });
}
if (details.line2) {
dataPayload.push({ TxnData2: details.line2 });
}
if (details.line3) {
dataPayload.push({ TxnData3: details.line3 });
}
if (details.billingId) {
dataPayload.push({ BillingId: details.billingId });
}
if (details.addCard) {
dataPayload.push({ EnableAddBillCard: details.addCard });
}
if (details.dpsBillingId) {
dataPayload.push({ DpsBillingId: details.dpsBillingId });
}
if (details.response) {
dataPayload.push({Response: details.response});
}
var dpsData = {
GenerateRequest: dataPayload
};
if (logOutput) {
console.log(dpsData);
}
return xml(dpsData);
},
generateResponse: function (details) {
var dataPayload = [
{ PxPayUserId: details.user },
{ PxPayKey: details.password },
{ Response: details.response}
];
var dpsData = {
ProcessResponse: dataPayload
};
if (logOutput) {
console.log(dpsData);
}
return xml(dpsData);
},
request: function (details, callback) {
var dpsData = this.generateRequest(details);
request({
uri: url,
method: 'POST',
body: dpsData
}, function requestCallback (err, res, body) {
process.nextTick(function requestHandler () {
if(err) {
process.nextTick(function(){
callback(err);
});
} else {
var parser = new require('xml2js').Parser({ explicitArray: false});
process.nextTick(function(){
parser.parseString(body, function parserHandler (error, result){
if (logOutput) {
console.log(error);
}
if(result.Request.$.valid === '1'){
process.nextTick(function(){ callback(null, result.Request); });
} else if (error) {
process.nextTick(function(){ callback(error); });
} else {
process.nextTick(function(){ callback({message: "result did not return correct code", result: result });});
}
});
});
}
});
});
},
process: function () {
},
parseResults: function(details, callback) {
var dpsData = this.generateResponse(details);
request({
uri: url,
method: 'POST',
body: dpsData
}, function responseCallback (err, res, body) {
process.nextTick(function responseHandler () {
if(err) {
process.nextTick(function(){
callback(err);
});
} else {
var parser = new require('xml2js').Parser({ explicitArray: false});
process.nextTick(function(){
parser.parseString(body, function parserHandler (error, result){
if (logOutput) {
console.log(error);
}
if(result.Response.$.valid === '1'){
process.nextTick(function(){ callback(null, result.Response); });
} else if (error) {
process.nextTick(function(){ callback(error); });
} else {
process.nextTick(function(){ callback({message: "result did not return correct code", result: result });});
}
});
});
}
});
});
}
};