stripe payment firebase cloud function not updating database correctly
up vote
0
down vote
favorite
I am using stripe payments in my app - trying to implement a user balance.
Currently, I have the following code in my index.js. The key bit for the question is the final .then()
block of code. This section writes to my Cloud firestore database. However, it doesn't seem to actually update my database. When I look at my database, the balance doesn't change nor does the charge field get added to the stripe payment object that is saved in the database. I don't get any errors and I am not sure how to debug it.
const functions = require('firebase-functions'); //firebase functions library
const admin = require('firebase-admin'); //firebase admin database
admin.initializeApp(functions.config().firebase);
//initialse stripe with apikey
const stripe = require('stripe')('<stripe test key>')
//name of function is stripe charge
exports.stripeCharge = functions.database
//whenever a new payment is written to the database, the following will be invoked
.ref('/payments/{userId}/payments/{paymentId}')
//event object has the data from the node in the database which is the payment and the token
.onWrite((change, context) => {
const payment = change.after.val();
const userId = context.params.userId;
const paymentId = context.params.paymentId;
let balance = 0;
//return null if there is no payment or if payment already has a charge
if(!payment || payment.charge) return;
//now we chain a promise - this is the user in the database
//this is good for recurring charges or to save customer data in the database
return admin.database()
.ref(`/users/${userId}`)
//once gives a single snapshot of this data
.once('value')
//return the snapshot value
.then(snapshot => {
return snapshop.val();
})
//tell stripe to charge the customer with the payment token
.then(customer => {
balance = customer.balance;
const customerChargeAmount = payment.amount;
const idempotency_key = paymentId; //prevents duplicate charges on customer cards
const source = payment.token.id;
const currency = 'gbp';
const charge = {customerChargeAmount, currency, source};
return stripe.charges.create(charge, {idempotency_key});
})
//wait for stripe to return the actual stripe object which tells us whether the charge succeeded
.then(charge => {
if(!charge) return;
let updates = {};
updates[`/payments/${userId}/payments/${paymentId}/charge`] = charge
if(charge.paid){
balance += charge.amount;
updates[`/users/${userId}/balance`] = balance;
}
admin.database().ref().update(updates);
return true;
})
})
Any help is appreciated.
angular firebase google-cloud-firestore stripe-payments payment
add a comment |
up vote
0
down vote
favorite
I am using stripe payments in my app - trying to implement a user balance.
Currently, I have the following code in my index.js. The key bit for the question is the final .then()
block of code. This section writes to my Cloud firestore database. However, it doesn't seem to actually update my database. When I look at my database, the balance doesn't change nor does the charge field get added to the stripe payment object that is saved in the database. I don't get any errors and I am not sure how to debug it.
const functions = require('firebase-functions'); //firebase functions library
const admin = require('firebase-admin'); //firebase admin database
admin.initializeApp(functions.config().firebase);
//initialse stripe with apikey
const stripe = require('stripe')('<stripe test key>')
//name of function is stripe charge
exports.stripeCharge = functions.database
//whenever a new payment is written to the database, the following will be invoked
.ref('/payments/{userId}/payments/{paymentId}')
//event object has the data from the node in the database which is the payment and the token
.onWrite((change, context) => {
const payment = change.after.val();
const userId = context.params.userId;
const paymentId = context.params.paymentId;
let balance = 0;
//return null if there is no payment or if payment already has a charge
if(!payment || payment.charge) return;
//now we chain a promise - this is the user in the database
//this is good for recurring charges or to save customer data in the database
return admin.database()
.ref(`/users/${userId}`)
//once gives a single snapshot of this data
.once('value')
//return the snapshot value
.then(snapshot => {
return snapshop.val();
})
//tell stripe to charge the customer with the payment token
.then(customer => {
balance = customer.balance;
const customerChargeAmount = payment.amount;
const idempotency_key = paymentId; //prevents duplicate charges on customer cards
const source = payment.token.id;
const currency = 'gbp';
const charge = {customerChargeAmount, currency, source};
return stripe.charges.create(charge, {idempotency_key});
})
//wait for stripe to return the actual stripe object which tells us whether the charge succeeded
.then(charge => {
if(!charge) return;
let updates = {};
updates[`/payments/${userId}/payments/${paymentId}/charge`] = charge
if(charge.paid){
balance += charge.amount;
updates[`/users/${userId}/balance`] = balance;
}
admin.database().ref().update(updates);
return true;
})
})
Any help is appreciated.
angular firebase google-cloud-firestore stripe-payments payment
1
Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
– TheUnreal
Nov 11 at 16:49
Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
– JonnySprouse
Nov 11 at 20:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am using stripe payments in my app - trying to implement a user balance.
Currently, I have the following code in my index.js. The key bit for the question is the final .then()
block of code. This section writes to my Cloud firestore database. However, it doesn't seem to actually update my database. When I look at my database, the balance doesn't change nor does the charge field get added to the stripe payment object that is saved in the database. I don't get any errors and I am not sure how to debug it.
const functions = require('firebase-functions'); //firebase functions library
const admin = require('firebase-admin'); //firebase admin database
admin.initializeApp(functions.config().firebase);
//initialse stripe with apikey
const stripe = require('stripe')('<stripe test key>')
//name of function is stripe charge
exports.stripeCharge = functions.database
//whenever a new payment is written to the database, the following will be invoked
.ref('/payments/{userId}/payments/{paymentId}')
//event object has the data from the node in the database which is the payment and the token
.onWrite((change, context) => {
const payment = change.after.val();
const userId = context.params.userId;
const paymentId = context.params.paymentId;
let balance = 0;
//return null if there is no payment or if payment already has a charge
if(!payment || payment.charge) return;
//now we chain a promise - this is the user in the database
//this is good for recurring charges or to save customer data in the database
return admin.database()
.ref(`/users/${userId}`)
//once gives a single snapshot of this data
.once('value')
//return the snapshot value
.then(snapshot => {
return snapshop.val();
})
//tell stripe to charge the customer with the payment token
.then(customer => {
balance = customer.balance;
const customerChargeAmount = payment.amount;
const idempotency_key = paymentId; //prevents duplicate charges on customer cards
const source = payment.token.id;
const currency = 'gbp';
const charge = {customerChargeAmount, currency, source};
return stripe.charges.create(charge, {idempotency_key});
})
//wait for stripe to return the actual stripe object which tells us whether the charge succeeded
.then(charge => {
if(!charge) return;
let updates = {};
updates[`/payments/${userId}/payments/${paymentId}/charge`] = charge
if(charge.paid){
balance += charge.amount;
updates[`/users/${userId}/balance`] = balance;
}
admin.database().ref().update(updates);
return true;
})
})
Any help is appreciated.
angular firebase google-cloud-firestore stripe-payments payment
I am using stripe payments in my app - trying to implement a user balance.
Currently, I have the following code in my index.js. The key bit for the question is the final .then()
block of code. This section writes to my Cloud firestore database. However, it doesn't seem to actually update my database. When I look at my database, the balance doesn't change nor does the charge field get added to the stripe payment object that is saved in the database. I don't get any errors and I am not sure how to debug it.
const functions = require('firebase-functions'); //firebase functions library
const admin = require('firebase-admin'); //firebase admin database
admin.initializeApp(functions.config().firebase);
//initialse stripe with apikey
const stripe = require('stripe')('<stripe test key>')
//name of function is stripe charge
exports.stripeCharge = functions.database
//whenever a new payment is written to the database, the following will be invoked
.ref('/payments/{userId}/payments/{paymentId}')
//event object has the data from the node in the database which is the payment and the token
.onWrite((change, context) => {
const payment = change.after.val();
const userId = context.params.userId;
const paymentId = context.params.paymentId;
let balance = 0;
//return null if there is no payment or if payment already has a charge
if(!payment || payment.charge) return;
//now we chain a promise - this is the user in the database
//this is good for recurring charges or to save customer data in the database
return admin.database()
.ref(`/users/${userId}`)
//once gives a single snapshot of this data
.once('value')
//return the snapshot value
.then(snapshot => {
return snapshop.val();
})
//tell stripe to charge the customer with the payment token
.then(customer => {
balance = customer.balance;
const customerChargeAmount = payment.amount;
const idempotency_key = paymentId; //prevents duplicate charges on customer cards
const source = payment.token.id;
const currency = 'gbp';
const charge = {customerChargeAmount, currency, source};
return stripe.charges.create(charge, {idempotency_key});
})
//wait for stripe to return the actual stripe object which tells us whether the charge succeeded
.then(charge => {
if(!charge) return;
let updates = {};
updates[`/payments/${userId}/payments/${paymentId}/charge`] = charge
if(charge.paid){
balance += charge.amount;
updates[`/users/${userId}/balance`] = balance;
}
admin.database().ref().update(updates);
return true;
})
})
Any help is appreciated.
angular firebase google-cloud-firestore stripe-payments payment
angular firebase google-cloud-firestore stripe-payments payment
asked Nov 11 at 16:26
JonnySprouse
797
797
1
Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
– TheUnreal
Nov 11 at 16:49
Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
– JonnySprouse
Nov 11 at 20:37
add a comment |
1
Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
– TheUnreal
Nov 11 at 16:49
Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
– JonnySprouse
Nov 11 at 20:37
1
1
Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
– TheUnreal
Nov 11 at 16:49
Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
– TheUnreal
Nov 11 at 16:49
Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
– JonnySprouse
Nov 11 at 20:37
Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
– JonnySprouse
Nov 11 at 20:37
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250761%2fstripe-payment-firebase-cloud-function-not-updating-database-correctly%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Check your firebase functions log history and check if it has been executed at all. if not, make sure you make an update on your user payment node in the DB
– TheUnreal
Nov 11 at 16:49
Checked the logs, function is definitely being executed. How can I ensure an update is being made on the user payment node? One part of the index.js code also creates a stripe object and stores it in my DB. But, in the last .then block, nothing is being updated.
– JonnySprouse
Nov 11 at 20:37