2014-11-02 148 views
0

我正在學習如何處理信用卡支付。這裏是測試卷曲...將Curl請求轉換爲Node.JS服務器 - 將數據附加到請求中?

curl -k -v -X POST -H "Content-Type:application/json" -H "Authorization: Basic Mxxxxxxxxxxxxxxxxxx=" -d "@json_file.txt" -o output.txt https://w1.xxxxxxxxxxxx.net/PaymentsAPI/Credit/Sale 

凡json_file.txt包含

{ 
    "InvoiceNo":"1", 
    "RefNo":"1", 
    "Memo":"TEST_TEST_PHONY", 
    "Purchase":"1.00", 
    "AccountSource":"Swiped", 
    "AcctNo":"5xxxxxxxxxxxxxxxxx1", 
    "ExpDate":"0816", 
    "OperatorID":"xxxxxxxxxx", 
} 

我轉換到節點模塊HTTPS

var https = require("https"); 

var options = { 
    host: 'w1.xxxxxxxxxxxxxx.net', 
    port: 443, 
    path: '/PaymentsAPI/Credit/Sale', 
    headers: { "Content-Type" :"application/json", 
    "Authorization" : "Basic Mxxxxxxxxxxxxxxxxxxxxxxxxxxx="} , 
    data: { 
    "InvoiceNo":"1", 
    "RefNo":"1", 
    "Memo":"xxxxxxxxxxxxxxx", 
    "Purchase":"1.00", 
    "AccountSource":"Swiped", 
    "AcctNo":"5xxxxxxxxxxxxxxxxx1", 
    "ExpDate":"0816", 
    "OperatorID":"xxxxxxxxxxxx", 
    }, 
    method: 'POST' 
}; 

// oops... 400 Bad Request 
// The request could not be understood by the server due to malformed syntax. 

var req = https.request(options, function(res) { 
    console.log('STATUS: ' + res.statusCode); 
    console.log('HEADERS: ' + JSON.stringify(res.headers)); 
    res.setEncoding('utf8'); 
    res.on('data', function (chunk) { 
    console.log('BODY: ' + chunk.toString()); 
    }); 
}); 

req.on('error', function(e) { 
    console.log('problem with request: ' + e.message); 
}); 

// write data to request body 
req.write('data\n'); 
req.write('data\n'); 
req.end(); 

問題在原始curl請求上,JSON文本文件中包含的數據作爲POST附件提交。捲曲請求正常工作。另一方面,我不清楚如何從node.js服務器執行此操作。響應頭回來很好,但我得到了一個400響應(格式錯誤)。任何人都知道如何將JSON數據附加到HTTPS請求的附件中?

+0

爲什麼要投票?爲什麼沒有評論的投票呢? – zipzit 2014-11-04 03:10:31

回答

1

糟糕。我完全不明白req.write()的東西是如何工作的。這段代碼是成功的。感謝Dan Ourada @ Mercury Payments的幫助。請注意,這裏的所有代碼都是純沙盒。沒有真正的美元買入。

var https = require("https"); 

var options = { 
    host: 'w1.mercurycert.net', 
    port: '443', 
    path: '/PaymentsAPI/Credit/Sale', 
    headers: { "Content-Type" :"application/json", "Authorization" : "Basic MDAzNTAzOTAyOTEzMTA1Onh5eg=="}, 
    method: 'POST' 
}; 

var inputdata = JSON.stringify({ 
    "InvoiceNo":"1", 
    "RefNo":"1", 
    "Memo":"XXXXX", 
    "Purchase":"1.00", 
    "AccountSource":"Swiped", 
    "AcctNo":"549999", 
    "ExpDate":"0816", 
    "OperatorID":"money2020", 
    }); 

var req = https.request(options, function(res) { 
    console.log('STATUS: ' + res.statusCode); 
    console.log('HEADERS: ' + JSON.stringify(res.headers)); 
    res.setEncoding('utf8'); 
    res.on('data', function (chunk) { 
    console.log('Return info: ' + chunk); // output the return raw data 
    }); 
}); 

req.on('error', function(e) { 
    console.log('problem with request: ' + e.message); 
}); 

// attach input data to request body 
req.write(inputdata); 
req.end(); 

提供的信息在這裏情況下別人卡從curl命令在轉換爲Node.js的http請求......

哎,這種玩弄後,我在驚訝將真實(安全)的支付系統納入任何商家網站是多麼容易。 (很明顯需要https網站。)

+0

另外,請查看'request'庫,每個Jed Watson的回答,ref [this link](https://stackoverflow.com/questions/6158933/how-to-make-an-http-post-request -in-節點-JS) – zipzit 2017-10-22 09:06:36