Permalink
Browse files

Merged pull request #5 from regular/master.

fixed some problems in payflow module
  • Loading branch information...
jamescarr committed Apr 26, 2011
2 parents caeb0d0 + 43de156 commit 3f8cfe9953cfd11705566e8659cc78ebb85d6234
Showing with 47 additions and 19 deletions.
  1. +1 −1 lib/gateway-client.js
  2. +34 −9 lib/payflowpro/paypal-network-client.js
  3. +12 −9 lib/payflowpro/response-parser.js
View
@@ -2,7 +2,7 @@ var qs = require('querystring'),
crypto = require('crypto'),
sys = require('sys'),
http_module = require('http')
-
+
function GatewayClient(opts, authInfo){
verifyRequiredArguments(opts)
var http = http_module
@@ -1,14 +1,39 @@
-
-var GatewayClient = require('../gateway-client').GatewayClient,
- paypalResponse = require('response-parser')
+var qs = require('querystring'),
+ https = require("https"),
+ paypalResponse = require('response-parser');
function PaypalNetworkClient(host, authInfo){
- var client = new GatewayClient({host:host
- ,contentType:'text/namevalue'
- ,path:'/nvp'
- ,responseParser:paypalResponse
- }, authInfo)
- this.request = client.request
+ this.request = function(request, callback) {
+ var requestString = qs.stringify(request)
+ var options = {
+ host: host,
+ port: 443,
+ path: '/nvp',
+ method: 'POST',
+ headers: {
+ "content-type":'application/x-www-form-urlencoded',
+ "content-length": requestString.length,
+ }
+ };
+ console.log(options);
+ var req = https.request(options, function(res) {
+ console.log("statusCode: ", res.statusCode);
+ console.log("headers: ", res.headers);
+ var dataList = [];
+ res.on('data', function(data) {
+ dataList.push(data);
+ });
+ res.on('end', function() {
+ var data = dataList.join("");
+ callback(paypalResponse.parseResponse(request, data));
+ });
+ });
+ req.end(requestString);
+
+ req.on('error', function(e) {
+ console.error(e);
+ });
+ }
}
exports.PaypalNetworkClient = PaypalNetworkClient
@@ -29,7 +29,7 @@ function buildResponse(request, data){
return result
}
- var newData = lowercaseKeys(qs.parse(data))
+ var newData = lowercaseKeys(qs.parse(data));
if(newData.l_errorcode0)
newData.errors = buildFrom(errorField, newData)
@@ -54,15 +54,18 @@ function buildResponse(request, data){
var addNestedFields = function(nestedFields, data){
return function(prop){
- var index = prop.charAt(prop.length - 1),
- newKey =prop.replace(/^l_/, '').replace(/\d$/, '')
-
- if(nestedFields.length == index){
- nestedFields.push({})
- }
- nestedFields[index][newKey] = data[prop]
- delete data[prop]
+ var match = prop.match(/^l_(\D*)(\d+)$/);
+ if (match) {
+ var index = parseInt(match[2]);
+ var newKey = match[1];
+
+ while(nestedFields.length <= index){
+ nestedFields.push({})
+ }
+ nestedFields[index][newKey] = data[prop]
+ delete data[prop]
+ }
}
}
var parsers = {

0 comments on commit 3f8cfe9

Please sign in to comment.