From 0240361254b9554afc0bdf28fc0f84bddbaa4790 Mon Sep 17 00:00:00 2001 From: Ryan Daulton Date: Wed, 1 Mar 2017 09:17:19 -0600 Subject: [PATCH] Added Source Management - updating and replacing source --- Source Management/Add Payment Source/index.js | 24 ++++++++++++++ .../Update Payment Source/index.js | 32 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 Source Management/Add Payment Source/index.js create mode 100644 Source Management/Update Payment Source/index.js diff --git a/Source Management/Add Payment Source/index.js b/Source Management/Add Payment Source/index.js new file mode 100644 index 0000000..cfdf3e9 --- /dev/null +++ b/Source Management/Add Payment Source/index.js @@ -0,0 +1,24 @@ +var app = require('express')(); +var http = require('http').Server(app); +var stripe = require('stripe')( + "your_stripe_key" +); +var bodyParser = require('body-parser'); + +app.use(bodyParser.urlencoded({ extended: true })); +app.use(bodyParser.json()); + +//add a payment source (card) to customerid +exports.addPaymentSource = app.get("/:customerid/:tok", function addPaymentSource (req,res){ + stripe.customers.createSource( + req.params.customerid, + { source: req.params.tok}, + function(err, card) { + // asynchronously called + if(err) { + return res.send(JSON.stringify(err)); + } + //returns new source id to client + res.send(JSON.stringify(card["id"])); + }); +}); diff --git a/Source Management/Update Payment Source/index.js b/Source Management/Update Payment Source/index.js new file mode 100644 index 0000000..c318aa1 --- /dev/null +++ b/Source Management/Update Payment Source/index.js @@ -0,0 +1,32 @@ +var app = require('express')(); +var http = require('http').Server(app); +var stripe = require('stripe')( + "your_stripe_key" +); +var bodyParser = require('body-parser'); + +app.use(bodyParser.urlencoded({ extended: true })); +app.use(bodyParser.json()); + +//update a card by deleting & replacing +exports.updatePaymentSource = app.get("/:customerid/:oldsourceid/:newsourcetoken", function updatePaymentSource (req,res){ + stripe.customers.deleteCard( + req.params.customerid, + req.params.oldsource, + function(err, confirmation) { + if(err) { + return res.send(JSON.stringify(err)); + } + stripe.customers.createSource( + req.params.customerid, + { source: req.params.newsource }, + function(err, card) { + if(err) { + return res.send(JSON.stringify(err)); + } + //returns the new source id only + res.send(JSON.stringify(card["id"])); + }); + + }); +});