Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Added Source Management - updating and replacing source
Browse files Browse the repository at this point in the history
  • Loading branch information
rldaulton committed Mar 1, 2017
1 parent 8f5232b commit 0240361
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Source Management/Add Payment Source/index.js
Original file line number Diff line number Diff line change
@@ -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"]));
});
});
32 changes: 32 additions & 0 deletions Source Management/Update Payment Source/index.js
Original file line number Diff line number Diff line change
@@ -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"]));
});

});
});

0 comments on commit 0240361

Please sign in to comment.