-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
executable file
·118 lines (116 loc) · 3.36 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
const xml = require('xml')
const Parser = require('xml2js').Parser
const prod = 'https://sec.paymentexpress.com/pxaccess/pxpay.aspx'
const uat = 'https://uat.paymentexpress.com/pxaccess/pxpay.aspx'
const txtTypes = ['Purchase', 'Auth', 'Complete', 'Refund', 'Validate']
const SUCCESS_STATUS = 1
const FAIL_STATUS = 0
module.exports = {
generateRequest: function(details) {
const 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.TxnId) {
dataPayload.push({ TxnId: details.TxnId })
}
if (details.transactionId) {
dataPayload.push({ TxnId: details.transactionId })
}
if (details.reference) {
dataPayload.push({ MerchantReference: 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 })
}
var dpsData = {
GenerateRequest: dataPayload,
}
return xml(dpsData)
},
request: function(details, callback) {
var rquest = require('request')
var dpsData = this.generateRequest(details)
var self = this
rquest(
{
uri: process.env.NODE_ENV === 'production' ? prod : uat,
method: 'POST',
body: dpsData,
},
function requestCallback(err, res, body) {
process.nextTick(function() {
self.process(err, res, body, callback)
})
}
)
},
process: function(err, res, body, callback) {
if (err) {
process.nextTick(function() {
callback(err)
})
} else {
var parser = new Parser({ explicitArray: false })
process.nextTick(function() {
parser.parseString(body, function parserHandler(error, result) {
if (error) {
process.nextTick(function() {
callback(error)
})
} else if (
!result ||
!result.Request ||
!result.Request.$ ||
!result.Request.$.valid
) {
process.nextTick(function() {
callback({
message: 'result did not return correct code',
result: result,
})
})
} else if (result.Request.$.valid === '1') {
process.nextTick(function() {
callback(null, result.Request)
})
} else {
process.nextTick(function() {
callback({
message: 'result did not return correct code',
result: result,
})
})
}
})
})
}
},
}