Skip to content

Instantly share code, notes, and snippets.

@julienma
Last active February 20, 2019 20:47
Show Gist options
  • Save julienma/7bb3f2e9298729b122cb4f990756db19 to your computer and use it in GitHub Desktop.
Save julienma/7bb3f2e9298729b122cb4f990756db19 to your computer and use it in GitHub Desktop.
CLI script to parse PayPal's NVP to JSON

Usage

Let's say you saved the output of a PayPal NVP API request to output.txt.

To parse it to JSON:

yarn start output.txt

An output.txt.json will be created.

let fs = require('fs');
let path = require('path');
let parse = require('paypal-nvp-parser');
// Make sure we got a filename on the command line.
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME');
process.exit(1);
}
function parsePayPal(filename) {
fs.readFile(filename, 'utf8', function(err, raw_paypal) {
if (err) throw err;
let parsed_paypal = parse(raw_paypal);
console.log(parsed_paypal);
writeParsedPayPal(parsed_paypal);
});
}
function writeParsedPayPal(parsed_paypal) {
fs.writeFile(path.join(process.cwd(), `${filename}.json`), JSON.stringify(parsed_paypal, null, 2), function(err) {
if(err) throw err;
console.log(`==> Saved as: ${filename}.json`);
});
}
// Read the file and print its contents.
let filename = process.argv[2];
parsePayPal(filename);
{
"name": "parse-paypal-nvp",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"paypal-nvp-parser": "^1.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment