Skip to content

Instantly share code, notes, and snippets.

@amasses
Created October 2, 2012 06:29
Show Gist options
  • Save amasses/3816790 to your computer and use it in GitHub Desktop.
Save amasses/3816790 to your computer and use it in GitHub Desktop.
Node.js payment example
https = require('https');
var payload = {
amount: 1000,
card_holder: "Mark Smith",
card_number: "5123456789012346",
card_expiry: "05/2013",
cvv: "123",
customer_ip: "123.4.5.6",
reference: "NODE" + Math.random()
};
var post_data = JSON.stringify(payload);
var options = {
host: "gateway.sandbox.fatzebra.com.au",
port: 443,
path: "/v1.0/purchases",
method: "POST",
headers: {
"Content-Length": post_data.length,
"Content-Type": "application/json"
},
auth: "TEST:TEST"
}
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) {
var result = JSON.parse(chunk);
console.log("Gateway response: ");
console.log(" - Successful: " + result.successful);
if (result.successful) {
console.log(" - ID: " + result.response.id);
console.log(" - Authorization: " + result.response.authorization);
} else {
console.log(" - Errors: ");
console.log(JSON.stringify(result.errors));
}
});
});
req.on("error", function(e) {
console.log("Error with request: " + e.message);
});
var payload = {
amount: 1000,
card_holder: "Mark Smith",
card_number: "5123456789012346",
card_expiry: "05/2013",
cvv: "123",
customer_ip: "123.4.5.6",
reference: "NODE1"
};
req.write(post_data);
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment