-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
51 lines (46 loc) · 1.3 KB
/
app.js
File metadata and controls
51 lines (46 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var firebase = require("firebase");
firebase.initializeApp({
serviceAccount: "firebase-cred.json",
databaseURL: "https://echo-pmt.firebaseio.com"
});
var stripe = require("stripe")(
""
);
var db = firebase.database();
var ref = db.ref("/");
ref.on("child_added", function(snapshot) {
console.log(snapshot.key, snapshot.val());
var orderRef = ref.child(snapshot.key);
var order = snapshot.val()
stripe.customers.create({
source: order.payment
})
.then(function(customer) {
// Save customer id to Firebase
var customerRef = orderRef.child("customer");
return customerRef.update({
stripeId: customer.id
})
.then(function() {
return customer;
});
})
.then(function(customer) {
console.log("customer id saved")
// Charge customer
return stripe.charges.create({
amount: order.product.price*100, // Amount in cents
currency: "usd",
customer: customer.id
});
})
.then(function(charge) {
// Save charge to Firebase -- similar to product, using orderRef (nothing overwritten)
var chargeRef = orderRef.child("charge");
return chargeRef.update({
charge: charge.id
})
});
}, function(errorObject) {
console.log("The read failed: " + errorObject.code)
});