Reputation: 4017
I'm learning how to process credit card payments.. Here is the test CURL...
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
Where json_file.txt contains
{
"InvoiceNo":"1",
"RefNo":"1",
"Memo":"TEST_TEST_PHONY",
"Purchase":"1.00",
"AccountSource":"Swiped",
"AcctNo":"5xxxxxxxxxxxxxxxxx1",
"ExpDate":"0816",
"OperatorID":"xxxxxxxxxx",
}
I converted over to node module 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();
Problem is on the original curl request, the data contained within the JSON text file is submitted as a POST attachment. Curl request works fine. On the other hand its not clear to me on how to do that from a node.js server. The response headers come back fine, but I get a 400 response (malformed syntax). Anybody know how to attach JSON data as a post attachment to a HTTPS request?
Upvotes: 0
Views: 1632
Reputation: 4017
Oops. I totally didn't understand how the req.write() stuff works.. This code is successful. Thanks to Dan Ourada @ Mercury Payments for his assistance. Note, all code here is pure sandbox. No real $$ going buy buy.
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":"5499990123456781",
"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();
Info offered here in case anybody else gets stuck converting from a CURL command over to a Node.js http request...
And hey, after playing around with this, I'm amazed on how truly easy it is to incorporate a real (and secure) payments system into any merchant website. (Obviously Https site required.)
Upvotes: 1